Vim commands

From wikinotes

Documentation

:h cmdline https://neovim.io/doc/user/cmdline.html
:h wildmenu https://neovim.io/doc/user/options.html#'wildmenu'
:h index (builtin commands) https://vimhelp.org/index.txt.html#index
:map (user/plugin commands) n/a

Command Mode

  • : enters the command mode interpreter
  • ctrl-f from within command mode opens command/history in a buffer
:                       "enter commandmode
<c-f>                   "edit command in buffer (w/ vim navigation etc)

!<command>              "run shell command
:<command>:<command>    "each command-mode command is prefixed by a ':'
:%s/search/replace/gc   "search/replace text in entire file (confirming before replacing for each occurrence)
:%s/search/replace/g    "search/replace text in entire file
0-9<command>            "prefix a command with a number to perform command N times
u                       "undo
<c-r>                   "redo

Command Completion

Command mode completion controlled by the wildmenu.
See :h wildmenu.

Hotkeys

CTRL-Y, SPACE   " accept completion
CTRL-E          " abort completion

CTRL-N, RIGHT   " prev completion
CTRL-P, LEFT    " next completion

" if completing filepaths
UP              " complete parent dir
DOWN            " complete child dirs

Configuration

set ignorecase  " ignore case when completing vim-commands

Commands

Search

vim[grep]

vimgrep lets you search files

:vim[grep] test %   " find all occurrences of 'test' in current file (%)

:copen              " list occurrences
:cnext              " jump to next occurrence
:cprev              " jump to prev occurrence

grep

grep lets you search with the cli grep (faster).

:grep searchterm **/*.md                                    " search files glob-matched
:grep searchterm `find . -type f '*.rb' -not -path 'test/*'` " search files provided by `find`

Or directly cli grep to create repors (cannot jump results)

:!grep '^[a-zA-Z].*{' % | awk -F' ' '{ print $1 }' | sort | uniq

cfdo

cfdo will execute a command on all files in the quickfix list (:copen).

" cfdo execute in each file in quickfix
" (ex: search-replace all sourcefiles in project)
:grep foo
:cfdo %s/foo/bar/g | write

" cdo execute for each match in quickfix
" (ex: unfold all modules in ruby file)
vim '\(module \|class \)' % | cdo :normal zo