1. Introduction

This tutorial has been written for both vi and vim. It starts with real basics, such as cursor navigation and ends with more advanced techniques like merging files.

For every section of this tutorial there is a short video with hints to help you understand how vim / vi works. Even that I have divided this tutorial into parts from novice to the expert user, there is plenty more what vim can do to make your work with vim editor easier and more efficient.

However completing this tutorial you will give sufficient knowledge about vim / vi and its features for your daily tasks.

2. Vim Novice level ( Vim Basics )

2.1. Moving cursor around

 move vim text editor cursor left with h, move vim text editor cursor right with l, move vim text editor cursor up with k ,move vim text editor  cursor down with j

In vim you can move cursor around with following keys h, l, k, j which is left, right, up and down respectively.

You can move cursor around also with arrow keys, however this is possible only if they are available.

Vim was designed for all kinds of terminals where arrow keys may not be available for you. Moreover, once you get used to using vim with h, l, k, j you will move more quickly than using arrow keys.

Open some text file and try use above keys now:

vim yourfile.txt

NOTE: You do not have to create a file prior to execution of the above command. If the file does not exists it will be created. If the file does exists it will be opened.

Vim Novice level summary:
At this point of this vim tutorial you have learned:

Moving around with cursor:
h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN

2.2. Exiting from vim

At the moment, you know how to open a file with vim and how to navigate vim cursor around. What you now need to know is how to exit from vim editor without saving changes. Vim editor has two modes:

  • command mode
  • editing mode

Command mode is the one where we can instruct vim editor to exit to the command line ( shell ). To do that we need press ESC and type :q!. Play video and watch bottom of the video screen for entered commands.

Vim Novice level summary:
At this point, of this vim tutorial you have learned:
  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.

2.3. Character Deletion

In this section you will learn how to delete characters in a vim command mode. To delete character in vim, you need to position cursor on the character you are intent to delete and press x. Remember you need to be in a command mode! If unsure press ESC key.

Vim Novice level summary:
At this point of this vim tutorial you have learned:

  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete character with x key

2.4. Inserting Text

Well, you have already learned couple things about vim! Now its time to insert some text. In order to insert some text you need to switch vim into an editing mode. To get into editing mode press, i when in a vim command mode. -- INSERT -- key word at the bottom will indicate that you are in an inserting mode. See video for demonstration. When you become familiar with i ( insert mode ) use instead i letter a ( append ) and see what it does !


Vim Novice level summary:
At this point, of this vim tutorial you have learned:
  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete with x key
  • Inserting / appending text:
    Press i or a in command mode and type

2.5. Saving edited file

Now you know, have to move cursor, delete characters insert text and exit file without changes with :q! vim command. Exiting vim editor without changes does not always produce desired outcome. To save changes for file you are editing use :wq command in vim command mode. There is also quicker way to do it, which is by key strokes SHIFT+zz . Watch video for demonstration of both :wq and SHIFT+zz commands. Remember you need to be in vim's command mode to execute those commands !


Vim Novice level summary:
At this point, of this vim tutorial you have learned:
  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete with x key
  • Inserting / appending text:
    Press i or a in command mode and type
  • Saving changes and exit:
    in command mode :wq or SHIFT+zz

2.6. VIM novice level Summary

  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete with x key
  • Inserting / appending text:
    Press i or a in command mode and type
  • Saving changes and exit:
    in command mode :wq or SHIFT+zz

3. Vim Operators and Motions

3.1. Deleting Words

Let's see if we can also make vim delete words instead of deleting characters one by one! Vim has for deleting words very intuitive command dw which means I guess Delete Word. Let's tryto delete some words. In order to do so navigate cursor at the beginning of the word you want to delete and enter command dw in a command mode.


Vim Operators and Motions summary:
At this point of this vim tutorial you have learned:
  • Deleting words:
    delete word with dw command

3.2. Delete to the end of the line

In fact, the vim editor in the previous example did delete all characters from your cursor to the first space. That is the reason that you need to navigate to the beginning of the word you want to delete. With command d$ you can instruct vim to delete all characters to the end of the line. Place you cursor anywhere in the text and enter d$ command. Remember you need to bein a vim command mode.

