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**

Navigation

Action

Key

Mode

Move left

**<count>h**

Normal

Move right

**<count>l**

Normal

Move up

**<count>k**

Normal

Move down

**<count>j**

Normal

Move to beginning of next word

**w**

Normal

Move to the beginning of the previous word

**b**

Normal

Move to end of the word

**e**

Normal

Move to the beginning of the line

**0**

Normal

Move to the end of the line

**$**

Normal

Jump to line number

:<number> or <number>G

Command / Normal

Jump to the first line in the file

**gg**

Normal

Jump to the last line in the file

**G**

Normal

Page down

**CTRL-f**

Normal

Page up

**CTRL-b**

Normal

Editing

Action

Key

Mode

Undo the last action

**u**

Normal

Undo all recent changes to the current line

**U**

Normal

Redo

**CTRL-r**

Normal

Delete the current line

**dd**

Normal

Cut the current line (yank)

yy or Y

Normal

Paste the cut text

**p**

Normal

Paste above the current line

**P**

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 time

**v**

Normal

Enter Visual Mode and select lines at a time

**V**

Normal

Enter Visual Mode and select blocks at a time.
Makes a rectangle selection of text.

**CTRL-v**

Normal

Action

Key

Mode

Search for text

**/<text>ENTER**

Normal

Next search match

**n**

Normal

Previous search match

**N**

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!

The post Vim for everyday use appeared first on Bogdan Cornianu.