Basic Vim Commands Tutorial
Vim is what is known as a modal editor. While using it becomes very easy over time, learning it initially can be confusing and pretty irritating. This tutorial aims to give a basic run through of the most commonly used commands in Vim,
Opening A File And Inserting Text
The first step in vim is to use it to open a file or create a new one. To do so you just do the following at your terminal:
$ vim test.txt
This will open a file for you to write to. When you open vim you are immediately placed in command mode, so if you want to insert some new text you will need to enter Insert Mode. To enter insert mode you just hit the letter i. After this if you at any point want to return to command mode just hit the escape key. There are several more modes available to you but we’ll stick with the basics here.
After entering command mode again you can enter replace mode by hitting the R key. You can also insert text after the cursor (as i inserts it at the cursor) by hitting a (append), or open a new line by hitting o.
Navigating
The historical navigation keys in vim are h,l,j, and k. These are to move left, right, down and up respectively. Since Vi became Vim (Vi IMproved) however you can also just use the arrow keys. It is good to take note of these keys though as some default installs of Vim will actually be Vi (or at least use the old style navigation keys from Vi). Also, remember you need to be in command mode for navigation otherwise you will just be typing in the letters h,j,l and k!
You can move forward a word using w or back a work using b. You can use ^ to move to the start of a line and $ to move to the end of one. gg will bring you to the top of a document and G to the bottom of one.
Deleting Text
I say delete but that is not entirely true. When you use the delete commands below the deleted text is actually kept in a register (the equivalent of a clipboard in Windows). This means you can paste the deleted text somewhere else once you haven’t deleted anything else since. So delete is closer to a cut than an actual delete.
Anyway, you can “delete” the character under the cursor by hitting the x key. dw will delete the word the cursor is currently over, d$ will delete text from the cursor to the end of the line, d^ to the start of the line, and dd will delete the whole line. You can also use :d$ to delete to the end of the document.
In all the above you can also add in a numerical argument to change how many letters/words/lines etc you want to delete. i.e. d2w would delete two words, and d2d would delete two lines
Copying (Yanking) Text
You can yank a piece of text to copy it from one place to another. The syntax is exactly the same as delete, with all the same options. So for example yw would yank a word, y5w would yank 5 words, yy would yank a whole line and so on.
Pasting Text
I’m only going to cover the basic paste command here. While in command mode you can just hit the p key to paste. Of course you must have something to paste first, either through having deleted text or copied text.
Search and Replace
To search the document you just use the good old unix search method /. That is to say, enter command mode, hit the / key and type what you are looking for. For example, if you we’re looking for the word Dublin in a document you would hit Esc (for command mode), then type /Dublin and vim will find the text for you. If you use a ? instead of a / vim will search in reverse.
You can also replace text in Vim. In fact you can do so using regular expressions but I won’t cover that here as it is far too large an area. If you are interested though look up “Mastering Regular Expressions” written by Jeffrey E. F. Friedl, and printed by O’Reilly. Regular expressions are a great thing to know.
Anyway, to replace text use the following in command mode:
:%s/find/replace/g
The : occurs quite often in Vim and is used to issue commands not covered by key shortcuts. You then type in the %s at the prompt followed by a / the phrase you are looking for, another /, what you want to replace the phrase with, and a g. The g makes the replacement global. As an example of this I’ll use the line:
John went to the shop this afternoon.
Now we try a string replacement
:%s/shop/park/g
Which will change all occurrences of the word shop to park.
John went to the park this afternoon.
Saving And Exiting Vim
Now we come to saving and exiting vim. To save a document we just use the :w command (think of it as write) in command mode. To quit without saving we will just use the :q command. If we have unsaved changes this will prompt us to save them before quitting. We can save and quit at the same time using :wq, or save discarding changes using :q! which will not prompt us to save before quitting.
Further Commands
For further knowledge of Vim I would recommend using the Vim tutor. This is installed with Vims common files and can be started using the vimtutor command in your terminal, as below:
$ vimtutor
