Vim

Getting Started With Vim

Basic Vim Motions

  • hjkl – left, up, down, right

  • 0 – beginning of line

  • ^ – beginning of text (within a line)
  • $ – end of line

  • gg – beginning of file

  • G – end of file

Basic Vim Commands

  • i – go into insert mode before the current character
  • I – go into insert mode at the beginning of the line (shortcut for ^i)

  • a – go into insert mode after current character

  • A – go into insert mode at the end of the line (shorcut for $a)

  • c_ – change (delete and insert)

  • C – change everything till the end of the line (shorcut for c$)
  • s – change a letter (shorcut for cl)

  • x – delete a character

  • d_ – delete

  • . – repeat last change

  • > and < – indent/unindent

  • = – fix indentation

  • o – new line and insert mode (shortcut for A+<ENTER>)

  • O – new line above and insert mode (shortcut for ko)

Examples of Common Commands

  • x – Delete a character
  • dd – Delete a line

  • . – Repeat last change

  • >> – Increase indentation in current line

  • << – Decrease indentation in current line
  • >G – Increase indendation till the end of the file

The Vim Way

  1. Use the . command to repeat the last change
  2. Remove extraneous, unnecessary movement (f.i. use I instead of 0i or A instead of $a)
  3. Use compound commands that leave you directly in insert mode instead of separate commands (f.i. use cw instead of dwi or C instead of d$i)
  4. Attempt to make changes repeatable

The dot command (.) lest us repeat the last change. It is the most powerful and versatile command in vim.

Miscellaneous tips

Run vim With Factory Settings

To run with with factory settings use the -u option to point vim to the following configuration file (vim -u essential.vimrc -N):

1
2
3
# essential.vimrc 
set notcompatible
filetype plugin on

Vim And The Command Line

To run a command-line command from vim use:

1
2
3
4
5
$ => execute this in external shell
: => execute this internally in command-line mode
/ => use command-line mode to perform a forward search
? => use command-line mode to perform a backward search
= => use command-line mode to evaluate a vim script expression

Interesting Tips from Vim’s Help

Jump to a subject: Position the cursor on a tag (e.g. |bars|) and hit CTRL-]. Use CTRL-T or CTRL-O to come back to the original position

Getting Specific Help With Context

It is possible to go directly to whatever you want help on, by giving an argument to the |:help| command. It is possible to further specify the context:

1
2
3
4
5
6
7
8
9

WHAT                    PREPEND     EXAMPLE  ~
Normal mode command     (nothing)   :help x
Visual mode command     v_          :help v_u
Insert mode command     i_          :help i_<Esc>
Command-line command    :           :help :quit
Command-line editing    c_          :help c_<Del>
Vim command argument    -           :help -r
Option                  '           :help 'textwidth'

Resources

  • Vim Help (in vim :help)
  • Vimtutor (in a shell $vimtutor)
  • Practical Vim

Comments