From now onwards we will refer to d as an operator and to w or $ as a motion. There are many other operators and motions, for example you may try what d operator with e motion does.


Vim Operators and Motions summary:
At this point of this vim tutorial you have learned:
  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion

3.3. Motions and count number

When in a command mode and entering motion key e your cursor will be placed to the end of the word. When entering motion key w your cursor will be placed at the beginning of the following word. To do some magic trick you can use count number before motion key, which will multiply your action. For example, to jump to the end of the 8th word you will enter count 8 and motion e thus: 8e . Try it and also try to play with w motion and count number. If you want to navigate to the end of the line it would be silly to count words and then enter count and motion key-strokes. To navigate to the end of the line use $ motion and to navigate vim to the beginning of the line enter 0 motion.

Vim Operators and Motions summary:
At this point, of this vim tutorial you have learned:

  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w

3.4. Deleting multiple words

No you have seen how to jump over multiple words by using vim's motions with combination of counts. Let's combine this acquired knowledge with operator like d ( delete ). For example, to delete 4 words we can use command d4w. It is very easy to remember this just take first characters from words "Delete 4 Words" and you get your command d4w.

Vim Operators and Motions summary:
At this point, of this vim tutorial you have learned:

  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w

3.5. Deleting lines

Vim is also capable to delete lines. To delete single line you can use dd command. If your intention is to delete multiple lines add count number in front of the actual command. For example, to delete 100 you can use command 100dd. Remember that the first line which will be deleted, is the one with your cursor on it.!

Vim Operators and Motions summary:
At this point, of this vim tutorial you have learned:

  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w
  • Deleting lines:
    to delete single line dd, delete n lines ndd

3.6. Vim undo command

You have already learned how to delete characters, words as well as whole lines. With all this processing power you are very likely to make a mistake. Let's say you wanted to delete just 2 lines not 4. Simple help comes in form of vim's undo command u. Once you have made a mistake go to command mode and press u to undo your previous changes.

Vim Operators and Motions summary:
At this point of this vim tutorial you have learned:

  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w
  • Deleting lines:
    to delete single line dd, delete n lines ndd
  • Undo changes:
    undo changes with u

3.7. VIM Operators and Motions summary

  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w
  • Deleting lines:
    to delete single line dd, delete n lines ndd
  • Undo changes:
    undo changes with u

4. Vim apprentice user level

4.1. Paste command

Congratulations, you have reached apprentice user level in using vim text editor! Let's continue this vim tutorial with paste command. When deleting lines, words or characters with d command, vim actually saves them into cache memory for later use. Having said that, you can also think of d command as cut. To use your cache memory in this concept you can use p ( paste/put)command. Try delete line with dd command, move you cursor where you intent to insert this and hit key p to paste it.

Vim apprentice user summary:
At this point of this vim tutorial you have learned:

  • Paste command:
    paste your cache memory with p command

4.2. VIM replace operator

Replacing characters with vim editor is a very easy task. Simply get vim to the editing mode, remove character and insert replacement character. Well, this approach is logical but, there is a way to do it faster with r ( replace ) operator. Move you cursor on the character you would like to replace first enter r command followed by character which should be there instead.rt command will replace current character with t.

Vim apprentice user summary:
At this point of this vim tutorial you have learned:

  • Paste command:
    paste your cache memory with p command
  • Replace characters:
    rt replace current character with t

4.3. VIM change operator

Vim's replace operator allows us to replace single o multiple characters with a single character type. To multiple characters you can use a change operator. If you would like to change a single word you can use command ce.

Vim apprentice user summary:
At this point of this vim tutorial you have learned:

  • Paste command:
    paste your cache memory with p command
  • Replace characters:
    rt replace current character with t
  • Change characters:
    ce to change single word, c$ to change to the end of the line

4.4. VIM apprentice user summary

  • Paste command:
    paste your cache memory with p command
  • Replace characters:
    rt replace current character with t
  • Change characters:
    ce to change single word, c$ to change to the end of the line

