Skip to content

Latest commit

 

History

History
231 lines (155 loc) · 7.29 KB

File metadata and controls

231 lines (155 loc) · 7.29 KB

LEARN VIM PROGRESSIVELY

The following text is based on the tutorial Learn Vim Progressively that can be found here

1ST LEVEL:

Vim starts by default in NORMAL mode. Use i to change to INSERT mode. To get back to NORMAL mode just press the ESC key.

  • i -> Go to INSERT mode. Inserts in the place where the cursor is
  • ESC -> Back to NORMAL mode
  • x -> Delete the character under the cursor
  • :wq -> Save (:w) and Quit (:q)
  • dd -> Delete (and copy to clipboard) the current line
  • p -> Paste what is in clipboard
  • hjkl -> Move Left, Down, Up, Right (j looks like a down arrow)
  • :help <command> -> Show help about command. :help provides general help

Note: The key for productivity in Vim is to stay most of the time in NORMAL mode, and use from there all the available commands to modify text and move around.

2ND LEVEL:

  1. Insert mode variations:

    • i -> insert IN the cursor
    • a -> insert AFTER the cursor
    • I -> insert at the BEGINNING of the current line
    • A -> insert at the END of the current line
    • o -> insert a new line AFTER the current one
    • O -> insert a new line BEFORE the curren one
    • cw -> replace from the cursor to the end of the word
  2. Basic moves:

    • 0 -> (Zero) go to the first column of the line
    • $ -> go to the end of the line
    • ^ -> go to the first NON-BLANK character of the line
    • g_ -> go to the last NON-BLANK character of the line
    • /pattern -> search for pattern
  3. Copy/Paste:

    • P -> paste BEFORE current position
    • p -> paste AFTER current position
    • yy -> copy the current line
  4. Undo/Redo:

    • u -> undo
    • Ctrl-r -> redo
  5. Load/Save/Quit/Change File (Buffer):

    • :e <path/to/file> -> open path/to/file
    • :w -> save
    • :saveas <path/to/file> -> save to path/to/file
    • :wq, :x or ZZ -> save and quit (:x only save if necessary)
    • :q! -> quit without saving
    • :qa! -> quit without saving even if there are modified hidden buffers
    • :ls -> show all the files (buffers) currently opened
    • :bn -> show NEXT file (buffer)
    • :bp -> show PREVIOUS file (buffer)
    • :bd -> close current buffer and delete it from buffer list

3RD LEVEL:

  • . -> (dot) will repeat the last command

  • N<command> will repeat command N times

    For example:

    • 100idesu [ESC] -> will write "desu " 100 times
    • . (dot) -> just after last command will write again "desu " 100 times
    • 3. -> will write "desu " 3 times (and not 300 times)
  • NG -> go to line N

  • gg -> go to start of file (shortcut for 1G)

  • G -> go to last line of file

Mind that 'words' are composed of letters and underscore character, while 'WORDS' are a group of characters separated by blank characteres. Thence, in the following string:

    (name_1,vision_3); # this is a comment.

the first 'word' is name_1, while the first 'WORD' is (name_1,vision_3);

  • w -> go to the START of the following 'word'

  • e -> go to the END of this 'word'

  • W -> go to the START of the following 'WORD'

  • E -> go to the END of this 'WORD'

  • % -> go to the correspondig (, {, [

  • * -> go to the NEXT occurence of the word under the cursor

  • # -> go to the PREVIOUS occurence of the word under the cursor

Most commands can be used using the following general format:

    <START-position><COMMAND><END-position>

For example, 0y$ means:

  • 0 -> (Zero) go to the beginning of this line
  • y -> yank from here...
  • $ -> ...up to the end of this line

Other examples are:

  • ye -> yank to the end of this word
  • y2/foo -> yank up to the second occurence of foo

4TH LEVEL:

  1. General:

    • fX -> go to the next occurence of character X on this line

      For instance:

      • 3fX -> find the 3rd occurence of character X in this line

      • F -> like f, but backwards

      • ; -> find the NEXT occurrence

      • , -> find the PREVIOUS occurrence

    • tX -> go to just BEFORE the character X

      • T -> like t, but backwards
    • dtX -> remove everything up to X, without including X

  2. Zone selection:

    This follows the patterns:

    <action>a<object>  and  <action>i<object>

Where action can be d (delete), y (yank), v (select in visual mode),etc. The object can be: w (word), W (WORD), s (sentence), p (paragraph), but also natural characters like ", ', ), }, ].

Let's suppose the cursor is on the first 'o' of (map (+) ("foo")):

  • vi" -> will select foo
  • va" -> will select "foo"
  • vi) -> will select "foo"
  • va) -> will select ("foo")
  • v2i) -> will select map (+) ("foo")
  • v2a) -> will select (map (+) ("foo"))
  1. Rectangular blocks:

    They are very useful for commenting many lines of code. For instance:

    ^<Ctrl-v><Ctrl-d>I-- [ESC][ESC]
  • ^ -> go to the first non-blank character of the line

  • Ctrl-v -> start of block selection (ATTENTION!, it is CTRL!!!)

  • Ctrl-d -> move down (could also be jjj, %, etc.)

  • I-- [ESC][ESC] -> insert -- at the beginning of each line

  • o -> in Visual Block Mode, toggles selection cursor to OPPOSITE corner

    Note #1: Instead of I we can use i, A, a, c, r, etc.

    Note #2: Don't confuse Ctrl-v (Visual Block Mode) with V and v modes

To add something at the end of all visually selected lines:

  • Ctrl-v -> gets into Visual Block Mode
  • go to the desired line (with j, Ctrl-d, /pattern, etc)
  • $ -> go to the end of the line
  • A + write new text + ESC

A fabulous video about the Visual Block Mode can be found at Vimcasts.

  1. Completion:

    • Ctrl-p -> call the autocompletion feature
  2. Macros:

    • qa -> records your actions in register 'a'
    • @a -> this will call the macro saved in register 'a'
    • @@ -> calls the last executed macro

    Example: On a line containing only the number 1, type:

    • qaYp<Ctrl-a>q
      • qa -> starts recording the macro
      • Yp -> duplicates this line
      • Ctrl-a -> increments the number
      • q -> stops recording the macro
    • @a -> writes 2 under 1
    • @@ -> writes 3 under 2
    • 100@@ -> writes increasing numbers up to 103
  3. Visual selection: v, V and Ctrl-v

    Once the selection has been made:

    • J -> joins all the lines together
    • < -> indent to the left
    • > -> indent to the right
    • = -> autoindent
  4. Splits:

    • :split -> create a (horizontal) split (shorthand: :sp)
    • :vsplit -> create a vertical split (shorthand: :vs)
    • Ctrl-w <direction> -> (where direction is hjkl) to change split
    • Ctrl-w_ -> Maximise the size of the (horizontal) split
    • Ctrl-w | -> Maximise the size of the vertical split
    • Ctrl-w + -> Grow the split
    • Ctrl-w - -> Shrink the split
  5. The manuals:

    • :help usr_01.txt -> Start the User Manual
    • Ctrl-] -> jumps to the subject under the cursor
    • Ctrl-o -> jumps back