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 going through the pages, I try to explain as I go, but assume some base knowledge. The Tao of Vim is a good page to start as it covers the core principles of Vim.

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 tutor program. You need to install a full version, something like apt install vim-nox

I use Neovim as my primary editor, installed with apt install neovim, and to launch the tutor run :Tutor inside the editor.

Exiting Vim

There are plenty of memes and 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 vim

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

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.

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.

Modes - Vim has different explicit modes of operations. The two main ones are NORMAL and INSERT modes. INSERT mode is when you are typing text in the editor. NORMAL mode is when you are operating on that text. See The Tao of Vim page for a more indepth explaination.

Recording - Recording are macros in Vim that can record and playback a set of actions. See recording section for more.

Registers - Registers are the location Vim copies text to and from, they are a set of clipboards. See the registers page for more.