Hi there Linux people! Today we are going to talk about the VIM editor. The VIM is a text-editor but not just a text-editor when you get to know it. The VIM editor is Based on the vi editor common to Unix-like systems. The VIM editor is a more feature rich extension of the VI editor. Best part is that the VIM editor is free. In other words the VIM editor is open source. Since the VIM editor is a cross platform application, It does not matter whether If you are a linux user, a mac user or even windows user you will find the VIM editor really helpful in your day to day work. I don’t want to beat around the bush let’s get to it(happy-face).

Installation

Most of the unix-like systems contain VIM editor. But unfortunately if you haven’t got it, use the below command to get the VIM editor. You might need sudo for the below command.

apt-get install vim vim-common vim-gnome vim-gui-common vim-runtime

Yeah it’s simple as that. Let’s move on to the basics.

The Basics of Vim

There are 3 modes in the VIM editor(Given that different people say different numbers but for me it’s 3)(tongue-out-face).

  1. Insert mode – Bottom of your screen INSERT text will be shown
  2. Command mode
  3. Visual mode – Bottom of your screen VISUAL LINE text will be shown

If you are not sure which mode you are in press esc key, then you will be sent back to command mode. Let’s take a look at the movement of the VIM. To execute below commands you should in the command mode.

  • h – move the cursor one character to the left
  • j – move the cursor down one line
  • k – move the cursor up one line
  • l – move the cursor one character to the right
  • 0 – move the cursor to the beginning of the line
  • ^ – move the cursor to the beginning of the line
  • $ – move the cursor to the end of the line
  • w – move forward one word
  • b – move backward one word
  • G – move to the end of the file
  • gg – move to the beginning of the file
  • } – move forward one paragraph
  • { – move backward one paragraph
  • % – find the matching bracket(in programming)

Okay let’s assume that you want to go to line 15 of the document you are in. Entering :15 in the command mode will take you to line 15 of the doc. To set line numbers in the document enter :set nu or :set number in the command mode. To disable line numbers enter :set nu! or :set number! in the command mode.

Basic Editing

When you are in the command mode to edit the document you will have to enter the insert mode. To do that press to enter the insert mode. Now whatever you type will be taken as text input. If you want to go back to command mode press esc key. Once you are done editing you can press :w write the changes to the file. then :q to quit the file. Happiness is you can enter the both commands at once :wq, it will write the changes and quit the file. Okay what if you want to quite without saving the file, you can enter :q! in the command mode.

  • x – delete the character that the cursor is on
  • X – delete the character behind the cursor
  • dw – delete the word that the cursor is on
  • cw – delete the word that the cursor is on and then go to insert mode
  • d0 – delete to the beginning of a line
  • d$ – delete to the end of a line
  • dd – delete the whole line
  • cc – delete the whole line and then go to insert mode
  • db – delete the word behind
  • dj – delete the current line and line below
  • dgg – delete to the beginning of the file
  • dG – delete to the end of the file
  • u – undo the last operation
  • Ctrl-r – redo the last undo
  • U – undo all
  • ct’ – delete everything until ‘
  • ci’ – delete everything inside ‘
  • ca’ – delete around the ‘

The vim editor takes every delete command as a cut command. So later in time you can paste what you have deleted.

  • p – paste text that the cursor is on
  • P – paste text on before the cursor
  • yw – yank(copy) the word that the cursor is on
  • yy – yank(copy) the current line that the cursor is on
  • y0 – yank(copy) everything behind the cursor
  • r – Replace the character that the cursor is on
  • R – Replace multiple characters

Okay now I’ll give you guys few little tricks. Let’s assume that you want to copy 10 lines. you can use 10yy, here 10 is called the repetition counter. You can delete 5 lines by 5dd, here 5 is called the repetition counter. You can apply the repetition counter to almost every editing command in the VIM editor.

Search & Replace

Search and replace is one of the most used features of a text editor. Let’s see how to accomplish that in the VIM editor.

  • /search-text – search for search-text in the document, going forward
  • n – move the cursor to the next instance of the search-text from the last search
  • N – move the cursor to the previos instance of the search-text from the last search
  • ?search-text – search for search-text in the document, going backwards
  • :%s/text/replacement-text/g – search through the entire document for text and replace it with replacement-text
  • :%s/text/replacement-text/gc – search through the entire document and confirm before replacing text

There are few flags that you could pass as your search and replace params.

  • g – global
  • c – confirm flag
  • i – case insensitive

In your search you can set the following options

  • :set incsearch – incremental search
  • :set ignorecase – no case sensitivity in search
  • :set hlsearch – hi-light the search term

Advanced Movement

  • control + d – go down half a screen
  • control + u – upwards half a screen
  • control + f – go screen down
  • control + b – go screen up
  • M – move the cursor to the middle of the window
  • H – move the cursor to the top of the window
  • L – move the cursor to the last line of the window
  • 3L – move the cursor 3 lines above from the last line of the window
  • 3H – move the cursor 3 lines from the top of the window
  • zt – set the line that the cursor is on to top
  • zb – set the line that the cursor is on to bottom
  • zz – set the line that the cursor is on to middle

Okay under advanced movement I’m going to teach you a little trick. In the VIM editor there is a feature called makers that allow you to mark positions in a file. When you set a maker in a file you can travel back to the particular maker position at a later time. Those markers are saved in a register in VIM. If you are curious about VIM register, read this and this one. Let me elaborate more. Assume that you have a text file with 1000 lines. You are going to set a marker in line 250. After setting a marker you go through the file again. Now you are at line number 750 and you want to go back to line 250 to change something. You easily travel using the marker. Advantage is you can set multiple markers in your file and travel as you need.

  • ma – set a marker in the current line that cursor is on and save the marker in register a
  • :’a – go back to the marker that is stored in the register a

Tips & Tricks

if you have been using vim for sometime you might have seen this error below which occurs when you don’t have enough permission to write to a file. I’ll show you a trick to write to the file.

'readonly' option is set (add ! to override)
E45: ‘readonly’ option is set (add ! to override)

Try the below command to save the file.

:w !sudo tee %

Let me explain

  • :w – write
  • !sudo – call shell sudo command
  • tee – the output of write (:w) command is redirected using tee
  • % – current file name

Once you enter the command you will see the below output. Presskey to reload the file. More info.

W12: Warning: File "my.txt" has changed and the buffer was changed in Vim as wel l
W12: Warning: File “my.txt” has changed and the buffer was changed in Vim as well

I hope now you must have a good idea about how to operate the VIM editor. There is a lot more to it. This will give you a jump start. If you have any questions let me know in the comments below. Your feedback is highly appreciated(happy-face).

 

Loading

5 Comments

  1. kasun Rathnayaka February 24, 2014 at 8:09 am

    good articular..If you can please add some example videos also ..

    Reply
    1. admin February 24, 2014 at 8:31 am

      Thanks for the idea :-)

      Reply
  2. ajith September 2, 2014 at 10:06 am

    good work mcn.

    Reply
  3. phuong trinh August 14, 2015 at 9:14 am

    Can I talk about my problem, please?
    When I use :w !sudo tee % then I enter my password and I recevied ” hduser is not in the sudoers file. This incident will be reported.

    shell returned 1

    Press ENTER or type command to continue

    I don’t know how to fix it.

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.