Vim for everyday use

  1. Prologue: edit, save and exit!
  2. Introduction
  3. Modes
  4. Navigation
  5. Editing
  6. Search
  7. Useful commands
  8. Customization
  9. Conclusion

Prologue: edit, save and exit!

Edit a file with Vim:
1. vim <filename>
2. Press i
3. Make your changes

Saving in Vim:
1. Press ESC
2. Press :
3. Type w

Exit Vim:
1. Press ESC
2. Press :
3. Type q!

Introduction

When using Vim I often find myself trying to remember how to do various things: how do you comment on multiple lines of code? what’s the shortcut for undo? what was the command for setting line numbers? With this post, I’m trying to gather all this information in one place so I can easily find it at a later date and hope that it will be useful to you too.

Vim is a clone of Vi and stands for Vi IMproved. It was written by Bram Moolenaar and released to the public in 1991. Vi itself was written as a visual editor for the ex line editor which in turn was inspired by ed. Ed was the line editor on the Unix operating system.

You might be wondering what is a line editor and how is it different from a text editor?
A line editor is a limited text editor; limited in the sense that you edit the text but you don’t see the result immediately because there’s no monitor to output the text. It was used especially on teletypes (which are printers with keyboards) that had no video display and therefore no ability to interactively move a cursor or see the result of your editing, unlike a screen-based text editor.

Modes

Vim has several modes, in which characters that you type have different meanings.

  • Normal
    • this is the default mode
    • can be accessed by pressing ESC or CTRL-[
    • characters typed in this mode have special meaning and are used for manipulating, searching and navigating within the text
  • Insert
    • can be accessed by:
      • pressing i enters insert mode before the cursor
      • pressing I enters insert mode at the start of the current line
      • pressing a enters insert mode after the cursor
      • pressing A enters insert mode at the end of the current line
    • characters don’t have a special meaning in this mode; they appear as you type them
  • Visual
    • can be accessed by pressing v and will also mark a selection start point
    • used for making text selections
    • useful for copy, paste, delete, replace operations
  • Command
    • can be accessed by typing :
    • a prompt appears at the bottom of the window where you can input your commands
    • useful for things which can’t be done easily in the normal mode
  • Replace
    • easily replace text by writing over it
    • from normal mode, position your cursor over the first character that you want to replace; press R and start typing your text; when done, press ESC
ActionKeyMode
Move left<count>hNormal
Move right <count>lNormal
Move up <count>k Normal
Move down <count>j Normal
Move to beginning of next wordw Normal
Move to the beginning of the previous wordb Normal
Move to end of the worde Normal
Move to the beginning of the line0 Normal
Move to the end of the line$ Normal
Jump to line number:<number> or <number>GCommand / Normal
Jump to the first line in the filegg Normal
Jump to the last line in the fileG Normal
Page downCTRL-f Normal
Page upCTRL-b Normal

Editing

ActionKeyMode
Undo the last actionuNormal
Undo all recent changes to the current lineU Normal
RedoCTRL-r Normal
Delete the current linedd Normal
Cut the current line (yank)yy or Y Normal
Paste the cut textp Normal
Paste above the current lineP Normal
Delete the character under the cursor.
If <count> is given, it will also delete characters after the cursor
<count>x Normal
Enter Visual Mode and select one character at a timev Normal
Enter Visual Mode and select lines at a timeV Normal
Enter Visual Mode and select blocks at a time.
Makes a rectangle selection of text.
CTRL-v Normal
ActionKeyMode
Search for text/<text>ENTERNormal
Next search matchn Normal
Previous search matchN Normal

Useful commands

Find and replace
:%s/foo/bar/g
: enter command mode
% search across all lines (optional)
/foo search for “foo”
/bar replace with “bar”
/g global otherwise will execute only once per line

Show line number
:set number
: enter command mode
set builtin command
number option to be configured
There are a lot of options that can be configured. Please see https://www.shortcutfoo.com/blog/top-50-vim-configuration-options/ for a more complete list.

Comment / uncomment multiple lines
Comment:
Go to the first line you want to comment. Press CTRL-v and use the arrow key to select until the last line you want to comment. Press SHIFT-i and then write your comment character (# for Python, // for C, C++, etc.). Press ESC to exit visual mode.

Uncomment:
Go to the first line you want to uncomment. Press CTRL-v and use the arrow keys to select all the lines you want to uncomment. Press d to delete characters and then press ESC.

Customization

You can create a .vimrc file in your home directory and add various configuration options which will be applied to Vim.
For an example of what a vimrc file looks like, please see https://vim.fandom.com/wiki/Example_vimrc

Conclusion

Vim can be confusing if you’re used to text editors like nano, joe, notepad. I think it’s confusing because it has different modes which are specialized for different things. This is Vim’s greatest power and also its weakness because if you don’t take the time to get comfortable with this modes, you’ll be lost and frustrated.

I tried to summarize the most useful commands and shortcuts for making it easier to edit text in Vim. However, there’s a lot more power to Vim and several books have been written to show this like “Practical Vim” by Drew Neil, “Pro Vim” by Mark McDonnell, and others.

If you have any favorite shortcuts or snippets please let me know in the comments.
Thank you!

2 Comments

  1. Nice write up!

    One thing I like to set up in addition to `set number` is `set relativenumber`.

    This helps me navigate the file more easily. With just `number` you will have
    to manually count the lines where you want to jump; with `relativenumber` vim
    does this for us. It will show the relative distance in line number from the
    current line.

    Also a neat trick to format your paragraph in case you use it to write docs is
    `vipgq`. For example if you have set a maximum column width it will wrap the
    paragraph around that column. I used it to write this comment.

    Also you can use `J` to merge current line and next.

    What I love most about vim is the ease with which I can navigate around the file.
    Glad to meet a fellow vim lover. 🙂 Cheers!

Leave a Comment

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

four × one =

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