Watch on YouTube: youtu.be/eXzcs9Lm7d0

vim is 90% muscle memory! This video is not an explanation, it is an exercise! You must follow along for this video to work for you.

The most brief introduction to vim

The bare minimum you need to know in a world where vim exists and vi is sometimes your only option.

vim in an editor (a program you can use open a file, change text in the file, and save the file). My gut wrenches to make this comparison, but you can think of it as how you would use notepad if you constrained to using the mouse and navigating through trees of menus.

This introduction is aimed towards people who never intend to use vim and would prefer to know nothing about it, but are also realists and as such understand that, well, they'll still end up using it.

The goal is to teach you just enough to not leave you hopelessly lost, frustrated, or annoyed, but not enough to make you cool or respectable.

Lesson 1: How to exit vim

These are the things you MUST know (perfectly suitable if your only desire with vim is to get quit out of it.)

In order to exit vim and return to a terminal (without breaking anything), you need only to know these 4 things:

  • <ctrl> + q unfreezes vim after you accidentally hit <ctrl> + s
  • i puts you into insert mode
  • <esc> takes you back to the command mode (the from which you can rut the quit command)
  • :q! force quits without saving

If you do nothing but those 4 sequences in random order, you'll at least make it out alive.

Note: Even those with years of experience (i.e. novices like myself) occasionally hit a key sequence that puts them in a weird state. Pressing <esc> about 5 times and then i 5 times and then running <esc>:q! twice has always worked for me (except when I needed to hit <ctrl> + q first).

Lesson 1.5: How to save an edit

These are the things you MUST know (if you intend edit anything)

When you're in insert mode you can edit.

  • o will open a new line
  • :x exits after saving

When you're in command (<esc>) or visual mode you can move around

  • arrow keys will move you around (even in insert mode)
  • The right hand home row (h, j, k, l) will move you around if you're in old-school vi (which is not vim)

Pop quiz: Which home row underneath your right hand can be used for moving around? (answer in the appendix)

Lesson 2: Making it hurt less

If you intend to use vim more than accidentally, here's what I suggest learning first:

Undo

  • u undoes an action

Visual Selection

  • v toggles visual selection (like you would do with a mouse)

Text Manipulation

  • d deletes (and copies, aka cuts) the current selection
    • Tip: use d and then u to copy
  • p pastes the current buffer

Moving around

  • /foo search for foo

    • n moves to the next occurance
    • <shift> + n moves to the previous occurance
  • ^ moves to the beginning of line

  • $ moves to the end of line

  • w moves to the next word

  • 42j takes you 42 lines down (use any number you like)

  • 37k takes you 37 lines up (use any number you like)

  • Note: this combining numbers with commands (or multiple commands) is a pattern

  • Arrow Keys are sometimes an option, but not always.

  • h - move left (it's the leftmost character key on the home row)

  • l - move right (it's the rightmost character key on the home row)

  • j - move down

  • k - move up (like a kangaroo)

File Manipulation

  • :w - write the file
  • :wq - do both at once (same as x)
  • :q! - quit without saving

Lesson 3: Feel the burn

  • :%s/ABC/XYZ/gc replaces ABC (or whatever) with XYZ (or whatever else)
  • a edit after the current character
  • > indents all selected lines
  • < unindents all selected lines
  • . repeats the previous action (such as indenting or a macro)
  • shift+g takes you to the bottom of the file
  • gg takes you to the top of the file

Worship Services

P.S. vim is also a religion, in case you didn't already know.

Appendix

Pop Quiz Answer

That's right! Underneath your right hand is the home row with the little notch in the j that is the row that let's you move around!

The magic sequence that will always exit vim

<ctrl> + q
<esc> <esc> <esc> <esc> <esc>
i i i i i
<esc> <esc> <esc> <esc> <esc>
i i i i i
<esc> <esc> <esc> <esc> <esc>
:q!

Things I wish I had taught you

  • % moves to beginning or closing (, {, [, etc
  • y yanks (copies) the current selection
  • yy - copy line
  • dd - delete line
  • 37l takes your cursor 37 characters right (use any number you like)
  • 7h takes your cursor 7 characters left (use any number you like)
  • w moves to the next word

References


By AJ ONeal

If you loved this and want more like it, sign up!


Did I make your day?
Buy me a coffeeBuy me a coffee  

(you can learn about the bigger picture I'm working towards on my patreon page )