Signup/Sign In

Learn Basic Vim Commands

Posted in Tricks   LAST UPDATED: MAY 15, 2021

    In this article, we are going to discuss some really basic vim commands that you need to handle the vim editor. There is no end to learning vim commands and how to use this editor, but if you want to learn some simple commands like how to open a file, edit a file, or do some changes to it, then you are at the right place. This basic vim commands guide will help you to handle the vim editor in the Linux terminal.

    Basic Vim commands for beginners

    Open a file in Vim

    vi <filename> or vim <filename> To open a file using vim editor

    Switching between the modes in Vim

    Vim has two modes command mode and insert mode.

    When you open a file with vim, you enter the command mode by default. When in command mode, the below commands can be used to get into the insert mode:

    Press I To start insert mode or the pen mode. The cursor will stay at the same place and vim editor goes into edit mode and you can start editing the file content.
    Press SHIFT + I To start insert mode with the cursor at the beginning of the current line. (on left most character)
    Press SHIFT + A To start insert mode with the cursor at the end of the current line. (on right most character)
    Press O Start insert mode and new line will be created after the current line.
    Press SHIFT + O Start insert mode and new line will be created before the current line.

    Once you are in the insert mode, you can press ESC to exit the insert mode.

    Moving around in Vim

    Use the arrow keys To move up, down, and sideways in any file open in the vim editor.
    SHIFT + G To jump to the end of the file
    Press GG To jump to the beginning of the file
    Press L To move one character towards the right
    Press H To move the cursor one character towards the left
    SHIFT + M To jump to the middle of the page visible in the terminal window (this doesn't mean the middle of the file, but the middle of the visible section of the file).

    Undo and Redo changes

    When you are editing a file in the insert mode, shortcuts or commands for Undo and Redo are very useful.

    First switch to the command mode(press ESC) and then press U This will undo the changes. If you keep on pressing U, vim will keep on undoing the changes.
    Press CTRL + R This will redo the changes. If you keep pressing these keys together, Redo keeps getting edecuted.

    Delete text in Vim

    We can use the following commands to delete text:

    Press Delete To delete text, when in insert mode.
    Press X To delete text, when in command mode.

    Deleting the entire line

    To delete an entire line, use the following commands:

    Press D twice - DD To delete the current line.

    Copy & Paste Text

    If you are in the command mode,

    Press YY to copy the entire line into vim buffer, and then you can press P to paste this.

    If you have used the DD command to delete a line, then if you press P then that line is pasted because the line which is recently deleted remains in the buffer until some other command is run.

    NOTE: We can also copy the content using the normal copy command in Windows operating system or any other system, then enter the INSERT mode in the Vim editor, and then use the normal operating system shortcut to paste.

    Searching for text

    If you're looking for a particular text, you can use the following command to search it:

    ?<text-to-search> Forward search
    /<text-to-search> Backward search

    It will match the first match from the cursor position. Now if you want to go to the next match, you just have to press N and if you keep on pressing it will go on matching all the matches and if you want to go back to the previous match, you just have to press Shift + N.

    If you don't find your match the Vim editor will say that the pattern was not found.

    Save Changes and Quit in Vim

    The usual CTRL + C command doesn't work with the Vim editor.

    When in command mode:

    Press :w Save changes in the file. But this won't quit the file.

    To save and quit when in insert mode or to just quit throwing away all the changes, use the following commands:

    Press :wq Save and quit the file
    Press :q! To force quit a file. Changes are not saved.


    We hope this article helps you with some basic Vim editor commands which are more than enough to use the Vim editor in your day-to-day file while using a Linux machine. If you think we missed any important command, or if there is some command which you find super important, please share it in the comment section below.

    About the author: