Contents

Vim Cheatsheet


Started to learn vim from many years ago and used it on and off from time to time. I still feel it’s hard to master but fun to use. It’s frustrating that I wanted to improve my text editing speed but always stumbled for basic commands. So this time I am not aiming for complex combos, just enjoying the convenience it brings.

Reicepes

This is a section for common vim operations. It is meant to put this section in the end but I have to look back so often so I just move it to the begining for my own convenience.

Write with sudo trick

Allow users to write on a file that needs root permission even when users forgot to open vim with sudo:

1
:w !sudo tee %

Detailed explaination can be found on stackoverflow.

Fast operations

  • Ctrl + r(Normal mode): redo changes
  • 4Ctrl + r(Normal mode): redo changes for 4 times
  • Ctrl + r + registername(insert mode): paste the current buffer contents
    • ": last used register
    • %: name of the current file(Ctrl + r + %will paste current filename)
  • "+p: paste from system clipboard ("+ is the system clipboard register)
  • :reg: access all currently defined registers
  • "ayy: copy the current line into register a

Moving around

Marks

Similar to the idea of bookmark, a mark in vim allow users to record current curson location with lower case letters and easily go back to recorded location with chosen letter.

  • ma: set mark a at current cursor location, mark name can be chosen from [a-z]
  • 'a: jump to the first non-blank character in line of mark a
  • :marks: list all the marks

Cursor movements

  • h: move one character left
  • j: move one row down
  • k: move one row up
  • l: move one character right
  • 3k: move two rows up

Word movements

  • w: move to beginning of next word
  • b: move to previous beginning of word
  • e: move to end of word
  • W: move to beginning of next word after a whitespace
  • B: move to beginning of previous word before a whitespace
  • E: move to end of word before a whitespace

Fast line movements:

  • 0: move to the beginning of the line
  • ^: move to first character of the line
  • $: move to the end of the line
  • G: move to the end of the file
  • gg: move to the beginning of the file
  • 99G: move to the line 99
  • 99gg: move to the line 99

Copy y

  • yy: copy current line
  • 10yy: copy 10 lines
  • yw: copy a word
  • y$: copy to the end of current sentence
  • yfa: copy between cursor to first a charactor
  • y2fa: copy bwtween cursor to second a charactor

Cut x

  • x: cut a character
  • 2x: cut two characters
  • xp: switch two characters

Delete d

  • dd: delete current line
  • 2dd: delete two lines
  • dw: delete a word
  • dfa: delete from cursor to first a charactor

Paste

  • p: paste copied and cutted content(from vim register actually)
  • 2p: paste twice

Quote a word

Assume we are using single quotes here: ciw'Ctrl+r"'

  • ciw: delete the current word and enter insert mode
  • ': add the first quote
  • Ctrl+r": insert the content of the " resigter(the last yanked/deleted)
  • ': add the closing quote

Unquote a word

Assume we are using single quotes: di'hPl2x

  • di': delete the word inside single quotes
  • hp: move the cursor left for one character and put the deleted content before the quote
  • l: move the cursor right one character(right on the opeing quote)
  • 2x: delete two quotes

Change case of a word

  • visual select a word then ~
  • visual select a word then U for uppercase or u for lowercase
  • cusor on a word then gUiw for uppercase or gUaw for lowercase
  • g~~ to invert case for entire line

Why vim

  • It’s poweful. Vim enables uses to move around and edit text in a command way, which is more efficient than using mouse. This also introduces complexity, of course.

  • It’s ubiquitous. The basics remain the same on all text editing tools like vscode, terminal, pycharm, etc. Furthermore, it’s available on all operating systems.

Basics

Concept of modes

If you use normal text editors such as nodepad, there is only one mode: you open it and start type, and that is so nature because everyone use text editor like that. Couterintuitively, vim has 4 modes.

  • Normal mode: by default, vim starts in normal mode, also known as command mode. In this mode you cannot type characters like in insert mode, instead, what you type becaomes commands for moving and manipulating text.

    Note
    Normal mode can be accessed from other modes by pressing Esc.

  • Inert mode: in this mode, you write text as if in normal text editors.

    Note
    From normal mode, use an insert command to enter insert mode.

  • Visual mode: Your movement changes your text highliting. Just like you use mouse to click and drag for a text block selection, but more powerful because you can select text both character-wise or line-wise.

    Note

    Press v to enter visual mode then move cursot to select text.

    Press V to enter visual line mode.

    Press <ctrl-V> to enter visual block mode.

  • Ex mode: it’s similar to the command line mode as it allows you to enter Ex commands.

    Note
    Press : to enter command mode.

Thinking in vim

Like we form a sentence using different kinds of words. Vim is designed to construct operations like a language, more specificly, like a sentence with nouns, verbs, and modifiers.

Verbs

Verbs are the actions that can be performed on nouns.

  • d: delete
  • c: change
  • y: yank(copy)
  • v: visually select
  • V: visually select in line mode

Modifiers

Modifiers are used before nouns to describe how your verbs work on nouns.

  • i: inside
  • a: around
  • t: search and moves cursor before search term
  • f: search and moves cursor on serch term

Nouns

Nouns are object you want to work on. Can be a word, a sentence or your selection of text.

  • w: word
  • s: sentence

Building operations like a sentence

Think about how we used to do text editing, for example, changing a word to another: we use mouse to double click the word or drag cursor from beginning to end of the word, delete it and then start typing a new word.

In vim, we acheive this in a different way: In normal mode, move cursor to the word and type ciw, then start typeing new word. The key is having what operation in mind and translate that operation to a “language” construct by verb, modifier, and noun. It sounds complicated and indeed it is, but once you onvercome the learning curve than text editing becomes much faster than normal way.

The learning curve is there and nobody can remember all this in just a glance. I’ll note receipes for common tasks for my own references.