VIM tutorial for beginners

The Vim editor (and its predecessor, vi) is a command line editor for Linux systems. It has a long standing reputation as being the most powerful text editor on Linux. After mastering it, many users will even claim that it is far speedier and more convenient to use than a typical GUI editor packed with lots of features in its menus.

Vim has a staggering number of features and functions, but they are all hidden behind keyboard shortcuts and commands. There are no menus or hints in Vim, as users are expected to have enough of a grasp to navigate around on their own. And this is where Vim’s learning curve turns off most new users from bothering with it. Many novices would rather stick with something simple, like nano, than deal with all Vim’s depth.

While it is tempting to stick with a simpler editor, we can tell you that learning your way around Vim is well worth the effort. In this tutorial, we will introduce you to using Vim by starting with the bare basics. You will find that it is not so hard at all, as long as you can remember some of the most common commands. As a Linux user, you are probably already used to memorizing a few commands and keyboard shortcuts.

For every section of this tutorial there is a short video with hints to help you understand how Vim / vi works. We have also divided this tutorial into parts from novice to the expert user. There is still plenty more that Vim can do to make your work with text based files easier and more efficient. However, completing this tutorial you will give sufficient knowledge about Vim / vi and its features for your daily tasks.

In this tutorial you will learn:

  • How to navigate / move cursor around a file
  • How to exit from Vim
  • How to delete a character, word, or line
  • How to insert or append text in Vim
  • How to save changes to a file
  • How to use motions and count numbers in Vim
  • How to undo changes in Vim
  • How to yank (copy) and paste words or lines
  • How to replace characters or words
  • How to use Vim change operator
  • How to navigate to the beginning or end of a file
  • How to search for text in Vim
  • How to use Vim substitution
  • How to execute external commands on shell
  • How to write changes to a different file
  • How to save selected text to a file
  • How to retrieve content from a file
  • How to customize Vim’s environment
Vim Tutorial
Vim Tutorial
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Vim / vi
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Vim Novice level ( Vim Basics )



Moving cursor around

Vim navigation diagram
Vim navigation diagram

In Vim, you can move the cursor around with the h, l, k, and j keys, which are left, right, up and down respectively. You can move cursor around also with arrow keys, however this is possible only if they are available.

  • h or left arrow key = move cursor left
  • l or right arrow key = move cursor right
  • k or up arrow key = move cursor up
  • j or down arrow key = move cursor down
DID YOU KNOW?
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, and j keys, you will move more quickly than using arrow keys.

Open some text file and try use above keys now:

$ vim yourfile.txt

Note that you do not have to create a file prior to execution of the above command. If the file does not exist, it will be created. If the file does exist, it will be opened.

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!.

  • The : character prefaces our command
  • The q will quit out of Vim
  • The ! character instructs Vim to not save changes

Play the video below and watch the bottom of the video screen for entered commands.

Character Deletion

In this section, you will learn how to delete characters in the Vim command mode. To delete a character in Vim, you need to position your cursor on the character you are intent to delete and press x. Remember you need to be in the command mode! If unsure, press ESC key and you will enter command mode. If you are already in command mode, then pressing ESC again will not hurt.

Inserting Text




You have already learned the basics of getting around and closing Vim! Now it is time to insert some text. In order to insert some text, you need to switch Vim into the editing mode. To get into editing mode press i while currently in the command mode. — INSERT — keyword will appear at the bottom left of the screen and indicates that you are in insert mode. See video for demonstration. When you become familiar with i ( insert mode ) try also using letter a ( append ) and see what it does!

Saving edited file

We have already seen how to exit a file without saving changes by using the :q! Vim command. To save changes for a 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.

  • The w command will write (save) changes
  • The q will quit out of Vim
  • The SHIFT + zz shortcut does the same thing

Watch the video for demonstration of both :wq and SHIFT+zz commands. Remember you need to be in Vim’s command mode to execute those commands (press ESC if unsure)!

VIM novice level Summary

Congratulations, you have passed the novice level of our Vim tutorial. Here is what you have learned so far:

  • Moving cursor around: h = left, l = right, k = up, j = down
  • Exit without saving: Press ESC for command mode, then enter :q! to exit
  • Deleting characters: Highlight a character and press x in command mode
  • Insert or append text: Press i or a in command mode and start typing
  • Exit and save changes: :wq or SHIFT+zz in command mode

