Intermediate/Advanced VIM Movments

VIM is a very powerful text editor. It or it's predecessor, VI come installed on every nix based computer. If you ever need to edit a file in a nix operating system, you can type 'vi filename' and be able to use VI or VIM. Beyond always being available, VIM is extremely powerful. VIM users hail it as one of the most efficient text editors and often choose to use it over IDEs for their programming needs.

VIM can be edited and setup to do just about anything most IDEs can do. It has a ton of plug-ins and customizable commands. But even the basic commands that VIM comes with allow many of program and edit files faster than other text editors or IDEs.

The basic commands, such as 'h', 'j', 'k', 'l' are slow. Using numbers to speed things up is not that much better. '16k' will move you to the right 16 characters, but counting out how far to move can be just as inefficient as if you pressed 'k' 16 times. Other basic movement commands are more useful, such as 'w', 'e', 'b', etc.

As one progresses through VIM movement commands, you come across things like 'G' and 'gg', '$' and '^'. There are other keys that do the same thing, allowing you to jump lines and move between the front and end of a line. These commands are enough for proficient and relatively quick movement around a file.

Adding in the search commands '/' and '?' is even better. At this point a user can move around a file with good speed and efficiency. But there are even more ways to move around VIM in a quick and efficient manner.

I originally used VIM because it was all that was available. I was doing coding and administration on remote systems. Using SSH and VIM was one of my only options for getting things done. So I slowly started to learn all the above commands.

It was when I learned about and began using the following commands, my VIM efficiency really took off. It was then that I realized just how much faster I can move around a file and find the place in the file that I need to get to. It allowed be to type text, edit files and write code much faster and more efficiently. As with any VIM command, once I got used to using each of the following I was able to do it without thinking. It became second nature and I became a full blown VIM convert.

set relativenumber

This is a newer feature of VIM. It came out around version 7.3.787. What this does is change the line numbers to be relative to the line you are currently on, rather than the absolute line number of the file. This is great for knowing that you need to skip 5 lines forward.

I realized that every time I had to think of how many lines or characters I had to move in VIM, I slowed down. My mind went from a sort of ingrained muscle memory for moving around to actually having to stop and thing what VIM command to type. Setting relative numbers helps with this. Now VIM tells me exactly where 5 lines ahead is.

What I do is set relative numbers and then also set number

set relativenumber set number

Doing this, VIM shows the absolute line number for the line I am on, and all the other lines are shown relative to my current line. It allows me to know where I am in the whole document, while allowing me to quickly figure out how many lines I need to move to get to one of the nearby lines.

cib, cas, cap

'i' is normally used to change into 'insert' mode, allowing you to directly type into the file. however, combined with 'c' or 'd' or other editing commands, 'i' is used to mean 'inside'. So 'ci(' or 'cib' will select everything inside of the current parenthesis. 'b' in this instance stands for bracket. This is a great command for editing code. Code has some weird white spaces that can be annoying to navigate using standard VIM commands, so being able to select everything inside of a set of brackets is very powerful.

'a' is also normally used to go into insert mode after the current character. Combined with 'c', 'ca' means 'change all'. And then combined with 's' or 'p' it comes to mean 'change all sentence' or 'change all paragraph,' respectively. This does what it sounds like. It selects everything in the current sentence or paragraph and removes it and goes into insert mode. Again, if you are not changing, just deleting you can always use 'das' or 'dap'.

Instead of moving to the begging of a word to edit the whole word you can type 'ciw'.

VIM's true power shines when you learn to combine different commands. VIM commands are similar to a programing language. Try to think of commands as a way of communicating with VIM.

}, *, /REGEX

'}' moves you to the next blank line. Like most commands, this can be combined with others, such as 'd}'. '{' moves you to the previous blank line. For some reason it took me a very long time to find this command. If you organize your code into groups of lines, this can be very useful for skipping to the next group of code.

'' is another one that took me far too long to discover. '' simply searches for the next word that is currently selected. To search backwards type '#'. To match anything containing the current word type 'g*'. This would match 'your' if the current word is 'you'.

To search for a word type '/' and then the word. But this can also take a regular expression, allowing you a very powerful search. You can then use 'n' or 'N' to continue searching forwards and backwards. '?' is the same thing but starts searching backwards instead of forwards.

VIM has a lot of very powerful commands. The above movement commands helped me become much more efficient at moving around VIM. I am sure there are even more commands that I am not aware of. Every once in a while I will challenge myself to adopt a new VIM command. I then purposefully attempt to think of how I am editing a file and try to remember to use the new command whenever possible. Practice is the best way to install the muscle memory which allows you to use VIM efficiently. '})'`