Introduction

Back in 2007, I had a rough beginning as a design engineer at a semiconductor company in terms of software tools. Linux command line, Vim and Perl were all new to me. I distinctly remember progressing from dd (delete current line) to d↓ (delete current line as well as the line below) and feeling happy that it reduced time spent on editing. Since I was learning on the job, I didn't know about count prefix or the various ways I could've deleted all the lines from the beginning of the file to the line containing a specific phrase. Or even better, I could've automated editing multiple files if I had been familiar with sed or progressed that far with Perl.

I also remember that we got a two-sided printed cheatsheet that we kept pinned to our cabins. That was one of the ways I kept adding commands to my repertoire. But, I didn't have a good insight to Vim's philosophy and I didn't know how to apply many of the cheatsheet commands. At some point, I decided to read the Vim book by Steve Oualline and that helped a lot, but it was also too long and comprehensive for me to read it all. My memory is hazy after that, and I don't recall what other resources I used. However, I'm sure I didn't effectively utilize built-in help. Nor did I know about stackoverflow or /r/vim until after I left my job in 2014.

Still, I knew enough to conduct a few Vim learning sessions for my colleagues. That came in handy when I got chances to teach Vim as part of scripting course for college students. From 2016 to 2018, I started maintaining my tutorials on Linux command line, Vim and scripting languages as GitHub repos. As you might guess, I then started polishing these materials and published them as ebooks. This is an ongoing process, with Vim Reference Guide being the twelfth ebook.

Why Vim?

You've probably already heard that Vim is a text editor, powerful one at that. Vim's editing features feel like a programming language and you can customize the editor using scripting languages. Apart from plethora of editing commands and support for regular expressions, you can also incorporate external commands. To sum it up, most editing tasks can be managed from within Vim itself instead of having to write a script.

Now, you might wonder, what is all this need for complicated editing features? Why does a text editor require programming capabilities? Why is there even a requirement to learn how to use a text editor? Isn't it enough to have the ability to enter text, use Backspace/Delete/Home/End/Arrow/etc, menu and toolbar, some shortcuts, a search and replace feature and so on? A simple and short answer — to reduce repetitive manual task.

What I like the most about Vim:

  • Lightweight and fast
  • Modal editing helps me to think logically based on the type of editing task
  • Composing commands and the ability to record them for future use
  • Settings customization and creating new commands
  • Integration with shell commands

There's a huge ecosystem of plugins, packages and colorschemes as well, but I haven't used them much. I've used Vim for a long time, but not really a power user. I prefer using GVim, tab pages, mouse, arrow keys, etc. So, if you come across tutorials and books suggesting you should avoid using them, remember that they are subjective preferences.

Here are some more opinions by those who enjoy using Vim:

warning Should everybody use Vim? Is it suitable for all kinds of editing tasks? I'd say no. There are plenty of other well established text editors and new ones are coming up all the time. The learning curve isn't worth it for everybody. If Vim wasn't being used at job, I probably wouldn't have bothered with it. Don't use Vim for the wrong reasons article discusses this topic in more detail.

Installation

I use the following command on Ubuntu (a Linux distribution):

sudo apt install vim vim-gui-common

info See also https://github.com/vim/vim for source code and other details.

Ice Breaker

Open a terminal and follow these steps:

  • gvim ip.txt opens a file named ip.txt for editing
    • You can also use vim if you prefer terminal instead of GUI, or if gvim is not available
  • Press i key (yes, the lowercase alphabet i, not some alien key)
  • Start typing, for example What a weird editor
  • Press Esc key
  • Press : key
  • Type wq
  • Press Enter key
  • cat ip.txt — sanity check to see what you typed is saved or not

Phew, what a complicated procedure to write a simple line of text, isn't it? This is the most challenging and confusing part for a Vim newbie. Here's a brief explanation of the above steps:

  • Vim is a modal editor. You have to be aware which mode you are in and use commands or type text accordingly
  • When you first launch Vim, it starts in Normal mode (primarily used for editing and moving around)
  • Pressing i key is one of the ways to enter Insert mode (where you type the text you want to save in a file)
  • After you've entered the text, you need to save the file. To do so, you have to go back to Normal mode first by pressing the Esc key
  • Then, you have to go to yet another mode! Pressing : key brings up the Command-line mode and awaits further instruction
  • wq is a combination of write and quit commands
    • use wq ip.txt if you forgot to specify the filename while launching Vim, or perhaps if you opened Vim from Start menu instead of a terminal
  • Enter key completes the command you've typed

If you launched GVim, you'll likely have Menu and Tool bars, which would've helped with operations like saving, quitting, etc. Nothing wrong with using them, but this book will not discuss those operations. In fact, you'll learn how to configure Vim to hide them in the Customizing Vim chapter.

Don't proceed any further if you aren't comfortable with the above steps. Take help of youtube videos if you must. Master this basic procedure and you will be ready for Vim awesomeness that'll be discussed in the coming sections and chapters.

info Material presented here is based on GVim (GUI), which has a few subtle differences compared to Vim (TUI). See this stackoverflow thread for more details.

info Options and details related to opening Vim from the command line will be discussed in the CLI options chapter.

Built-in tutor

  • gvimtutor command that opens a tutorial session with lessons to get started with Vim
    • don't worry if something goes wrong as you'll be working with a temporary file
    • use vimtutor if gvim is not available
    • pro-tip: go through this short tutorial multiple times, spread over multiple days

info Next step is :h usr_02.txt, which provides enough information about editing files with Vim.

Built-in help

