Vim

| comments

Contents:

Docs & Articles #

Important:

Other:

Random tips #

Documentation #

  • :help
  • :help g
  • :help motion.txt
  • :help spell.txt
  • :help user-manual
  • :help visual.txt

Shell setup #

For Zsh:

set -o vi

For Bash:

bindkey -v

Indicating the editing mode via the prompt in Zsh:

export BASE_RPROMPT="$RPROMPT"

zstyle ':vcs_info:git:*' formats '%F{240}%b %f %F{237}%r%f'
zstyle ':vcs_info:*' enable git

function zle-line-init zle-keymap-select {
    RPS1="%B%F{237}${${KEYMAP/vicmd/--NORMAL--}/(main|viins)/--INSERT--}%f%b $BASE_RPROMPT"
    RPS2="$RPS1"
    zle reset-prompt
}

zle -N zle-line-init
zle -N zle-keymap-select

Surround text #

Functionality from vim-surround, which also works in VS Code (with VSCodeVim):

v                    # Enter visual mode
<visually select>    # Use the keyboard to select the section of text
s                    # Press upper case S
"                    # Specify what you want to surround the visual selection with

Visual-block mode #

This works like (and implemented with) multi-cursors in other editors (see VS Code (archive)).

  • Ctrl-v to enter visual-block mode
    • Select block
  • I (capital letter) to enter insert mode
  • <Esc> to exit insert mode

Copy & Paste #

Shortcuts:

  • d is for cut, y is for copy, p or P are for paste

Normally one uses the clipboard register to cut, copy and paste, which is *. So "*y copies to the system clipboard, and "*p pastes from that clipboard.

To activate the system clipboard for copy and pasting with the “unnamed” register, add this to .vimrc:

set clipboard+=unnamed

Fold / Unfold #

  • zc: fold
  • zo: unfold
  • zM: fold all
  • zR: unfold all

Go to … #

  • gd: go to definition
  • gx: open link
  • Ctrl+O: jump back to last position

Vimdiff #

  • do: Get changes from other window into the current window
  • dp: Put the changes from current window into the other window.
  • ]c: Jump to the next change.
  • [c: Jump to the previous change.
  • Ctrl W + Ctrl W: Switch to the other split window.