Graduate Program KB

(Slide assets and markdown avaliable on GitHub)

An Introduction to Vim

Agenda

  1. Background
  2. Know Your Modes!
  3. Command-line Mode Essentials
  4. Getting Around in Normal Mode
  5. Normal Mode Chords
  6. Getting into Insert Mode
  7. Bonus Content
  8. Sources and Futher Reading

Origins of Vim

ed (1973)

ed was developed by Ken Thompson at Bell Laboratories for the original UNIX operating system. ed was a line editor, meaning that it would only allow editing of files based on commands fed to it from a stdin stream. Users were unable to see the whole file they were editing without leaving the editor and wouldn't even display the results of their commands on their text without requesting it. Peter H. Salus once called it "the most user-hostile editor ever created" and is mainly known today for inspiring developers to create more usable successors like Emacs and vi.

vi (1976)

ex is an extended iteration of ed written by George Coulouris and Bill Joy included in the original BSD operating system. ex was soon rewritten to have a full-screen interface, allowing users to see the file they were editing, and became vi. Line editing commands could be invoked by entering command-line mode using the : character. To this day vi in included by default in most UNIX and BSD based distributions.

vim (1991 c. Bram Moolenaar)

Vi iMproved. Huge amount of enhancements including syntax highlighting, plugin support, mouse integration, comprehensive help system...

neovim (2015 c. Neovim Team)

Fork of Vim that supports the Language Server Protocol, asynchronous I/O and Lua scripting.

Setting Up Neovim

To install neovim, use the command snap install nvim --classic. It is not recommended to use the version avaliable via apt/apt-get since the package has not been actively maintained since 2021.

To run Neovim use the command nvim <file> or just nvim and use the vim command :e <file>.

Why should I use Vim?

  • When you don't have access to a GUI (e.g. ssh'ing into a remote server) or it's inconvenient to leave the command line.
  • Quickly edit one file without needing to wait for a fully featured IDE to open and analyze the entire containing directory/project.
  • Vim bindings are so popular that tons of programs add Vim modes for more efficient use.
  • Bragging rights.

Why should I use Vim instead of VS Code?

Don't!

  • Only use Vim if it makes the specific task you're doing more convenient. Vim can be effective as a lightweight support for a fully-featured IDE.
  • You can also integrate Vim (or NeoVim) into VS Code as an editor mode!

Know Your Modes

vimmodes

  • Normal Mode: For navigation and use of editor chords
  • Insert Mode: Insert text
  • Command-line Mode: Input : commands
  • Visual Mode: Visually select text

Command-line Mode Essentials

Saving and/or Exiting

  • :wq Save and exit
  • :x Alias for :wq
  • :q! Discard changes and quit
  • :w !sudo tee % Write to current file using sudo. Essential when you've accidentally started editing a file without calling nvim using sudo.

Customizing Your Editor

More info at :help options

  • :set nu Turn on line numbers
  • :set rnu Turn on relative line numbers
  • :set linebreak Ensure line wrap doesn't split words
  • :set autoindent Keep created new lines consistent with indentation of previous lines
  • To undo any of these commands, add a ! to the end of them, e.g. :set rnu!
  • To make sure your customizations apply each time you open neovim, create a file at ~/.config/nvim/init.vim

Getting Around in Normal Mode

Basic Motions

More info at :help options

← ↓ ↑ →
h j k l

  • gg and G go to start of or end of file
  • 0 and $ go to start of or end of line

  • F<char> and f<char> go to previous or next occurance of char in file
  • T<char> and t<char> go until previous or next occurance of char in file

  • e go to end of current/closest word
  • b go to beginning of current/closest previous word

Searching

More info at :help search

  • /word will search for all occurances of 'word' in your file, placing your cursor at the next occurance
  • ?word will search backward for all occurances of 'word' in your file, placing your cursor at the previous occurance
  • n to go to next word in search direction
  • N to go to previous word in search direction

  • To clear search highlighting use the command :nohl

Normal Mode Chords

"Vim's killer feature is the language it provides for making changes" - Chris Toomey, Mastering the Vim Language

What really sets vim apart from other text editors you may have used is the chords feature. Chords are structured sequences of keypresses users can enter in normal mode to get vim to complex actions for you automatically. Chords are performed atomically, so no matter how many actions vim is doing behind the scenes to complete an operation, the whole operation can be undone or repeated easily. Thousands of possible chords exist so any text opertation you could need will likely be possible with chords (and if it isn't I guarantee you there's a plugin for it).

The syntax of a vim chord is operator [modifier] object where modifier is optional. Another way of thinking about it is verb [modifier] noun.

Important Operators

More info at :help operator

  • d delete
  • c change
  • < and > unindent and indent
  • y yank
  • v select in visual mode

Text Objects

More info at :help text-object

  • l character
  • w word
  • s sentence
  • p paragraph
  • t HTML tag

Modifiers:

  • i inside text object
  • a around a text object

More text objects can be created by combining modifiers with text objects. Normally w represents a motion that just moves to the start of the next word, but iw translates to 'inner word', meaning that all the characters of the word you're currently in will be selected.
The difference between i and a is that a will select both the inside and the 'boundary' of a text object, e.g. it will select the content inside HTML tags while at represents the content AND the <tags>.

  • Modifiers also work with specific characters, e.g:
    • di( Delete all text inside parentheses
    • da" Delete whole string, including quote marks

Repeatable and Undoable

More info at :help repeating

  • . Execute last (non-navigation) command
  • <n><command> Adding a number before a command will repeat the command that many times, e.g. 4>l will indent 4 lines
  • u Undo last command
  • Ctrl+r Redo undone command

Motions as Text Objects

Motions will translate into text objects that contain all the text the cursor passed through while completing

  • Examples (What would these do?):
    • df"
    • c$
    • y2e

Getting into Insert Mode

More info at :help inserting

On the cursor

  • i Insert mode before cursor
  • a Insert mode after cursor
  • r Quick replace character at cursor
  • R Replace more than one character until Escape is pressed

On a line

  • I Insert mode at start of line
  • A Insert mode at end of line
  • o Create new line below current line and insert
  • O Create new line above current line and insert

Bonus Content

This section contains some useful features not mentioned in the slides

  • dd Alias for deleting entire line (probably the most important chord in vim)
  • >> and << Indent or unindent entire line
  • p Paste yanked content
  • "+y Yank into system clipboard
  • "+p Paste from system clipboard

Macros

Are chords not powerful enough for you? Just record a whole sequence of actions and save it to a register!

  • q<letter><record your actions...>q Keep in mind that this is not a chord, there isn't any limit to the number of actions you can do before you stop recording.
  • @<letter> Replay your actions from your current cursor permissions
  • 5@<letter> Repeat your recorded action 5 times
  • :%.normal @<letter> Run the macro on all lines in the file

Tabs

  • :tabnew Create a new tab. In the current snap version of neovim tabs are mouse selectable but you can also use:
    • gt next tab
    • gT previous tab
    • g<number> go to specific tab
  • :e <file> Start editing a specifc file while you're in an empty buffer

Sources and Further Reading