Alright, so when you’re learning vim it helps to try to avoid the arrow keys. Other people will tell you to use the ghjk or whatever home row keys to arrow around, but that defeats the entire point of vim. Vim’s power is in doing giant moves and edits with small commands. What helps understand this is to learn a bit of history about Vi:
Back in the day people had to edit files on big computers using tiny phone lines. Emacs came from MIT where they had blazing fast networking. Vi comes from Berkeley where I guess they only had 300bps lines (that’s BITS per second). When you have fast networking you can do a full screen editor with immediate feedback. When you have 300bps you have to treat the remote text file like it’s a transactional database and send tiny commands then wait for a reply.
So, in vim you can think of your text files as tiny databases that you are sending editing transaction to. You don’t want to mouse around one character at a time because that returns a lot of screen updating back to you. What you want to do is send a tiny command that demolishes a ton of text at a time. In a way, this 300bps ended up making vim faster because you have to think a little bit more about your edits, but when you do them a few keys modify tons of text.
Here’s the highlights of how you do this:
- Use the . to repeat the last thing you did. Let’s say you replace a word with cw. Then you can use / to search for the next place to do that and just hit . Then you do n . n . n . and you just did it all.
- Use :%s/TEXT/REP/g to just do #1 in one move. The TEXT is a regex, and you don’t have to use / to separate them. For example, if I want to replace all the directories mentioning /apple/ with /cars/ I can do this :%s,/apple/,/cars/,g
- Turn on line numbers in your .vimrc and then you can just jump to the ones you see on the screen. Don’t arrow down to them. If you need to go to line 34, then you do 34G. Done.
- Use f and F to jump to specific characters on the line. So after you go to 34G, you f( and and you’re at the first (.
- Use a number prefix to give a count. If you want the 3rd ( then 3f(. This works on most commands to repeat them any number of times.
- Use / to jump to exact text, and add an /e to jump to the end of that. Then use n to go through all the matches.
- Use c to change text in boundaries. ci( will change everything inside parens. ci" inside quotes. cw changes a word.
- When the region is a bit complex, use v to highlight what you want to replace, then use f and / to go to the end of it. Then s to substitute it and make your change.
- Create macros and repeat them. Hit q and any letter. It’ll say “recording”. Then do your edits using all the above things so that they work no matter where you are in the file. At the end hit q again. Then his @ and the letter to do all that again. You can repeat it with @@. You can also add the count modifer to do it X times so 10@@ will repeat the macro 10 times.
That’s just a sample, as vim has crazy numbers of these kind of relative big edit commands. The main idea is to try to do them in huge steps rather than arrowing around, and then use . and macros to repeat things quickly.