5. Vim Experienced user level

5.1. VIM advanced navigation

In many cases you will deal with large files which can contain thousands of lines. To navigate vim cursor to line 3500 with j key will take you whole morning including your lunch break. Fortunately vim developers equipped vim with g an operator. This is how you use it. To get to the end of the file hit G ( SHIFT+g ), to get to the beginning of the file use gg command, to navigate you cursor on n line use nG. If you are unsure how many lines your file has, you can use a CTRL+g key combination.


Vim experienced user summary:
At this point of this vim tutorial you have learned:

  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g

5.2. Searching text with vim

Navigating with vim's g operator is easy, but what if you do not know what line you need to edit? In this case vim allows you to search for a given keyword or phrase. You can use / or ? characters to search for keywords or phrases. /edit will search for a key word edit in forward direction. In case you use ?edit vim will search for key word edit in backward direction. You text may include more keywords, to search all of them keep pressing n key ( next ) or for backward search use N ( SHIFT+n )key.

Vim experienced user summary:
At this point, of this vim tutorial you have learned:

  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g
  • Search text with vim:
    search forward /, search backward ?, next search n , previous search N

5.3. Vim Substitution

Vim can replace given a pattern keyword throughout whole text. Vim also gives you an option to replace your pattern within certain range. This process is called substitution. To replacefirst occurrence of keyword bash with perl in your document you can use command :s/bash/perl/. If there is a more then one keyword of bash on the same line you can use command :s/bash/perl/g. This command can be used to replace keyword bash with perl in whole document: :%s/bash/perl/g . There may be a situation where you need to substitute keyword in certain line range only. In this case you replace % with line range. :20,40s/bash/perl/g this will substitute bash for perl only between lines 20 and 40.

Vim experienced user summary:
At this point, of this vim tutorial you have learned:

  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g
  • Search text with vim:
    search forward /, search backward ?, next search n , previous search N
  • Substitution :
    first occurrence single line :s/bash/perl/
    all occurrences single line :s/bash/perl/g
    first occurrence between line range: :23,100s/bash/perl/
    all occurrences between line range: :23,100s/bash/perl/g
    first occurrence in whole text: :%s/bash/perl/
    all occurrences whole text: :%s/bash/perl/g all occurrences whole text plus confirmation: :%s/bash/perl/gc

5.4. VIM experienced user summary

  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g
  • Search text with vim:
    search forward /, search backward ?, next search n , previous search N
  • Vim Substitution :
    first occurrence single line :s/bash/perl/
    all occurrences single line :s/bash/perl/g
    first occurrence between line range: :23,100s/bash/perl/
    all occurrences between line range: :23,100s/bash/perl/g
    first occurrence in whole text: :%s/bash/perl/
    all occurrences whole text: :%s/bash/perl/g

6. Vim Veteran user level

6.1. Execute external commands on shell

Vim editor also allows you to execute external commands while editing files. : followed by the ! allows you to do that trick. :!date will execute date command on the shell.

Vim Veteran user summary:
At this point, of this vim tutorial you have learned:

  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell

6.2. More options to write a file

For some reason, you may need to save your current progress on editing file to some different file, perhaps as a backup, or you just want to save current progress without quitting a vimeditor. :w command is used to save file without quitting vim. If you would like to save your current progress to some different file, for example, temp.txt you will do it with command :w temp.txt.

Vim Veteran user summary:
At this point of, this vim tutorial you have learned:

  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced:
    :w saves current file without quit, :w bash.sh writes to file bash.sh

6.3. Save selected text to the file

Let's say that you are in the situation where you have no mouse and no other windows available, and you need to selected text from currently opened file and save it to another file. This situation often happens with remote ssh / telnet logins. In this case you would use a v operator and highlight text with navigation keys, followed by :w to save selected text to different file.

Vim Veteran user summary:
At this point, of this vim tutorial you have learned:

  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced::w saves current file without quit, :w bash.sh whites to file bash.sh
  • Highlight text and save to different file:highlight text with v operator and save ti with :w <yourfile>

6.4. Retrieve content from file

