VIM

Vim (Vi IMproved) is a clone of the popular vi editor for Unix. It is a text editor designed for speed and increased productivity, and is ubiquitous in most unix-based systems. It has numerous keybindings for speedy navigation to specific points in the file, and for fast editing.

vimtutor is a an excellent application that teaches you how to use Vim. It comes with the vim package during installation. You should be able to just run “vimtutor” on the command line to open this tutor. It will guide you through all the major features in vim.

Basics of navigating Vim

    vim <filename>    # Open <filename> in vim
    :help <topic>     # Open up built-in help docs about <topic> if any exists
    :q                # Quit vim
    :w                # Save current file
    :wq               # Save file and quit vim
    ZZ                # Save file and quit vim
    :q!               # Quit vim without saving file
                      # ! *forces* :q to execute, hence quitting vim without saving
    ZQ                # Quit vim without saving file
    :x                # Save file(only when the file is modified) and quit vim

    u                 # Undo
    CTRL+R            # Redo

    h                 # Move left one character
    j                 # Move down one line
    k                 # Move up one line
    l                 # Move right one character

    Ctrl+B            # Move back one full screen
    Ctrl+F            # Move forward one full screen
    Ctrl+D            # Move forward 1/2 a screen
    Ctrl+U            # Move back 1/2 a screen

    # Moving within the line

    0                 # Move to beginning of line
    $                 # Move to end of line
    ^                 # Move to first non-blank character in line

    # Searching in the text

    /word             # Highlights all occurrences of word after cursor
    ?word             # Highlights all occurrences of word before cursor
    n                 # Moves cursor to next occurrence of word after search
    N                 # Moves cursor to previous occurrence of word

    :%s/foo/bar/g     # Change 'foo' to 'bar' on every line in the file
    :s/foo/bar/g      # Change 'foo' to 'bar' on the current line
    :%s/\n/\r/g       # Replace new line characters with new line characters
    :'<,'>s/foo/bar/g # Change 'foo' to 'bar on every line in the current visual selection

    # Jumping to characters

    f<character>      # Jump forward and land on <character>
    t<character>      # Jump forward and land right before <character>

    # For example,
    f<                # Jump forward and land on <
    t<                # Jump forward and land right before <

    # Moving by word

    w                 # Move forward by one word
    b                 # Move back by one word
    e                 # Move to end of current word

    # Other characters for moving around

    gg                # Go to the top of the file
    G                 # Go to the bottom of the file
    :NUM              # Go to line number NUM (NUM is any number)
    H                 # Move to the top of the screen
    M                 # Move to the middle of the screen
    L                 # Move to the bottom of the screen

Help docs:

Vim has built in help documentation that can accessed with :help <topic>. For example :help navigation will pull up documentation about how to navigate your workspace!

:help can also be used without an option. This will bring up a default help dialog that aims to make getting started with vim more approachable!

Modes:

Vim is based on the concept on modes.

  • Command Mode - vim starts up in this mode, used to navigate and write commands

  • Insert Mode - used to make changes in your file

  • Visual Mode - used to highlight text and do operations to them

  • Ex Mode - used to drop down to the bottom with the ‘:’ prompt to enter commands

The ‘Grammar’ of vim

Vim can be thought of as a set of commands in a ‘Verb-Modifier-Noun’ format, where:

  • Verb - your action

  • Modifier - how you’re doing your action

  • Noun - the object on which your action acts on

A few important examples of ‘Verbs’, ‘Modifiers’, and ‘Nouns’:

Some shortcuts and tricks

Macros

Macros are basically recordable actions. When you start recording a macro, it records every action and command you use, until you stop recording. On invoking a macro, it applies the exact same sequence of actions and commands again on the text selection.

Configuring ~/.vimrc

The .vimrc file can be used to configure Vim on startup.

Here’s a sample ~/.vimrc file:

References

Vim | Home

$ vimtutor

A vim Tutorial and Primer

What are the dark corners of Vim your mom never told you about? (Stack Overflow thread)

Arch Linux Wiki

Last updated