Vim Operators and Motions

Deleting Words

Let’s see if we can also make Vim delete words instead of deleting characters one by one! Vim has a very intuitive dw command for deleting words, which stands for “Delete Word”. Let’s try to delete some words. In order to do so, navigate your cursor to the beginning of the word you want to delete and enter command dw while in command mode.

Delete to the end of the line

In the previous example, you saw how to 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. We can also delete to the end of an entire line by using the d$ command. Place you cursor anywhere in the text and enter d$ command. Remember you need to bein a vim command mode.

NOTE
The $ dollar sign is often used in Vim to indicate “to the end of” something. In this example, it is to delete to the end of a line, but in some other commands it can also indicate “to the end of the document.”

From now on, 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 want to try the d operator with e motion and see what it does.



Motions and count number

When in the 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 a magic trick, you can use a count number before using a motion key, which will multiply your action.

For example, to jump to the end of the 8th word, you would enter count 8 and motion e, thus: 8e. Try it, and also try to play with the 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 keystrokes. To navigate to the end of the line, use $ motion and to navigate Vim to the beginning of the line, enter 0 motion.

Deleting multiple words

Now 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 an operator like d ( delete ). For example, to delete 4 words we can use command d4w. It is very easy to remember this by remembering the command as an abbreviation that stands for “Delete 4 Words” and you get your command d4w.

Deleting lines

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

Vim undo command

You have already learned how to delete characters, words, and whole lines. With all this processing power, you are very likely to make a mistake at some point. Let’s say you wanted to delete just 2 lines instead of 4. Simple help comes in form of Vim’s undo command, u. Once you have made a mistake, go to command mode (with ESC) and press u to undo your previous changes.

VIM Operators and Motions summary

Congratulations, you have passed the operators and motions section of our Vim tutorial. Here is what you have learned so far:

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


Vim apprentice user level

Paste command

Congratulations, you have reached the apprentice user level for using the Vim text editor! Let’s continue this Vim tutorial with the paste command. When deleting lines, words, or characters with the d command, Vim actually saves them into cache memory for later use.

Having said that, you can also think of the d command as “cut”. To use your cache memory in this concept, you can use the p ( paste/put ) command. Try deleting a line with the dd command, move you cursor where you intend to insert this deleted (or cut) line, and hit the key p to paste it.

VIM replace operator

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

VIM change operator

Vim’s replace operator allows us to replace single or multiple characters with a single character. To replace multiple characters, you can use a change operator. If you would like to change a single word, you can use command ce. To replace the rest of a line, use the c$ command, and begin typing your replacement character, word, or string.

VIM apprentice user summary

Congratulations, you have passed the apprentice level of our Vim tutorial. Here is what you have learned so far:

  • Paste command: Paste your cache memory with p command
  • Replace characters: r command will replace a character. Example: type rt to replace current character with “t”
  • Change characters: ce command will change a single word. c$ command will change to the end of the line

Vim Experienced user level

VIM advanced navigation

In many cases, you will deal with large files which can contain thousands of lines. To navigate the Vim cursor to line 3500 with the j key will take you a whole morning, including your lunch break. Fortunately, Vim developers have equipped Vim with the g operator. Here 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 your cursor on n line, use nG. If you are unsure how many lines your file has, you can use the CTRL+g key combination to see.




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 the key word “edit” in forward direction.

In case you use ?edit, Vim will search for the key word “edit” in backward direction. In case your text contains multiple instances of the keyword you are searching for, keep pressing n key ( next ) or for backward search, use N ( SHIFT+n ) key combo.

NOTE
Keep in mind that if you search with / then the n key will move forward to next result and N will move backwards to previous result. If searching with ?, the keys swap – n will go backwards and N will go forward.

Vim Substitution

Vim can replace a given a pattern keyword throughout the whole text. Vim also gives you an option to replace your pattern within a certain range of line numbers. This process is called substitution. To replace the first occurrence of keyword “bash” with “perl” in your document, you can use command :s/bash/perl/. If there is a more than 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 a keyword within a certain line range only. In this case, you replace % with the line range. The :20,40s/bash/perl/g command would substitute “bash” for “perl” only between lines 20 and 40.

VIM experienced user summary

Congratulations, you have passed the experienced level of our Vim tutorial. Here is what you have learned so far:

