Skip to content
Marcel Schmalzl edited this page Nov 20, 2024 · 13 revisions

Vim

Load/exit/save

  • Exit: :q
    • If there are changes you want to discard - exit without saving: :q!
  • Save: :w
    • Exit and save: :wq
  • Reload: :e / :edit (reloads the current file)
    • :e!: discard current changes and reload

Navigation

  • Jump in line to
    • beginning: ^
    • end: $
  • Alternative to arrow keys ("vim"-way to navigate because you don't have to move your hand)
    • h: ←
    • j: ↓
    • k: ↑
    • l: →
  • Move to word:
    • next: w (W after the whitespace) or Shift + <arrow key>
    • previous: b (B before the whitespace)
  • Move/scroll screen 1/2 page
    • up: Ctrl/Control + u
    • down: Ctrl/Control + d
  • Move to beginning of file: :0
  • Move to end of file: :$

INSERT mode

Enter INSERT mode with:

  • At current position of the cursor: i
  • After the current position of the cursor: a

Exit: ESC

General purpose commands

  • Repeat last command: .
  • Delete character (in normal mode): x
  • Convert tabs to spaces: :retab (note that the .vimrc option expandtab does not apply for existing tabs)

Line numbers:

  • Absolute numbers: :set number (or: :set nu)
  • Relative numbers: :set relativenumber (or :set rnu)
  • Hybrid mode (relative and current line is absolute): :set number relativenumber (or: :set nu rnu)
  • Disable with :set nonu nornu or :set nonumber norelativenumber (absolut + relative = hybrid)

Undo/Redo

  • List changes: :undolist
  • Undo: u
    • Undo last n changes: 2u (undo last 2 changes)
  • Redo: Ctrl + r (on Mac use control instead of Ctrl)

For more commands see: https://www.cyberciti.biz/faq/vim-undo/

Line operations (cut/copy/paste)

  • Cut line: dd
  • Cut n lines: ndd (3dd to delete 3 lines)
  • Cut until
    • beginning of line: d^
    • end of line: d$
  • Yank/copy line: yy
  • All mentioned earlier for cut works for yank as well
  • Paste: p (after current position/line); P (before current position/line)
    • Paste, writes the buffers from cut or yank

Character/word manipulation

  • Cut word under cursor: diw (delete in word)
  • Yank word under cursor: yiw (yank in word)

Indentation

  • Indent current line: >>
  • Unindent current line: <<
  • Indent multiple lines: 5>> (indents 5 lines by one level)

Visual block mode

Enter VISUAL BLOCK mode by hitting: Ctrl/Control + v

Multi-line edit

  1. Place cursor
  2. Enter visual mode
  3. Navigate to select multiple lines/characters/... (via hjkl or <arrow_keys>)
  4. Press I to get into INSERT mode (note i will not work)
    • Alternatively use other commands like deletion (e.g. 4d, ...)
  5. Add the text you want to insert (you'll see it only for the first line)
  6. <ESC> to finish (only now you'll see the changes on all lines)

Vim registers

Vim works with registers storing (temporarily) information for text processing automatically or by user interaction.

  • View currently assigned registers and their content: :reg
  • Pasting from a register is as simple as "0p (in command mode)
    • You will only see the paste after hitting p (no leading :)
Register cmd Description
"" Unnamed reg; default for yank, delete, ... (d, D, x, X, c, C, s, S)
"0 Default reg: default for yank
"1 Slot 1 in register stack (will get the value of "0 after next yank, ...)
... ...
"9 Slot 9 (last slot)
"a Named register a (possible values a-z)
"_ Black hole register ()
". Last inserted text
": Last exec command
"% Name of current file
"/ Last search text
"* Current clipboard content

Notes:

  • For a yank "" and "0 have the same content
  • p will paste from the default register ("")
  • Registers persists session-wide
  • Delete a line without adding it to a register: "_dd
  • Clearing a register: :let @a=@_ (assign the black whole register (_) to register a)
  • Put (store) to register "4yy (yank line to register 4)
  • Append to register (only a-z): Use capital letters (A-Z)

Search & Substitution

Search

  • Fwd: /keyword

  • Bckw: ?keyword

  • Repeat (same direction): n

  • Repeat (opposite direction): N

  • Stop text highlighting from search: :noh

Substitution

  • Search/replace globally in file (-> *): :%s/searchPattern/replacePattern/g

~/.vimrc

Useful options:

" Activate syntax highlighting
syntax on
" Set relative/absolute line numbers (here: hybrid mode = both)
set number relativenumber
" Disable with in vim session via `:set nonu nornu` or `:set nonumber norelativenumber`

" Set tab widths
set tabstop=4     " (ts) width (in spaces) that a <tab> is displayed as
set shiftwidth=4  " (sw) width (in spaces) used in each step of autoindent (aswell as << and >>)
" Replace tabs with spaces and set tab width
set expandtab     " (et) expand tabs to spaces (use :retab to redo entire file)

""" File specific indendation settings
" for yml files, 2 spaces
autocmd Filetype yml setlocal ts=2 sw=2 expandtab
autocmd Filetype yaml setlocal ts=2 sw=2 expandtab
" ts = number of spaces for tab
" sts = number of spaces for tab while editing
" sw = number of spaces for auto-indent
" -> https://vimdoc.sourceforge.net/htmldoc/quickref.html#option-list

" Compensate for typo `:Wq` instead of `:wq` ans silently do the right thing (-> save and exit)
cnoreabbrev <expr> Wq ((getcmdtype() is# ':' && getcmdline() is# 'W' && getcmdline() is# q)?('Wq'):('wq'))

Further resources


vimdiff

Provides a kdiff3-alike 3-way merge tool within your terminal (see here for an example.

Colors

Line backgrounds:

  • Red/magenta: partial match
    • Magenta: common text
    • Red: differences
  • Blue: additional text

Shortcuts:

  • Cycle windows: Ctrl + w
  • Jump to next diff: [c
  • Jump to previous diff: ]c
  • Select diff code from:
    • local: :diffget LO
      • = theirs = local changes (current branch) (merge)
      • = ours = upstream (rebase)
        • Explanation about the role inversion in rebase vs merge: "Note that a rebase merge works by replaying each commit from the working branch on top of the branch." (from git man).
    • base: :diffget BA
      • = common base
    • remote: :diffget RE.
      • = ours = changes from remote/upstream (merge)
      • = theirs; local changes from your branch (rebase)
    • If you want to edit yourself select a version from above first and edit as usual.
  • Save and exit: :wqa (a for quitting all windows (3 diffs + editor)
Clone this wiki locally