In this post, I’m going to cover some basic functionalities of vim which will help you to get started. Like LaTeX, which alleviates the author’s burden on formating, vim will help you to focus on your programming by providing a wide range of shortcuts that improve your editting efficiency.
Command and editing modes
Before we start, it’s important to note that vim is not like your other text editors. It has two modes: insert (editing) and command mode. You type in the material you want in insertion mode, and you use commands to change your content “systematically” in the editing mode. We are going to cover more of this later.
Replacement for the arrow keys
In vim, you dont move around by the arrow keys or the mouse. They are too far away from the center of your editing panel, the a-z’s. Also, because of the two editing mode, we’ve doubled the function of each key. Therefore,
h left, h down, k up, l right
are used for moving around in the command mode. Once you get used to it, you’ll find it intuitive and much more convenient than the arrow key.
Cursor movements: what the arrow keys can’t do
Beyond what’s simply moving up and down, left to right, there are a lot more cursor movements you’ll be able to do in vim.
wW beginning of next word
eE end of next word
bB go back to the beginning of the previous word
The difference between to lower case and upper case is, the lower cases treat symbols as words while the upper case doesnt.
0$ beginning/end of the line
+- first character of next/previous line
1-9G go to line number, line number followed by G
Now you know how to move around, you’ll need the insert mode to get your content going. To switch to insert mode, you’ll need to press certain keys in command mode. These key will move your cursor and switch your mode.
i insert will let you insert text at the current cursor position
a append will let you insert text after the current cursor position (moves it right by 1 place)
x removes the character unber cursor (cross out)
r replace the character under the cursor by another character. It will exit insert mode as soon as you’ve finished the replacement.
~ Toggle case
v Visual mode for moving the cursor and selecting text
oO open new line below/above
S delete line and Substitute text
I Insert at beginning of line
A Append text at end of line
R Rplace mode, overstrike existing characters with new text
J Join current and next line
V Select entire line
y Yank (copy)
c change word
Combining numbers with commands
You can often combine numbers, with commands, and then even with commands again. Here, we’ll just provide some examples.
5 i hello world escwill insert hello world 5 times once you press esc to exit from the insert mode.
3 kwill move your cursor up 3 lines.
2 d wwill delete the next two words.
d $wll delete from the cursor to the end of the line.
In short, the first number is the number of repetitions you want vim to perform on the following command. Then you can combine commands together.
If you don’t know, Ex is a direct predecessor of vim. You can use Ex commands in the command mode by typing :.
:set nuturns on line numbers. :set nonu turns it off.
:set tw=79automatically wraps your line before the 79th character
:wqsave and quit
- Arnold Robbins, Linda Lamb, and Elbert Hannah. 2008. Learning the Vi and Vim Editors (Seventh ed.). O’Reilly.