NeoVIM


Help

  • :h <search>
  • also works with keymaps,
    • :h ^n to search CTRL-N purpose in normal mode
    • :h i_^n to search CTRL-N purpose in insert mode
  • :helpgrep <search> search through everything in the vim manual

ZZ and zz

vim has 3 modes:

  • insert mode: you write text as if in normal text editor
    • use the i button to switch to this mode
    • use the a button to switch to this mode and move the cursor to the next character
    • use the o button to switch to this mode and add a newline
  • normal mode: provides efficient ways to navigate and manipulate texts
    • use the Esc button to switch to this mode
  • visual mode: select text using movement keys before deciding what to do with it
    • use the v button to switch to this mode
    • use the shift+v buttons to select lines instead of characters

VIM movement

In normal mode, you can navigate by using the arrow buttons, but vim has been thought for productivity. So instead of using the arrow buttons, and thus moving your right hand, vim has a set a buttons for which you won’t need to move your palms and be able to reach every functionnalities vim has to offer.

So use the following to navigate in your file:

  • h: move left
  • j: move down
  • k: move up
  • l: move right
  • H: move to top of screen
  • M: move to middle of screen
  • L: move the bottom of screen

You can also move by words:

  • w: moves to the start of the next word
  • e: moves to the end of the word
  • b: moves to the beginning of the word

If you want to navigate quickly in your file:

  • ctrl+d: page down
  • ctrl+u: page up
  • gg: go to the first line
  • G: go to last line of file
  • gj / gk: move cursor up and down to wrapped part of a line as if it’s a different line
  • g0 / g$: same as previous but move cursor to the first and last letter of a wrapped line
  • gq: turn a long line into multiple lines
  • gu / gU: uncapitalize / capitalize words / lines
  • g~: switch capitalization of a letter
  • gf: open highlighted text as file
  • gv: re-select previous selected text
  • gJ: conjoining line without leaving spaces
  • g&: rerun substitute command for all lines
  • shift+g: go to the last line
  • <number>g: go to line “number”
    • 5g: go to line 5
    • 10g: go to line 10
  • 0: move cursor to the beginning of the line
  • $: move cursor to the end of the line
  • *: find next occurrence of the word under the cursor
  • #: find previous occurrence of the word under the cursor
  • %: go to matching parenthesis/brackets

VIM editing

You don’t have to be in insert mode to edit text. You can also edit portion of your file in normal mode.

  • x: delete the character under the cursor
  • X: delete the character to the left of the cursor
  • r: replace only one character under the cursor
  • cw: remove character until the end the word and switch to insert mode
  • c$: remove character until the end of the line and switch to insert mode
  • dw: delete the first word on the right side of the cursor and copies the content
  • dd: delete the line and copies the content
  • yw: copy word
  • yy: copy line
  • p: paste the copied content
  • .: repeat the previous command

Search and replace

Still in normal mode:

  • /: search from top to bottom
  • ?: search from bottom to top

Use n and N to search next and previous occurrence, respectively.

To replace, you can use the same syntax as sed command:

  • :s/wordtoreplace/replacedword/: only replace the first instance of “wordtoreplace” of the line where the cursor is
  • :%s/wordtoreplace/replacedword/: apply to the entire file
  • :%s/wordtoreplace/replacedword/g: apply to all instances

Other VIM commands

In normal mode:

  • :w: save file
  • :q: quit file edition
  • :q!: force quit
  • :wq: save and quit file edition
  • :x: save and quit the file edition
  • :set invnumber: display/hide line numbers
  • :set invpaste: format/do not format copied content (useful when copy pasting formatted code)

In insert mode:

  • press the ctrl+n for autocompletion

Undo

You can undo the last action by pressing the u button in normal mode.

If you want to undo the undo, you can press the ctrl+r buttons.

Going further with VIM

I did not cover everything, only what’s needed to be able to perform basic edition on files using VIM.

There are lot of tutorials out there: