mkaz.blog

Working with Vim

Working with Vim

A set of vim tips and features to help you improve your fluency with vim or neovim. This started as my personal cheat sheet, than a post, and now numerous pages. I keep adding to it over time so who knows where it ends.

A basic understanding of vim would probably help, I try to explain everything but assume some base knowledge.

Learn Basics

The program vimtutor is a great way to learn the basics of vim. It is an interactive tutorial in vim walking through vim. Depending on your OS and how vim is installed, you may need to install the full vim package.

Ubuntu for example, only ships with a minimal vim package that does not include the tutorial. You need to install a full version, I use neovim as my primary, so install with apt install neovim first.

If you use Neovim, run :Tutor inside the editor.

Basic Definitions

First, let's define a few terms around the structure of Vim.

A buffer is the text loaded into memory for editing. In other editors or programs, this might just be called a file. In Vim, it is similar but the same object is also used in ways unrelated to files.

A window is a viewport onto a buffer. There is always at least one window and one buffer in Vim, typically they are 1:1. One buffer open in one window. When you start Vim without specifying a file, it is an empty buffer.

You may also have multiple buffers and/or multiple windows open at the same time. These are covered in the Buffers and Windows sections for more on each.

Exiting Vim

There are a meme of jokes about exiting Vim, the truth is once you grok using vim, you don't want to exit. 🙂

Here are just a few ways to exit

CommandAction
:wWrite current file
:w [file]Write buffer to file
:qQuit
:wqWrite current file and Quit
:x or ZZWrite if changes made and Quit
:q! or ZQQuit without save
:wqaSave all changed files, and quit
:qa!Quit all files, without save

Yes, there are numerous ways to exit. I find using either ZZ or :x my preferred way because it will not change the file's modified time if no changes were made.

Vim will try to protect you from yourself, if there are multiple files open with changes, it will not exit. You must explicitly use one of the ! commands or save the changes first.

Help Yourself

The first place to turn when you need help is Vim itself. It has extensive documentation on every command and feature.

Type :help [command] to see help for any command.

For example, :help gg will explain what the gg command does.

Help opens in a new window split horizontally, see Windows section for working with windows.

Close the window split using ctrl-w c or :close

To make help the only window, use ctrl-w o or :only. This removes the split but does not close help or your original buffer. Close the help buffer using :bd, to switch back to your original buffer.

Help is simply a read-only buffer. You can navigate, highlight, copy, paste, search, and do any vim things as any other buffer, except edit. See Buffers section for more on working with buffers.

The help pages include tags to other help sections, depending on your colorscheme it might be blue. With your cursor on an item type ctrl-] to jump to that definition.

Jumping to tag definitions actually works for any words in help regardless if they are tags. If a definition exists for the word, you will jump to it. Tags tell you that a definition does actually exist.