Vim also allows you to append / merge content of one file into your currently opened file. For this you can use a r operator. :r file.txt will retrieve content of the file.txt and insert it to your currently opened file. The place where the text will be inserted depends on you cursor position.

Vim Veteran user summary:
At this point, of this vim tutorial you have learned:

  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced::w saves current file without quit, :w bash.sh whites to file bash.sh
  • Highlight text and save to different file:highlight text with v operator and save it with :w <yourfile>
  • Retrieve text from different file::r <yourfile> will retrieve content of other file

6.5. VIM veteran user summary

  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced::w saves current file without quit, :w bash.sh whites to file bash.sh
  • Highlight text and save to different file:highlight text with v operator and save it with :w <yourfile>
  • Retrieve text from different file::r <yourfile> will retrieve content of <yourfile> file

7. Vim Expert user level

7.1. Using open operator

Switching vim into an editing mode with i (insert) or a (append) operator is easy. However, sometimes you may find it easier to use an o operator. o operator inserts new line below yourcursor and switch vim into the editing mode. O (SHIFT+o) operator inserts new line above your cursor and switch vim into editing mode.

Vim Veteran user summary:
At this point, of this vim tutorial you have learned:

  • Using o operator:
    :o insert line bellow you cursor, O inserts line above your cursor

7.2. Copy and Paste

Previously, we have seen how we can use d ( delete ) operator to delete line and paste it to another location within file. This time we will do the same but instead of deleting line, wewill copy it and paste it. The principle is the same as with a d operator but in this case we will use y ( yank ) operator.

Vim Veteran user summary:
At this point, of this vim tutorial you have learned:

  • Using o operator:
    :o insert line bellow you cursor, O inserts line above your cursor
  • Copy and paste:
    yank line with y and paste it with p

7.3. Customize vim's environment

Vim is extended version of its older brother vi and this part of the tutorial applies only to vim. Every time you start up vim it reads a .vimrc file from you home directory. In .vimrc file you can set up a background, font color and many other different settings which are beyond this tutorial.


Vim Veteran user summary:
At this point, of this vim tutorial you have learned:

  • Using o operator:
    :o insert line bellow you cursor, O inserts line above your cursor
  • Copy and paste:
    yank line with y and paste it with p
  • Customize vim's environment:
    edit ~/.vimrc file to customize vim's environment


Congratulations !
You are now expert in using vim text editor and your expert level feedback on this vim tutorial is much appreciated: lubos(at)linuxconfig.org

8. Vim Tutorial Summary

8.1. VIM novice level Summary

  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete with x key
  • Inserting / appending text:
    Press i or a in command mode and type
  • Saving changes and exit:
    in command mode :wq or SHIFT+zz

8.2. VIM Operators and Motions summary

  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w
  • Deleting lines:
    to delete single line dd, delete n lines ndd
  • Undo changes:
    undo changes with u

8.3. VIM apprentice user summary

  • Paste command:
    paste your cache memory with p command
  • Replace characters:
    rt replace current character with t
  • Change characters:
    ce to change single word, c$ to change to the end of the line

8.4. VIM experienced user summary

  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g
  • Search text with vim:
    search forward /, search backward ?, next search n , previous search N
  • Vim Substitution :
    first occurrence single line :s/bash/perl/
    all occurrences single line :s/bash/perl/g
    first occurrence between line range: :23,100s/bash/perl/
    all occurrences between line range: :23,100s/bash/perl/g
    first occurrence in whole text: :%s/bash/perl/
    all occurrences whole text: :%s/bash/perl/g

8.5. VIM veteran user summary

  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced::w saves current file without quit, :w bash.sh whites to file bash.sh
  • Highlight text and save to different file:highlight text with v operator and save it with :w <yourfile>
  • Retrieve text from different file::r <yourfile> will retrieve content of <yourfile> file

8.6. VIM expert user summary

  • Using o operator:
    :o insert line bellow you cursor, O inserts line above your cursor
  • Copy and paste:
    yank line with y and paste it with p
  • Customize vim's environment:
    edit ~/.vimrc file to customize vim's environment

Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top