Advanced Navigation:

  • Go to end of the file with G command
  • Go to like n with Gn command
  • Display number of lines in file with Ctrl + g

Search text with Vim:

  • Search forward with /keyword
  • Seach backward with ?keyword
  • Press n to jump forward to next keyword
  • Press N to jump backward to previous keyword




Vim Substitution:

  • Replace first occurrence in a single line :s/bash/perl/
  • Replace all occurrences in a single line :s/bash/perl/g
  • Replace first occurrence between line range: :23,100s/bash/perl/
  • Replace all occurrences between line range: :23,100s/bash/perl/g
  • Replace first occurrence in the whole text: :%s/bash/perl/
  • Replace all occurrences in the whole text: :%s/bash/perl/g

Vim Veteran user level

Execute external commands on shell

Vim editor also allows you to execute external commands while editing files. : followed by an exclamation point ! allows you to do that trick. For example, :!date will execute the date command on the shell. See the video below to see what it looks like.

More options to write a file

There may be times where you need to save your current progress on a file you are editing to some different file, perhaps as a backup, or you just want to save current progress without quitting out of Vim editor. The :w command is used to save a 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.

Save selected text to the file

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

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 the 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 your current cursor position.

VIM veteran user summary

Congratulations, you have passed the veteran level of our Vim tutorial. Here is what you have learned so far:

Execute external commands on shell from Vim:

  • :!ls will execute ls command on your shell
  • :!date will execute date command on your shell

Writing to files advanced:

  • :w saves the current file without quitting
  • :w bash.sh saves the current contents to file bash.sh

Highlight text and save to different file:



  • Press v and then select text with nav keys
  • Then, save the highlited text to a file with :w file.txt

Retrieve text from different file:

  • :r file.txt command will retrieve content of file.txt and place it at the position of your cursor

Vim Expert user level

Using open operator

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

Copy and Paste

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

Customize Vim’s environment

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

Vim Tutorial Summary

In this tutorial, we saw how to use Vim on a Linux system. We have rounded up all the main points for you in a summary below. Be sure to check back whenever you need a quick refresher, as some of these commands can be difficult to remember unless you use them many times.

VIM novice level Summary

  • Moving cursor around: h = left, l = right, k = up, j = down
  • Exit without saving: Press ESC for command mode, then enter :q! to exit
  • Deleting characters: Highlight a character and press x in command mode
  • Insert or append text: Press i or a in command mode and start typing
  • Exit and save changes: :wq or SHIFT+zz in command mode

VIM Operators and Motions summary

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


VIM apprentice user summary

  • Paste command: Paste your cache memory with p command
  • Replace characters: r command will replace a character. Example: type rt to replace current character with “t”
  • Change characters: ce command will change a single word. c$ command will change to the end of the line

VIM experienced user summary

Advanced Navigation:

  • Go to end of the file with G command
  • Go to like n with Gn command
  • Display number of lines in file with Ctrl + g

Search text with Vim:

  • Search forward with /keyword
  • Seach backward with ?keyword
  • Press n to jump forward to next keyword
  • Press N to jump backward to previous keyword

Vim Substitution:

  • Replace first occurrence in a single line :s/bash/perl/
  • Replace all occurrences in a single line :s/bash/perl/g
  • Replace first occurrence between line range: :23,100s/bash/perl/
  • Replace all occurrences between line range: :23,100s/bash/perl/g
  • Replace first occurrence in the whole text: :%s/bash/perl/
  • Replace all occurrences in the whole text: :%s/bash/perl/g

VIM veteran user summary

Execute external commands on shell from Vim:

  • :!ls will execute ls command on your shell
  • :!date will execute date command on your shell

Writing to files advanced:

  • :w saves the current file without quitting
  • :w bash.sh saves the current contents to file bash.sh

Highlight text and save to different file:

  • Press v and then select text with nav keys
  • Then, save the highlited text to a file with :w file.txt

Retrieve text from different file:

  • :r file.txt command will retrieve content of file.txt and place it at the position of your cursor

VIM expert user summary

Using open operator:



  • Insert a new line below your cursor with o
  • Insert a new line above your cursor with O

Copy and paste:

  • Yank (copy) a line with y command
  • Paste the copied line with p command
  • To yank five lines, use command 5y

Customize vim’s environment:

  • Vim settings are stored in the ~/.vimrc file
  • The “syntax on” setting showed in our video will turn on syntax highlighting