Vim comes with comprehensive user and reference manuals. The user manual reads like a text book and reference manual has more details than you are likely to need. There's also an online site with these help contents, which will be linked as appropriate throughout this book.

  • You can access built-in help in several ways:
    • type :help from Normal mode (or just the :h short form)
    • GVim has a Help menu
    • press F1 key from Normal mode
  • :h usr_toc.txt table of contents for User Manual
    • Task oriented explanations, from simple to complex. Reads from start to end like a book
  • :h reference_toc table of contents for Reference Manual
    • Precise description of how everything in Vim works
  • :h quickref quick reference guide
  • :h help-summary effectively using help depending on the topic/feature you are interested in

Here's a neat table from :h help-context:

WHATPREPENDEXAMPLE
Normal mode command:help x
Visual mode commandv_:help v_u
Insert mode commandi_:help i_<Esc>
Command-line command: :help :quit
Command-line editingc_:help c_<Del>
Vim command argument- :help -r
Option' :help 'textwidth'
Regular expression/ :help /[

info You can go through a copy of the documentation online at https://vimhelp.org/. As shown above, all the :h hints in this book will also be linked to the appropriate online help section.

Vim learning resources

As mentioned in the Preface chapter, this Vim Reference Guide is more like a cheatsheet instead of a typical book for learning Vim. In addition to built-in features already mentioned in the previous sections, here are some resources you can use:

Tutorials

Books

Interactive

  • OpenVim — interactive tutorial
  • Vim Adventures — learn Vim by playing a game
  • vimmer.io — master Vim from the comfort of your web browser
  • vim.so — interactive lessons designed to help you get better at Vim faster

info See my Vim curated list for a more complete list of learning resources, cheatsheets, tips, tricks, forums, etc.

Modes of Operation

As mentioned earlier, Vim is a modal editor. This book will mainly discuss these four modes:

  • Insert mode
  • Normal mode
  • Visual mode
  • Command-line mode

This section provides a brief description for these modes. Separate chapters will discuss their features in more detail.

info For a complete list of modes, see :h vim-modes-intro and :h mode-switching.

Insert mode

This is the mode where the required text is typed. There are also commands available for moving around, deleting, autocompletion, etc.

Pressing the Esc key takes you back to Normal mode.

Normal mode

This is the default mode when Vim is opened. This mode is used to run commands for operations like cut, copy, paste, recording, moving around, etc. This is also known as the Command mode.

Visual mode

Visual mode is used to edit text by selecting them first. Selection can either be done using mouse or using visual commands.

Pressing the Esc key takes you back to the Normal mode.

Command-line mode

This mode is used to perform file operations like save, quit, search, replace, execute shell commands, etc. Any operation is completed by pressing the Enter key after which the mode changes back to the Normal mode. The Esc key can be used to ignore whatever is typed and return to the Normal mode.

The space at the end of the file used for this mode is referred to as Command-line area. It is usually a single line, but can expand for cases like auto completion, shell commands, etc.

Identifying current mode

  • In Insert mode, you get a blinking | cursor
    • also, -- INSERT -- can be seen on the left hand side of the Command-line area
  • In Normal mode, you get a blinking rectangular block cursor, something like this █
  • In Visual mode, the Command-line area shows -- VISUAL -- or -- VISUAL LINE -- or -- VISUAL BLOCK -- according to the visual command used
  • In Command-line mode, the cursor is of course in the Command-line area

info See also :h 'showmode' setting.

Vim philosophy and features

info Commands discussed in this section will be covered again in later chapters. The idea here is to give you a brief introduction to modes and notable Vim features. See also:

As a programmer, I love how composable Vim commands are. For example, you can do this in Normal mode:

  • dG delete from the current line to the end of the file
    • where d is the delete command awaiting further instruction
    • and G is a motion command to move to the last line of the file
  • yG copy from the current line to the end of the file
    • where y is the yank (copy) command awaiting further instruction

Most Normal mode commands accept a count prefix. For example:

  • 3p paste the copied content three times
  • 5x delete the character under the cursor and 4 characters to its right (total 5 characters)
  • 3 followed by Ctrl+a add 3 to the number under the cursor

There are context aware operations too. For example:

  • diw delete a word regardless of where the cursor is on that word
  • ya} copy all characters within {} including the {} characters

If you are a fan of selecting text before editing them, you can use the Visual mode. There are several commands you can use to start Visual mode. If enabled, you can even use mouse to select the required portions.

  • ~ invert the case of the visually selected text (i.e. lowercase becomes UPPERCASE and vice versa)
  • g followed by Ctrl+a for visually selected lines, increment number by 1 for the first line, by 2 for the second line, by 3 for the third line and so on

The Command-line mode is useful for file level operations, search and replace, changing Vim configurations, talking to external commands and so on.

  • /searchpattern search the given pattern in the forward direction
  • :g/call/d delete all lines containing call
  • :g/cat/ s/animal/mammal/g replace animal with mammal only for the lines containing cat
  • :3,8! sort sort only lines 3 to 8 (uses external command sort)
  • :set incsearch highlights current match as you type the search pattern

Changes to Vim configurations from the Command-line mode are applicable only for that particular session. You can use the vimrc file to load the settings at startup.

  • colorscheme murphy use a dark theme
  • set tabstop=4 width for the tab character (default is 8)
  • nnoremap <F5> :%y+<CR> map F5 key to copy everything to system clipboard in Normal mode
  • inoreabbrev teh the automatically correct teh to the in Insert mode

There are many more Vim features that'd help you with text processing and customizing the editor to your needs, some of which you'll get to know in the coming chapters.

Finally, you can apply your Vim skills elsewhere too. Vim-like features have been adopted across a huge variety of applications and plugins, for example:

Vim's history

See Where Vim Came From if you are interested in knowing Vim's history that traces back to the 1960s with qed, ed, etc.

Chapters

Here's the list of remaining chapters: