vi/vim Basic Commands

As far as I know there are 10 types of programmers around: emacs programmers and vi programmers. Jokes aside I think both text editors are fine, but I prefer vi/vim.

The problem is that getting started with it is not trivial, as once you open it you’ll be presented with a blank screen and a blinking cursor, and nothing else!

Below you’ll find some basic commands to get you moving:

1. Vi Modes

Vi has two modes: command and input. When you start it you’ll be in command mode, which is used to save the file, move around the file, and quit. Input mode is used to actually edit the file. When you are in input mode a –INSERT– will appear at the bottom.

2. i and a

By pressing i (insert) or a (add) you’ll switch from command to input mode. the only difference is that i will start editing the left to the left of where the cursor is, and a will start editing to the right.

3. I and A

You can also switch to input mode by typing I or A. I will start editing at the beginning of the current line (regardless of where the cursor is), and A at the end of the current line.

4. o and O

Finally, you can use o and O (open) to start editing on new lines. o will open a new line below your current one, and O will open a new line above.

5. Moving around the file

The simplest way to move around the file is by using the arrow keys. If you prefer you can also use the h,j,k,l keys, which might be faster.

You can also use:

w – moves to beginning of next word
b – moves to beginning previous word
H – moves to top left of the screen
M – moves to the line in the middle of the screen
L – moves to the last line of the screen
Ctrl + F – moves to the next page on a large file
Ctrl + B – moves to the previous page on a large file

6. Deleting and Editing text

x – deletes the current character
dd – deletes the whole line
yy – copies the current line to the buffer
P – pastes whatever is in the buffer, in the current line
p – pastes as well, but in the line below the current one
u – undo the last change
Ctrl+R – redo what was undone

7. Searching

Use /pattern to search forward, and ?pattern to search backward. To keep searching type n (forward) or N (backward).

8. Saving and Exiting

ESC – switch back to command mode
:w – writes to the file (i.e., saves it)
:wq – writes and quites vi
:q! – quits without saving

Leave a Reply

Your email address will not be published. Required fields are marked *