Learning Vim The Hard Way


#1

As discussed in chat by a few of us, I’m committing this week to learning Vim to a level that I can actually replace my other text editors. I’ve followed Zed’s lead in binning the IDE’s, but I’d like to bin VSC and Atom also in favour of Vim.

After watching a few tutorials and messing about on open vim.com, I was really impressed with the commands and ability to do cool stuff like “delete contents between parens”, etc.

I appreciate its a learning curve, as it is different from what I am used to, but the rewards seem to outweigh the effort.

I’m about to set up my own .vimrc config, so if anyone has any tips, or a .vimrc that works really well, please share here.

Thanks
G

BTW - found this quite good… https://www.glump.net/files/2012/08/vi-vim-cheat-sheet-and-tutorial.pdf


#2

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:

  1. 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.
  2. 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
  3. 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.
  4. 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 (.
  5. 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.
  6. 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.
  7. Use c to change text in boundaries. ci( will change everything inside parens. ci" inside quotes. cw changes a word.
  8. 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.
  9. 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.


#3

@zedshaw Are you happy to share your .vimrc file? I’d like to save a decent one to git before playing about.


#4

I have to clean it up first, but what you should probably do is check out pathogen which is a Vim plug-in management thing that works pretty good.