4 ms·
I'm shocked that this doesn't cover keyboard macro recording. If you press q and then some letter, then do something, and press q again, it will record that as
by silentbicycle 18y ago
I'm shocked that this doesn't cover keyboard macro recording.
If you press q and then some letter, then do something, and press q again, it will record that as an action. For instance, qadWElpbq ("Start recording this in register a: cut next word, move to end of next word, forward char, paste, backward to beginning of that word. Done recording.". Basically, move the word under the cursor ahead one word.) under a (the qa) at the beginning, and then to repeat it you can just type @a. This is frequently straightforward for things that would be awkward with regex find-and-replace.
This works with the numeric prefixes, too; you could then type 10@a to shift a word ahead ten words, or double-space the next 200 lines, or what have you. Note, though, that vim's . repeat operator doesn't see the macro as one unit, so to repeat it you have to specify quantity.
Once your fingers learn this, you will use it all the time.
You can do this in basic vi, but as far as I know you have to manually stuff the macro into the character register* by writing it out and deleting it into a register, e.g. "ad$ (that's "a for into-register a, d$ for delete-to end-of-line). This feels more awkward than just hitting q (register) to start recording, but it's still useful. (* In BSD's nvi, at least.)
(The same thing in Emacs is C-x ( to start recording, C-x ) to stop recording, and C-x e to execute the last macro. There are commands to name and save your keyboard macros in a more permanent fashion, but I don't remember what they're called. Anything that isn't a throwaway macro I tend to just write in elisp anyway.)
- matstc 18y ago> I'm shocked that this doesn't contain keyboard macro recording. Yes. And maps (e.g. :map <F12> :w<cr> to save with F12). And block mode (Ctrl-V) and text objects (e.g. ci" deletes the inside of parentheses and goes to insert mode) and a slew of others. I find the idea that there are 100 vim commands every programmer should know a but ludicrous though.
- fallintothis 18y agoci" deletes the inside of parentheses I think you mean inside quotes, by the way. I find the idea that there are 100 vim commands every programmer should know a but ludicrous though. Especially considering that the "100" commands were all compositions of simpler vim commands anyways. I don't think this is actually that great a submission, for this reason. How many ways are there to rehash some really specific example -- say, counting both ":set syntax [on|off]"? Vim is so much more general than that. It seems inaccurate -- even discourteous -- to treat this as some laundry list of commands in your bag of tricks (except in the case of, say, muscle memory). From my experience, you don't memorize some long string of letters as a command; you think of what each tiny bit does in and of itself, then compose that into whatever you want to accomplish. Rather than commit the string "ggguG" to your brain, vim users think of it as "go to the beginning of the file (gg), then uppercase (gu) to the end of the the file (G)". Granted, vim gets more complicated as you delve into the realm of motions, .vimrc level configurations, ex command line, etc. Also, it seems that vim users would be the real ones who need to know such things, not every programmer altogether. The title feels cheap / sensationalized that way. If there were some REAL list of vim commands "every programmer should know" (well, vim-user), I'd at least start by recommending the alphabet...
- joshuaxls 18y agoI agree wholeheartedly with pretty much everything you've said here. There is no magic "Vim cheat sheet" for new Vim users. To understand the power of Vim, you have to understand the power of its parts. It reminds me of playing with K'NEX as a kid: you either learn how to build magnificent things by learning how pieces combine to form powerful designs, or you blindly follow the built-in diagrams for prefabricated creations (not really understanding how they came to be) and then dump it a few days later out of boredom. That said, I up-voted this submission solely because it's showing off Vim. That's what I try to do with the programmers I meet -- evangelize about Vim. I'll never forget the first time I saw an ancient Vim user hacking away at code. It seriously blew me away, and I knew that I had to learn it. Via a combination of :help and O'Reilly's "Learning the Vi Editor", I learned. Four years later (and that's young for a Vim user), I'll never look back. Point being: you can't learn via a cheat sheet. Dive into Vim, starting from the basics.
- palish 18y agoWell, I found the list to be very useful. I did not know about "/jo[ha]n", for example.
- BrandonM 18y agoThat's basic regular expression syntax. Learning that will go a long way in helping you learn a lot of tools in Unix, including grep, sed, vim, emacs, perl (which generally has a slightly different syntax, but one that matches in this case), bash programming in general, and more.
- Jasber 18y agoI frequently use: set pastetoggle=<F11> This toggles paste mode which allows you to paste without indention getting screwed up.
- BrandonM 18y agoNote, though, that vim's . repeat operator doesn't see the macro as one unit, so to repeat it you have to specify quantity. True, but @@ repeats the last keyboard macro.
- graywh 18y agoAnd @: repeats the last Ex command. And wow, this list of commands is awful. Just use "vimtutor" instead.