Vim

| Comments

Contents:

Docs & Articles #

Important:

Other:

Setup #

Installing the Python provider #

Some plugins require the Python provider to be available. This can be checked with the following vim command:

:checkhealth provider

This requires the pynvim Python package. It’s best if the setup uses virtualenv. On macOS prefer to use pyenv:

brew install pyenv pyenv-virtualenv

This needs the following initialization code in ~/.zshrc:

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Then, install a Python 3 version:

pyenv install 3.10.6

Create a new virtualenv:

pyenv virtualenv 3.10.6 neovim

Activate it temporarily in your shell session:

pyenv activate neovim

Install the required Python package:

pip install pynvim

Then add this to ~/.config/nvim/init.vim:

let g:python3_host_prog=expand('~/.pyenv/versions/neovim/bin/python')

Random tips #

Documentation #

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

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.

Record macros #

  • to record macros: q<register><commands>q
    • pressing qa starts recording in register a
    • pressing q again stops recording
  • to view recorded macros: :reg
  • to play the macro once: @<register>
    • @a plays the macro in register a
  • to repeat the macro execution: @@

LazyVim setup #

The following tips assume LazyVim.

Shortcuts #

Search for files:

<leader><leader>

Search text in files:

<leader>sg

View list of errors and warnings reported in project:

<leader>xx

Switch to window:

Ctrl-w + h/j/k/l

Close current window:

Ctrl-w + q

Open link at cursor:

gx

Go to definition at cursor:

gd

Go back:

Ctrl-t