Basic text editing with Vim

Vim is a powerful text editor that can be operated without your hands ever leaving the keyboard. If you master the basics, you will hardly ever need a mouse or other pointing device again and still be faster.

Vim is much more than a text editor, Vim is a

and you will find it on almost every UNIX-based system.

There are numerous online resources, tutorials and cheat-sheets available. Here are some examples:


Cheat sheet and tutorial



Vim Cheat Sheet


Getting started

The most important things to master in your text editor are

Create a new file

When you start Vim without specifying a file to open, it shows a welcome text that disappears as soon as you add some text. But... how do you add text? The first experience that most users have with Vim is a feeling of being totally lost because

"it doesn't do anything and I can't get out!"

Be patient and read on...

Add text

Vim has several modes: Remember: you can always get back to command mode by pressing ESC.

So, let's add some text.

Start Vim and press a.
Then write something like "My first vim line" followed by ESC.

Copy text

To copy the line of text we just created type y y p

... and do that twice more...

To copy those 4 lines, go back to line 1 and type 4 Y p (note the capital Y)

... or UNDO with u and put them BEFORE the cursor ... 4 Y P (note the capital P)

Save your work

Save your work and do it often. There is no technical reason to delay saving, so make a habit of saving your text every time you made some changes or additions, even if that is several times per minute!

:w
To save your file, enter 'Command-line mode' from command mode by pressing : followed by w (this looks like :w) and then ENTER. Congratulations, you have created your first text-file with Vim.

Undo/redo changes

If you want to undo something use u. Vim has a multi-level undo so you can revert a huge number of changes.

For re-doing an action, use CTRL r

Exit, with or without saving

:q
You can now continue editing. When you are ready and want to leave Vim, do as follows: Enter 'Command-line mode' from command mode by pressing : followed by q and then ENTER.

:wq
If you want to combine saving your file and leaving Vim, do as follows: Enter 'Command-line mode' from command mode by pressing : followed by w followed by q and then ENTER.

Move around

Moving around while keeping your hands on the keyboard is fast and easy. You can use the arrow keys, but alternatively and much faster are these:

k - UP
j - DOWN
h - LEFT
l - RIGHT
w - move one word forwards
b - move one word backwards
0 - to the start of a line
$ - to the end of a line

Of course Vim needs to be in command mode, otherwise you end up with a piece of text looking like "iiijjllj kkjlkkjjkkj wwjjlkj bb"

Now let's make some bigger moves. Type some more text, with line breaks and empty lines or copy-paste some text from another document.


G - to the end of the file
1G - to the start of the file
5G - to line 5
{ - UP a block (separating lines need to be really empty!)
} - DOWN a block (separating lines need to be really empty!)
'' - back to where you came from

N.B.: blocks are separated by empty lines. They should be really empty, not even containing a SPACE or TAB!

Remove text

Sometimes you want to delete one letter, sometimes an entire word or line or even an entire section. Vim uses a buffer to store the last thing you deleted, so you can paste p it somewhere else. When using the edit keys this makes it possible to do really powerful tricks like changing the order of letters, words, lines and blocks with ease.

x - delete one letter
dd - delete one line
3 dd - delete 3 lines
dw - delete next word
3 dw - delete next 3 words
d} - delete next block
d$ - delete to end of line
dG - delete to end of file

Load an existing file for editing

In general, you can open an existing file from the terminal command-line by typing something like
vim myfile
or if you want to use the graphic version of Vim, use one of
gvim myfile
mvim myfile

Search

/ brings you to the command-line mode where you can type the text you want to find.

n - go to the next occurrence
N - go to the previous occurrence