18 ms·
A Vim Guide for Advanced Users
- rualca 6y agoSuperb submission! I've been using vim for over a decade and I never knew about gx or gf. Thanks!
- RMPR 6y agoI was meant to write a blog post like this since forever. Anyway, if you want really advanced stuff this[0] is definitely a way to go. As for ```gx``` since I'm using aerc as my email client, a trick I found very useful to open links using only the keyboard is to open an email then rq (reply with quote) search for the link and gx. 0: https://www.ele.uri.edu/faculty/vetter/Other-stuff/vi/009-index.html https://www.ele.uri.edu/faculty/vetter/Other-stuff/vi/009-in... Edit: add the bit about aerc
- thanato0s 6y agoWow this looks super interesting. Thanks for sharing :)
- RMPR 6y agoNo problem, I also wrote a blog post about Vim sessions[0]. I think it definitely counts as intermediate/advanced vim material. Great work with your article. 0: https://rmpr.xyz/Vim-Session/ https://rmpr.xyz/Vim-Session/
- thanato0s 6y agoNice! I did that for managing my vim sessions: " Save session nnoremap <leader>ss :mksession! $VIMCONFIG/sessions/ " Reload session nnoremap <leader>sl :so $VIMCONFIG/sessions/
- issamehh 6y agoDo you have any more aerc tricks? I use it and do like it but there's a whole lot of basic things even that I don't know how to do. And some things like saving file attachments that I'm not even sure if it supports or if I'm just clueless
- RMPR 6y agoYou can browse the different parts of your email using Ctrl+J/K when you reach the attachment you can save it with :save
- tjoff 6y agoRelated, gF will also scroll down to the line number if specified as: File.txt:45 Very handy when using a terminal from within vim and looking at compiler errors/warnings or log files etc.
- thanato0s 6y agoNice one. Didn't know that. I'll add it to the article :)
- thanato0s 6y agoThanks for the kind words! I'm happy it helps.
- benrutter 6y agoSame here, with vim it's so easy to so learning once you can navigate comfortably, but I'm really glad I know this now!
- yewenjie 6y agoNice. I would love to see more advanced guides for vim.
- thanato0s 6y agoOh I don't want to stop there, especially when I see the crazy positive returns I get ;) So it will come.
- OskarS 6y agoThe article mentions ”gn” in passing, but it doesn’t give it enough credit: this is an enormously useful motion, almost more useful on its own than the rest of the article. The thing that makes it golden is that it is a motion, and thus combined with a command, it is repeatable! So, like: 1. Search for something (either using / or *). 2. Type ”cgn” in Normal mode (i.e. change next search match) and replace what you searched for with whatever 3. Hammer on ”.” (i.e. repeat last command) to replace other matches. This replaces, like, 97% of uses of :s and is a much more convenient way to do search/replace, especially if you use * to search for the word under the cursor. It’s the bee’s knees!
- throwoutttt 6y agoAs someone who uses this regularly, it's still not as convenient as multi cursors that most modern ides give you. There's a vim extension for multiple cursors but it's a little buggy.
- OskarS 6y agoI dunno, I’ve also used both, and I keep coming back to the Vim/gn way of doing it, I just like it more. I see how people can feel otherwise, though.
- boogies 6y agoYeah, not everyone is the same, but there are definitely people interested in multi-cursor modal editors, enough to build their own, eg. vis⁰ — a light, fast, and simple text editor with a pretty standard vim normal mode but sam’s structural regex command language¹ for creating and procedurally editing selections in command mode², and kakoune³ — known for switching vi’s word order from verb noun to noun verb, so typical normal mode commands work by first creating one or more selections with motions (no need to press v), and only then operating on them. And of course Emacs has multi-selection modes and AFAIK they’re compatible with evil. ⁰https://sr.ht/~martanne/vis/ https://sr.ht/~martanne/vis/ ¹http://doc.cat-v.org/bell_labs/structural_regexps/ http://doc.cat-v.org/bell_labs/structural_regexps/ ²http://doc.cat-v.org/bell_labs/sam_lang_tutorial/ http://doc.cat-v.org/bell_labs/sam_lang_tutorial/ ³http://kakoune.org/ http://kakoune.org/
- JohnCurran 6y agoHow does gI differ from I?
- asicsp 6y ago`gI` will always go to column 1 whereas `I` will go first non-blank character
- graywh 6y agoin other words, I = ^i and gI = 0i
- kludgeon 6y agothe article has a tricky typo here. it says "The keystroke `I` insert text after the first non-blank characters of the line." but it should say _before_ the first non-blank, which is equivalent to _after_ the last leading blank.
- thanato0s 6y agoVery good point. I'll fix that. Thanks!
- thomastjeffery 6y agoThe same way that 0 differs from ^.
- orthecreedence 6y agoThank you! Two things I really got from this: Capital marks. I use ma or ms and `a `s a lot, but did not know you could do global marks with mA and `A. Very useful! I've been using `` for a long time to mean "go back to the last place I was editing" but sometimes it gets overridden when navigating (I'm not super familiar with the jumplist, so maybe I need to get a better understanding) but `[ and `] are exactly what I've been looking for all these years.
- ryannevius 6y agoI use global marks for things like my .vimrc (`V), .zshrc (`Z), and other config files that I edit semi-regularly. Definitely a useful tool for the box.
- thanato0s 6y agoYou should try g; and g, You'll go through the change list. I think it's what you want. I explain it in the article vim-intermediate on the same blog :)
- orthecreedence 6y agoYes, thank you, I just read that article as well and that is what I was looking for =]
- danidiaz 6y ago> did not know you could do global marks with mA and `A. Incidentally, the "Vimium" browser extension also supports those marks with the same syntax; they're an interesting alternative to conventional bookmarks for frequently accessed pages. https://github.com/philc/vimium/blob/master/README.md https://github.com/philc/vimium/blob/master/README.md
- nafizh 6y agoPractical vim by Drew Neil is hands down the best vim learning resource out there. Nothing even comes close.
- maxioatic 6y agoThis book is so good I actually bought it twice. The second time was the ebook edition just so I can pull it up next to Vim. Highly recommend it.
- thanato0s 6y agoTotally agree with that. That's my favorite book about Vim for sure.
- mesaframe 6y agoI thought it will be a 100 page book but hot damn. 487 Pages for what?
- dllthomas 6y agoWhat are the high points? Would you expect it has much to add for someone who's been using vim for closer to 3 decades than 2?
- dm319 6y agoIn some ways it's quite conceptual about the best way to use vim. Inside the cover there's a quote "I thought I knew vim, but practical vim has massively improved my code-wrangling productivity".
- JNRowe 6y agoDrew Neil's Modern Vim is also wonderful. While it introduces a lot of plugins in an attempt to modernize the experience, it also includes some excellent notes on standard features(qf/sessions/etc). I'll also note it covers both vim and neovim, with nice little asides explaining the differences where they matter.
- teeray 6y agoAnyone looking to pick this up: I found the best way to consume it is to grab a couple tips at a time (it’s structured as a “recipe book”) and incorporate them into your workflow. For example, try Netrw for a week, then another week try out folds. You don’t have to read it cover to cover to get the full benefit of it.
- craigary 6y agoNice, but still wondering if I should know vim since nano is enough for me :)
- lucb1e 6y agoCan you tell me how to exit nano without writing changes? Every time some silly default server kicks me into nano I'm absolutely lost and ctrl+c doesn't give you help text like in vim iirc. (Not kidding)
- calmoo 6y agoIt's CTRL+X and then N to not save changes
- lucb1e 6y agoThanks! Gonna try to remember that combo.
- Smaug123 6y agoIt appears in the table of commands at the bottom of the screen by default, doesn't it?
- lucb1e 6y agoI don't remember exactly, I just remember having a hard time exiting nano but not vim (also not before I knew vim: it just tells you what to do when you press the usual ctrl+c). From the sibling comment to yours, it says ^X which probably wasn't clear to me is supposed to be ctrl instead of shift. With hindsight it seems quite obvious, I'll admit.
- mkl 6y agoYou don't even need to remember it. At the bottom it lists keyboard shortcuts, and on the left is "^X Exit". When you press it with a modified file, it gives you: Save modified buffer? Y Yes N No ^C Cancel
- Agentlien 6y agoThis was a really nice refresher and I learned a few new cool tricks (like gx). I'm a bit surprised that the 'u' flag for sort wasn't mentioned. That's the most useful one in my mind and I use it almost every time.
- residualmind 6y agoMy thoughts exactly about ':sort u'
- lucb1e 6y agoThe u option making it keep only unique lines, for anyone else left wondering. Personally, instead of re-learning how to do stuff with vim-sepecific commands, I just use the existing command line tool, especially since I don't do this on a daily/weekly basis (but I use sort in command line pipes at least weekly). The command is literally "sort" and for unique you just pass the -u flag, e.g. select something in Vim and type :!sort -u
- ravoori 6y agoVim's sort is handy on Windows where the option to shell out usually doesn't exist. Additionally you have the full power of Vim regex to sort on subsets of lines.
- thanato0s 6y ago... I forgot it :D I'll fix that. Thanks to point it out :)
- rcgs 6y agogf is a game changer! Great article – hard to soak up so much knowledge at once. Bookmarked.
- thanato0s 6y agoI agree, it's a bit dense, but like somebody else was saying on this thread, I think picking one or two things from it and trying to include it in your worfklow for a limited period of time (say one week) will work better than trying to learn everything at once. Then you can try new stuff the next week, and so on. Rinse and repeat :)
- happy-go-lucky 6y agoOne of my favorites is the Vim's ability to split both horizontally ( <Ctrl>+<w>, then <v> ) and vertically ( <Ctrl>+<w>, then <s> ).
- deleted 6y ago[deleted]
- dllthomas 6y agoThe section on the quickfix list should include cbuffer, which puts the contents of the current buffer in the quickfix list.
- thanato0s 6y agoThat's a good point. I'll add it :)
- cma 6y agoIs there a good way by default to wrap a visual selection in braces/parens/brackets?
- ludsan 6y agoI use vim-surround. https://github.com/tpope/vim-surround https://github.com/tpope/vim-surround
- caymanjim 6y agoGreat plugin. Everything Tim Pope produces is gold. All Vim users should check out his whole collection on GitHub.
- SpaceNugget 6y agoA fairly easy way to do it is either c()^[P or c(^r") The first leaves you in normal mode the second in insert mode. I recommend vim-surround by tpope though.
- thanato0s 6y agoI used vim-surround for a long time, but I prefer vim-sandwich now: https://github.com/machakann/vim-sandwich https://github.com/machakann/vim-sandwich
- khnov 6y agoUse nano
- ravoori 6y agoAs far as I am concerned, this constitutes a good read for the VIM pro. It's a shame that critical comments tend to bubble up to the top on HN
- benatkin 6y agoVim is getting more popular. Not just within vim, but outside vim! Practically every full-featured editor has a vi or vim mode, and when they say vi, usually they also mean vim. Things that have nothing to do with vim but are inspired by vim are even sprouting up! And apparently it's OK to name something based on vim: https://groups.google.com/g/vim_use/c/EnSMrx_rGg4/m/d_nVlmLQCQAJ?pli=1 https://groups.google.com/g/vim_use/c/EnSMrx_rGg4/m/d_nVlmLQ... Edit: In Bram's reply he mentioned the household cleaner. Apparently it's similar to Ajax. That's so awesome. https://en.wikipedia.org/wiki/Vim_(cleaning_product) https://en.wikipedia.org/wiki/Vim_(cleaning_product)
- CoffeePython 6y agoI definitely agree that it’s getting more popular. I use vim inside VS code as my main editor and love the experience. It was also an easy way to step into learning vim. I also have a vim product with vim in the name (it’s practically the whole name) and haven’t had any sort of trademark issues.
- benatkin 6y agoYeah, the current situation, with no registered trademark for vim the editor, seems to be great for everyone! Vim is popular enough that it's unlikely to be taken. However, someone is currently trying to claim Screen for a screen sharing product despite a screen sharing tool called screen(1) having been around for decades, so nothing is impossible.
- teeray 6y agoI used to work near a gym called “Vim Fitness”[0] in Cambridge, MA and there were endless jokes about trainers calling out: “H! J! K! L!” [0] https://vimfitness.com/ https://vimfitness.com/
- chestervonwinch 6y agoI bet it's constantly crowded since no one can figure out how to exit.
- jzer0cool 6y agoOne of my goals is to become a really good vim power user. I am not able to pick up all of these but one here and there. In the first command gf, once new file opened and then closed (wq), is there a a way to close such that the previous file that was originally opened to return? At moment returning back to shell prompt. Also curious if there is a way to open the file as a new split screen rather than replacing the entire window?
- lucb1e 6y ago> is there a a way to close such that the previous file that was originally opened to return? If I understand the question correctly then the answer is ":bd" after ":w"riting the file. If you just want to switch between buffers (switch between the two files) then use ":b#". You can also list open buffers with ":ls" Someone recently told me of :tabedit, though, and that is lookin' way nicer to me. But I haven't gotten used to it yet or know how to make it work with gf or other handy things. As for split screen, those are hotkeys I have yet to learn. If someone has an answer to that question, please reply here so the parent commenter and I both see it :)
- alex_smart 6y agowq writes the current file and closes the current window (which quits vim if you happen to be on the last window). The command to close the current buffer is :bd(elete). If you want to return to the previous file without closing the new file, you can just use :bp(rev) so that the new file also stays in memory. Relevant man page is available at :help buffers. >Also curious if there is a way to open the file as a new split screen rather than replacing the entire window? CTRL-W f Another remark: vim's shortcuts are organized in a way that you are often able to guess what the correct shortcut should be. CTRL-W is the common prefix for commands regarding split windows CTRL-W v/s for splits, CTRL-W CTRL-W cycle through windows. So naturally you can guess that CTRL-W f should be open file in split and CTRL-W ] should be jump to definition in split.
- lucb1e 6y agoThe article mentions gI but that's 3 keystrokes (g, shift, i) when you can do 0i, and you probably want to know about 0 and i regardless, making the extra learning of gI unnecessary. Unfortunately this doesn't help anyone because it's not as if by deleting gI from your memory you can more easily learn something else, but perhaps it's helpful for a future guide writer or perhaps someone knows why gI is useful. Edit: The section "The Insane Expression Register" also doesn't make much sense to me. Why would I do this 'system("ls")' thing when there is '!ls'? Using vimscript instead of bash probably has advantages in, y'know, vim, but most people know their shell and probably very few people know vimscript (certainly not me) so I'm not quite clear on the advantages here.
- the_jeremy 6y agoRe your edit: they explain it pastes the output of the command directly into your buffer, rather than temporarily showing you a shell with the output.
- CGamesPlay 6y agoOne difference is that `gI` is repeatable with `j.`, but `0i` is not.
- bobbylarrybobby 6y agoThe number of keystrokes is an interesting number to use because it is only roughly correlated with the time it takes to press the keys. Both 2dd and 2[] are three keys, yet the latter can be typed much faster than the former as it is spread over both sides of the keyboard and it doesn’t repeat a key (so you can “roll” your fingers over the [] which is faster than double pressing the d). In the case of g-shift-I, I’d call it 2.5 keys. Assuming you press g and I with the right hand, the shift key hardly matters in terms of time. Of course, by the same logic, 0i is faster still.
- lucb1e 6y agoOne thing I've never been able to figure out in Vim: why does O (insert new line above the current line and enter insert mode) often wait and hang? And when you start typing, it'll sometimes do random stuff? Is there a way to just make it behave like a true counterpart of o (insert new line below and enter insert mode)?
- leadingthenet 6y agoI think I remember having this issue in vim too. Don’t remember it ever crossing my mind since I’ve started using neovim, though, so maybe give it a try?
- drewfish 6y agoTook some effort to refine a websearch: https://stackoverflow.com/questions/2158516/delay-before-o-opens-a-new-line https://stackoverflow.com/questions/2158516/delay-before-o-o...
- lucb1e 6y agoThanks! I never use arrows in insert mode so that's an easy choice for me, but removing 'nocompatible' from my vimrc does nothing and setting 'compatible' instead has side effects like not showing what mode I'm in. Not sure I need it, but I'm also wondering what other side effects I'll run into. The help file mentions a ton of things that I frankly don't care enough to comb through. The only thing I randomly spotted (and knew what it meant without digging deeper) is that this also breaks backspace in insert mode. Overall it makes vim feel quite a lot like vi. Instead, setting nocompatible and timeoutlen=1 (1ms I guess? Help file doesn't say) works fine in my terminal, I guess because it's not over any sort of network connection. I'll give this a go and see if I run into any issues when using these escape-based features (afaik I use those only rarely) over the network.
- gnubison 6y agoWoah, I just looked at the Vim help page for 'timeoutlen' and the fact that it's in milliseconds is only mentioned below, under 'ttimeoutlen'. The Neovim help page specifies that it's in milliseconds. I think that the Stack Overflow answer was just saying that the default depends on 'compatible'; not that the recommended solution is to go back to 'compatible'.
- jimnotgym 6y agoEveryone who uses Vim is an advanced user. The rest of us have to be content learning how to get back out of it without closing the terminal.
- thanato0s 6y agoThis question of what mean "intermediate", "advanced", "expert", or "master of the universe" is purely subjective to me. I've named these articles like I did to create some sort of progression, that's all. So yes, I think you're right, like everybody :D
- lucb1e 6y agoThe number increment example doesn't seem to work for me. It says to start with this: 1. a 1. b 1. c The post says to "select in VISUAL mode the last two lines and hit `g CTRL+a`". So I guess with shift+v select the bottom two lines, and then I hit g, space, and Ctrl+a. I get: 1. a 2. b 2. c The post says that the last line should now start with 3 instead of 1, or in my case 2. The behavior is the same as when omitting g+space for me. Am I doing it wrong?
- tux1968 6y agoDon't press space. "g" and then "ctrl-a"
- lucb1e 6y agoThank you!
- mklein994 6y agoThe docs for this is in `:help v_CTRL-A`. It should actually say VISUAL BLOCK mode. This is where you use `CTRL-v` to select a block of text. You can read more in `:help blockwise-visual`. Also see the 'nrformats' option: `:help 'nrformats'`. You can include alphabetic characters to do the same thing with A-Z, for example. https://vimhelp.org/change.txt.html#v_CTRL-A https://vimhelp.org/change.txt.html#v_CTRL-A
- forgotmypw17 6y agoWhat an amazing guide, as well as the others. I think these are the best Vim guides I've ever read. Vim user for years.
- thanato0s 6y agoWow, thanks a lot for the kind words!
- javitury 6y agoThose commands may look useless at first sight, but combined can create powerful routines. As an example, my routine to refactor non-typesafe codebases. - Find the pattern that you want to replace (the last dot is for current dir) :grep -r 'pattern' . - Open the quickfix window and check if matches are correct :copen - Find and replace the entries of the quickfix list :cdo s/pattern/replacement/ | update
- thanato0s 6y agoThanks for sharing! It's always inspiring to see different workflows.
- the_jeremy 6y agogx is cool, but seems broken on mac at the moment: https://github.com/vim/vim/issues/4738 https://github.com/vim/vim/issues/4738
- geniium 6y agoThanks for the tips! Very interesting read.
- gauchojs 6y agoFor my daily tasks, VSCode works better than Vim. But, sometimes, I'll have to do a macro in Vim, end up staying there for a couple of days and I have to admit: using those commands, motions and macros is pretty fun. It feels like gamifying work for me, adding an extra layer of enjoyment to the frequently boring tasks. Eventually I notice that I'm playing with Vim instead of finishing tasks, and go back to VSCode...
- lajarre 6y agoI can't get my location list to fill up. `:lex system("ls")` then `:llist` works (files are listed). But after `:lex system("ag something")`, `:llist` doesn't show anything.