What is Vim and how to use it? Vim basics

Esc + :q!

Well that’s an answer to a slight different question: How do I exit the Vim editor? However, while writing this article for a very popular website https://stackoverflow.com/ it has reached over 2 300 000 views!

Vim (Eng. vi improved) is a multiplatform clone of the vi editor. It was written by Bram Moolenaar. It’s an open source program, which for the first time was released in 1991.

Vim is often automatically installed, especially in Linux operating system. That’s why it’s really good to know a couple of commands, so that you can edit files (open logs’ files and exit) by yourself.

Before we get down to work with Vim, let’s have a closer look at working modes in this programme. For the user who works with Vim for the first time, some of the work modes may seem very unintuitive.

Basic work modes are:

  • Normal
  • Insert/replace text
  • Command-line

Text insertion mode

Text insertion mode is used to a standard text edition.

You can enter it from the normal mode by the use of the following commands:

i - switch to inser text mode after the cursor
a - switch to text inser text before the cursor
I - switch to text inser text mode at the beginning of the line 
A - switch to text inser text mode at the end of the line 

To exit to normal mode press Esc.

Normal mode

After launching, we’re in normal mode, where we can freely move around the document:

       ^
       k		      Hint:  h on the left
  < h     l >		             l on the right
       j			     j looks like down arrow
       v
Or the standard arrow keys

And switch to different modes:

: - switch to comand line mode

In normal mode it is possible to delete text:

x - delete the character under the cursor in normal mode
dw - delete the entire word under the cursor
de - delete to the end of the current word 
d$ - delete the line of text to the end of the line
dd - delete the entire line

In normal mode it is also possible to repeat commands, e.g.:

2dw - removing two words
2dd -removing two lines

Canceling the commands in normal mode:

u - withdrawal previous command 
U - withdrawal all commands in line

Repeating the commands:

. - repeat the last action performed
Crtl + r - redo the withdrawn action

Command insert, replace

p - inserts the last deletion after the cursor
e.g. when we execute the command dd we would go to another line and press p,
the deleted line appears behind the cursor
r* - replaces the character under the cursor * (instead of * you can put any character)

An example of command p – insert:

The tag is in line a):

We execute command dd – line deletion

At the beginning of text, we execute command insert p:

Command insertion mode

To enter command insertion mode you have to press : then, at the bottom of your screen you’ll see a line of commands:

All of the commands entered in the command mode need to be submitted by pressing Enter.

Basic commands while working with Vim:

:wq – write changes and quite Vim 
:cq! – quit Vim unchanged
:w - write changes
:w plik2.txt - write changes to file plik2.txt
:help w - command help, in this case (w - write) 
:help index - complete index of all Vim commands
:setlocal spell spelllang=pl - starts the spell check, in this case in Polish
:100 - going to line 100
:e plik1.txt - open file plik1.txt
:!<SYSCMD> -  running any system shell command, eg:! cmd runs cmd



Check out other articles in the technology bites category

Discover tips on how to make better use of technology in projects

Do you have any questions?