Using Vim

Mastering the Dark Arts

Vim is like…

A Unicorn Horn

The Ax of a Heavy Metal God

Your Very Own Patronus

When you know how to use it, amazing things can happen.

Modal Editing

There are different modes, or states that can be activated.

Two Main Modes

  • Normal
    • "Do stuff" mode. Move around a document, copy and paste, etc.
  • Insert
    • Insert text. This is what you're used to in other editors.

vim-two-main-modes.gif

The Vim Language

Normal Mode

Exactly describe the changes you want to see.

movement-prose.gif

Moving between words

  • w Move to the start of the next word
  • e Move to the end of the next word
  • b Move back one word
  • f. Move to the next period '.' on this line
  • fA Move to the next capital 'A' on this line
  • F; Move to the previous semi-colon on this line

Moving in the document

  • } "Move to the next section"
  • G "Move to the end of the document"
  • gg "Move to the beginning of the document"

Changing Things

  • d Delete (cut)
  • y Yank (copy)
  • c Change (delete and change to insert mode)

These require a motion so that Vim knows what to act on.

For Example

  • dip "Delete in paragraph"
  • y3w "Yank the next three words"
  • ct. "Change until the next '.'"
  • dG "Delete to the end of the document"

vim-two-main-modes.gif

The above changes are all just keystrokes.

yy3pfjcwflew^[jciwsoared^[jFqc/the^Mslow green turtle crept past ^[

  • u Undo
  • Ctrl-r Redo

Search

We think of Vim as primarily text editor…

  • / "Start a search going forward in the document"
  • ? "Start a search going backwards in the document"
  • n "Go to the next search hit"
  • N "Go to the previous search hit"

search-default.gif

This works, but…

It's hard to know exactly where the cursor has jumped to.

What if we could get some better visual feedback?

Enable Settings

  • :set incsearch "Turn incremental search on"
  • :set hlsearch "Turn highlight search on"
  • :set number "Turn line numbers on"
  • :set wrap "Turn on line wrapping"
  • :set noincsearch "Turn incremental search off"
  • :set nohlsearch "Turn highlight search off"
  • :set nonumber "Turn line numbers off"
  • :set nowrap "Turn off line wrapping"

search-settings.gif

  • Ctrl-f Scroll one page forward
  • Ctrl-b Scroll one page backward

Macros

Replacing tedious tasks.

Registers

Vim has named registers from a .. z

Think of these like a variable that you can store text in.

Record Macros

  • qq "Start recording a macro to the 'q' register"
  • q "When done, press q again to stop recording"

Replay Macros

  • @q "Replay the macro in the 'q' register"
  • @@ "Replace the last used macro"
  • 12@q "For twelve times, replay the 'q' macro"

macros-names.gif

This is Portable

The keystrokes for the above transformation looks like this:

0cl#^[lly$o^[p:s/ /_/g^M^Vu^[Vu^[i  - https://example.com/^[o  - Notes:^M^[j

Because creating and using a macro is so easy, this becomes a great way to apply tedious and repeditive changes.

:ex Mode

While "Normal" and "Insert" mode are the most common modes in Vim, Ex-mode is where things really get exciting!

You've probably already made use of this mode with

  • :w Write (save)
  • :q Quit
  • :help ex-cmd-index – get a list of all ex commands

Global

:g/regex/command

"Do stuff on lines"

Global Print

:g/regex/p

"Global Regular Expression Print"

ex-mode-print.gif

Global Delete

  • :g/regex/d "Delete every line matching a regex"
  • :v/regex/d "Delete every line NOT matching a regex"

ex-mode-delete.gif

Global Normal

:g/regex/normal @q

ex-mode-global-normal.gif

ex-mode-global-normal-macro.gif

Scripting Vim

vim -c ":<do-stuff>"

Remove Wordfence Tables from SQL Dump

wordfence-table.png

#!/bin/bash
# Usage: remove-wf-tables.sh db.sql
file="$1"
mv "$file" "$file.original"
vim -c 'let @t=""' \
  -c ':g/\v_wf.{-}\`/normal "Tyapdap' \
  -c ":sav $file" \
  -c ':new wf-tables.sql.bak | :normal "tp' \
  -c ':wa | :qa' \
  "$file.original"

Resources

Vimgolf

vim-golf.png

vim-golf-solutions.png