Code in Vim with vim

References

What is Vim?

  • It's a modal editor
  • It allows you to make atomic edits
  • It a lot like an instrument or a language
  • It solves the missing Mac Delete button problem

Why should I care?

Right tool for the job

Here's why..

  • It's ubiquitous (i.e. it's "everywhere")
  • It's scalable (i.e. Can be use for small and big tasks)
  • It's powerful (i.e. With practice you can do some cool stuff)

One problem though..

It has a steep learning curve

The Four Levels of Vim Mastery

Level 1:

Survive

Surviving Normal Mode

i → Insert mode. Type ESC to return to Normal mode.

x → Delete the char under the cursor

:wq → Save and Quit (:w save, :q quit)

dd → Delete (and copy) the current line

p → Paste

hjkl→ basic cursor move (←↓↑→).

:help <command> → Show help about <command>.

Level 2:

Feel comfortable

Insert mode variations

a → insert after the cursor

o → insert a new line after the current one

O → insert a new line before the current one

cw → replace from the cursor to the end of the word 

Basic moves

0 → go to the first column

^ → go to the first non-blank character of the line

$ → go to the end of line

g_ → go to the last non-blank character of line

/pattern → search for pattern

Copy/Paste

P → paste before, remember p is paste after current position.

yy → copy the current line, easier but equivalent to ddP

Undo/Redo

u → undo

<C-r> → redo

Load/Save/Quit/Change File

:e <path/to/file> → open

:w → save

:saveas <path/to/file> → save to <path/to/file>

:x, ZZ or :wq → save and quit (:x only save if necessary)

:q! → quit without saving, also: :qa! to quit even if there are modified hidden buffers.

:bn (resp. :bp) → show next (resp. previous) file (buffer)

Level 3:

Feel Better, Stronger, Faster

Better: Repetition

. → (dot) will repeat the last command

N<command> → will repeat the command N times

Stronger: Movement

NG → Go to line N

gg → shortcut for 1G - go to the start of the file

G → Go to last line

w → go to the start of the following word

e → go to the end of this word

% → Go to the corresponding (, {, [.

* (resp. #) → go to next (resp. previous) occurrence of the word under the cursor

Faster: Ranges

<start position><command><end position>

eg. 0g$

  • 0 → go to the beginning of this line
  • y → yank from here
  • $ → up to the end of this line

 

Level 4:

Vim superpowers

Tip for building commands

<verb><modifiers><noun>

verb: d,c,y,v

modifier: i,a,t,f,2,/

noun:w,s,p,block,"lol"

 

Movement on the current line

0 → go to column 0

^ → go to first character on the line

$ → go to the last column

g_ → go to the last character on the line

fa → go to next occurrence of the letter a on the line. , (resp. ;) will find the next (resp. previous) occurrence.

t, → go to just before the character ,.

3fa → find the 3rd occurrence of a on this line.

F and T → like f and t but backward.

Zone selection

<action>a<object>

<action>i<object>

actions: v,d,c

objects: w,W,s,p or characters

Select rectangular blocks: C-v

C-v : Visual mode block selection

0<C-v><C-d>I-- [ESC]

Completion: <C-n> and <C-p>

C-v : Visual mode block selection

0<C-v><C-d>I-- [ESC]

Macros: q

Registers: a,b

qa

@a

Visual selection: v,V,<C-v>

J → join all the lines together.

< (resp. >) → indent to the left (resp. to the right).

= → auto indent

Splits: :split and vsplit

:split → create a split (:vsplit create a vertical split)

<C-w><dir> : where dir is any of hjkl or ←↓↑→ to change the split.

<C-w>_ (resp. <C-w>|) : maximise the size of the split (resp. vertical split)

<C-w>+ (resp. <C-w>-) : Grow (resp. shrink) split

:wq