This article is part of the VIM User Guide series:
- The Beginner’s Guide to Start Using Vim
- Vim Keyboard Shortcuts Cheatsheet
- 5 Vim Tips and Tricks for Experienced Users
- 3 Useful VIM Editor Tips and Tricks for Advanced Users
Choosing a text editor is a very important decision for a programmer. This is partly because of the plethora of variables: graphical/non-graphical interfaces, different shortcuts, language specializations, plugins, customizations, etc. My advice is not to try to search for the best one. Instead, choose the one that corresponds best to your habits and your tasks. If you want to work in a group, it’s generally best to select the same editor as your co-worker. That way, if you have a problem, you will be able to find some help.
It is exactly for that reason that I started using Vim a few years ago. Traditionally, Vim is placed in conflict with the legendary Emacs. I confess that I know very little about Emacs, but what you have to know about these two text editors is that they can both be fully customized, and very confusing at first. This tutorial will not explain everything about Vim but will try to give you the basics to use it correctly in the first place, and then present a few tips that will (I hope) allow you to learn on your own.
Vim comes from “VI iMproved”. Vi is a non-graphical text editor widely distributed in Unix systems. It comes by default with Linux. Vim is an enhancement of this original editor. However, unlke Vi, Vim is not installed by default on every distribution.
To install Vim on Ubuntu, use the command:
sudo apt-get install vim
If you are already interested in some plugins, use the command:
sudo apt-cache search vim
This will give you a long list of packages related to Vim. Among them are some for various programming languages, addon managers, etc.
For this tutorial, I will be using the latest version of Vim (7.3.154) on Ubuntu. You can use any other version though.
Type the command
vim in a terminal. You should see a nice welcome screen.
And if you’ve never used Vi or Vim before, it is very likely that you don’t even know how to exit… Yes, it’s true. None of the shortcuts you normally use will work in Vim.
First of all, to use any menu-type function like save or exit, your command should begin with a colon (:). Saving is
:w and quitting is
:q. If you want to quit a file without saving, use the force quit command
:q!. A cool thing with Vim is that you don’t have to type commands separately. In other words, if you want to save and then quit, you can directly use
So for now, quit Vim and open it on a sample text file. Simply add the name of the text file that you want to edit after the command:
vim [text file name]
By default, when you open a text file, you are in visual mode. It is quite specific to Vim and confusing at the beginning. Vim is composed mainly of two modes: visual and editing. The visual mode is for viewing a text and using some commands. To go into editing mode, just press
i to insert and
a to add some text. To go back into the visual mode and access all the menu-type functions, press the “Escape” key. The difference between insertion and addition is simply whether you want the text you type to appear before or after the cursor in visual mode. To understand this fully, you should really try it yourself. My advice is: add at the end of lines, and insert in other cases.
To move the cursor within a text, whether you are in visual or editing mode, you can generally use the keyboard arrows. A real purist would tell you to use the keys h for left, j for down, k for up, and l for right.
Now that you are warmed up and know how to control Vim at a basic level, let’s go to the core.
A few basic commands
Now that you master the transformation from visual to editing mode, here are a few commands that you can use in visual mode:
- x: to delete a character
- u: to undo an action (the equivalent of Ctrl+z)
- dd: to delete a line
- dw: to delete a word
- yy: to copy a line
- yw: to copy a word
- p: to paste the previously deleted or copied line or word
- e : to move to the next word (faster than just moving with the arrow keys)
- r: to replace a letter (press r, then the new letter)
And of course, there are more, but this is enough for now. If you master all of them, you will already be very fluent with Vim.
As a side note for those who always want more, you can type a number before any of these commands and the command will be executed that number of times. For example, 5x will delete five characters in a row, while 3p will paste three times.
Finally, as a bonus and an appetizer for your own research, here are a few advanced and very useful commands:
- /searched_word : to search for a word within the text
- :sp name_of_a_text_file: will split the screen in half horizontally, showing the new text file in the other half. To shift the focus from the right to the left window, use the shortcut Ctrl+w
- :vsp name_of_a_text_file: same as before, but splits the screen vertically
- Ctrl+Shift+C and Ctrl+Shift+V: to copy and paste text in a terminal
- :! name_of_a_command: to launch a command external to Vim, directly into your shell. For example,
:! lswill display the files within the directory you are currently working in, without quitting the editor
I think you now have every tool you need to start using Vim. You can go even further by installing the various plugins, editing the .vimrc file, or even using the interactive tutor by typing the command vimtutor.
If you have any other commands that you would like to share about Vim, please let us know in the comments.
Image credit: Tonight’s side project