VIM - All you need to begin

Vim tutorial and cheatsheet

VIM - All you need to begin

Introduction

A range of simple editors, such as nano, are available for beginners. But, the real "Linux" users use VIM (don't cancel me, thanks).

Vim is the "modern descendant" of Vi and stands for Vi Improved. Vi was developed in the 1970s by Bill Joy, and Vim came into existence around 31 years ago. Despite its age, Vim remains a top choice of editor among many programmers and has a loyal following. It comes pre-installed on all Linux machines.

Vim is a powerful editor with many commands, too many to explain in a blog such as this. This blog is designed to describe enough of the commands that you will be able to use Vim as an all-purpose editor easily.

Who should read this blog?

  1. Programmers who want to begin using VIM.
  2. Programmers/Linux users can use this article for revision and as a cheat sheet.
  3. Myself

Why use VIM?

  1. Vim arrives pre-installed on any Unix or Linux box, from tiny IoT devices to supercomputers. The Single Unix Specification and POSIX require it.
  2. When you ssh into a machine at work, VIM will sometimes be amongst the only choices available for an editor (given that you are not allowed to install other packages).
  3. It can be intimidating initially but eventually boosts productivity.
  4. It is the holy grail for Linux users. You cannot head to an interview and say that you don't know how to exit VIM.

Setup

  1. Check the version of vim installed on your machine, type in the following command:

    vim --version
    
  2. Download the book "Treasure Island" as a text file from this link (or anywhere else). We will use this file to experiment with the commands, making it easier to follow along with this blog.

  3. Open up the file in Vim:

    vim treasure-island.txt
    

Note: You need to execute the commands to learn them properly. If you only read the text, you will forget the commands!

Let's Go:

  • There are two "modes" - with very different behaviours
  • Little or nothing on screen lets you know which mode you're currently in!

The two modes are "normal mode" and "insert mode", and as a beginner, remember: Press Esc twice or more to return to normal mode

The "normal mode" is used to input commands, and the "insert mode" for writing text - is similar to a regular text editor's default behaviour. There are other modes too, which we will look at later.

Basic Editing

  • Moving the cursor

    The cursor can be moved using the arrow keys. But the suggested way to go is to use the hjkl keys. image.png Hints:

    1. The h key is at the left and moves left.
    2. The l key is at the right and moves right.
    3. The j key looks like a down arrow.

    But why does Vim use the hjkl keys for navigation? You can read about it at this link.

    Moving word-by-word:

    • You can use w to move ahead by one word to the start of the next word.
    • Also, use e to move to end of the next word.
    • $ to move to the end of the current line.
    • To move to the start of the line use a zero: 0
  • Exiting Vim

    First make sure that you are in the "normal mode" by pressing <ESC> twice or more times (you might hear a beep sound).

    1. To trash all changes - Type :q! and press <ENTER>. This exits the editor, DISCARDING any changes you have made.
    2. To save the changes - Type :wq and press <ENTER>
  • Text Editing - Deletion

    If you exited Vim in the previous step, re-open the file by running vim treasure-island.txt.

    Move the cursor until it is on top of the character to be deleted and press the x key to delete the unwanted character.

  • Text Editing

    1. Insertion - Move the cursor on top of the character BEFORE which the text is to be inserted. Now, press i and type in the necessary additions.
    2. Appending - It does not matter on what character the cursor is in that line, press A (note: capital A) and type in the necessary additions. This will add take the cursor to the end of the line and shift to the insert mode.
      To insert or append text type:
      i   type inserted text   <ESC>   -   insert before the cursor
      A   type appended text   <ESC>   -   append after the line
      
      Type a (lowercase) to append text AFTER the cursor.
    3. Open Command - Type the lowercase letter o to open up a line BELOW the cursor and place you in Insert mode. Type O to open a line ABOVE the cursor.

Deletion Commands, Operators and Motions

  • Deletion Commands

    1. To delete from the cursor up to the next word type:       dw
    2. To delete from the cursor up to the end of the word type: de
    3. To delete from the cursor to the end of a line type:      d$
    4. To delete a whole line type:                              dd
    
  • Operations & Motions

    Many commands that change text are made from an operator and a motion. The format for a change command is:

    • operation - is what to do, such as d for delete
    • number - is an optional count to repeat the motion
    • motion - moves over the text to operate on, such as w (word), e (end of word), $ (end of the line), etc.

    A short list of motions:

    • w - until the start of the next word, EXCLUDING its first character.
    • e - to the end of the current word, INCLUDING the last character.
    • $ - to the end of the line, INCLUDING the last character.
    operation       number        motion
    
    Example:
    d2w - delete 2 words
    Here, d - operation
          2 - number
          w - motion
    
  • Undo and Redo commands

    • To undo previous actions, type: u (lowercase u)
    • To undo all the changes on a line, type: U (capital U)
    • To undo the undo's, type: CTRL-R

Replace, Put & Change Operations

  • The Put command (p)

    When you delete a line with dd, you can put back text that has just been deleted, by typing p. This puts the deleted text AFTER the cursor (if a line was deleted it will go on the line below the cursor). It can also be used to put(paste) copied text.

  • The Replace command (r)

    To replace the character under the cursor, type r and then the character you want to have there. For example, type rb to replace the character at the cursor with b .

  • The Change operator (c)

    The change operator allows you to change from the cursor to where the motion takes you. eg. Type ce to change from the cursor to the end of the word, c$ to change to the end of a line.

    Notice that ce deletes the word and places you in Insert mode.cc does the same for the whole line.

    The change operator works in the same way as delete. The format is:

           c    [number]   motion
    

    The motions are the same, such as w(word) and $(end of line).

More Navigation, Searching and Substitution

  • Navigation

    1. CTRL-G displays your location in the file and the file status. image.png

    2. G moves to the end of the file.

    3. number G moves to that line number.

    4. gg moves to the first line.

  • Searching

    1. Typing / followed by a phrase searches FORWARD for the phrase.

    2. Typing ? followed by a phrase searches BACKWARD for the phrase.

    3. After a search type n to find the next occurrence in the same direction or N to search in the opposite direction.

    4. CTRL-O takes you back to older positions,CTRL-I to newer positions.

  • Typing % while the cursor is on a (, ), [, ], {, or } goes to its match.

  • The Substitute Command (s)

    1. To substitute new for the first old in a line type :s/old/new
    2. To substitute new for all 'old's on a line type :s/old/new/g
    3. To substitute phrases between two line #'s type :#,#s/old/new/g
    4. To substitute all occurrences in the file type :%s/old/new/g
    5. To ask for confirmation each time add 'c' :%s/old/new/gc

Copy & Paste, Another method to Replace, Setting options

  • Replace text - Typing a capital R enters Replace mode until <ESC> is pressed.

  • Copy and Paste text -

    1. Start Visual mode with v and move the cursor and select the portion that needs to be copied.
    2. The y operator yanks (copies) text,p puts (pastes) it.

      Note: The visual mode can be used with the d command to cut text and then paste it using p.

    3. Typing ":set xxx" sets the option "xxx". Some options are:

        - 'ic' 'ignorecase'       ignore upper/lower case when searching
        - 'is' 'incsearch'        show partial matches for a search phrase
        - 'hls' 'hlsearch'        highlight all matching phrases
      

      You can either use the long or the short option name.

Conclusion

This is as much you'll need to get started with using Vim as an all-purpose editor, but there's a lot more that you can do with it.

Remember the three basic tricks for a newbie to vim:

  • Esc Esc always gets you back to "normal mode"
  • From normal mode :q! will always quit without saving anything you've done, and
  • From normal mode u will undo the last action

Other Resources:

  1. Vim tutorial (video)
  2. Ultimate Vim configuration
  3. Vim Game

Did you find this article valuable?

Support Saurav Shrivastav by becoming a sponsor. Any amount is appreciated!