Skip to content

Vi / Vim / Neovim🔗

General Informations🔗

There are several starting point for informations:

Motions🔗

See: :help motion

Left-Right Motions🔗

See: :help left-right-motions

  • 0 - move to first character in current line
  • ^ - move to first non-blank character in current line
  • $ - move to last character in current line
  • fchar - move to character char to the right
    • cf> - change up to next character > - including match
  • Fchar - move to character char to the left
  • tchar - move to before character char to the right
    • dt) - delete up to next character ) - excluding match
  • Tchar - move to after character char to the left
  • ; - repeat latest f, F, t, T movement
  • , - repeat latest f, F, t, T movement in opposite direction

Up-Down Motions🔗

See: :help up-down-motions

  • gg - go to first line in file
  • G - go to last line in file
  • countG - go to last count line in file
  • countgo - go to count byte

Word Motions🔗

See: :help word-motions

  • w - word forward
  • W - WORDS forward (WORDS are words with special characters)

Text Object Motions🔗

See: :help object-motions

  • ) - sentence forward
  • } - paragraph forward
  • ]] - section forward

Text Object Selection🔗

See: :help text-objects

  • commands starting with a selects an object with whitespaces
  • commands starting with i selects an inner object without whitespaces
  • objects
    • quotes: da" - delete around quotes or da'
    • brackets: di] - delete inside brackets
    • braces: ci{ - change inside braces
    • paragraph: dap - delete inside paragraph
    • word: vaw - visual select around word
    • WORD: vaW - visual select around WORD
    • tag: vit - visual select inside tag or vi<

Quickfix🔗

See: :help quickfix

The quickfix commands are used to work with a list of positions in files. A location list is a window-local quickfix list.

:vim word ** or :vim /pattern/ ** searches the word or pattern in all files recursively beneath the current directory. And opens the first location. After that the following commands are working on this quickfix list.

  • :copen - open the quickfix list
  • :cfirst / :clast - open the first resp. last location
  • :cnext / :cprev - open the next resp. previous location
  • :cc4 - open the fourth location
  • :cclose or :ccl - close the quickfix list
  • :colder / :cnewer - open the older resp. newer quickfix list (within the last ten)
  • :cdo cmd - execute cmd on each location
    • :cdo s/Foo/Bar/ - replace Foo with Bar at each location
    • :cdo s/Foo/Bar/ | update - store each buffer after executing the command
  • :cfdo cmd - execute cmd on each file in the quickfix list
    • :cdo bd - close all buffers from the quickfix list

Commands starting with g🔗

See: :help *g*

  • ga - print ASCII value of character under the cursor
  • gqmotion - format text described by motion
  • gwmotion - format text described by motion without cursor movement

Jump through or work with changes:

  • g; - jump to last change
  • g, - jump forward through the change list
  • :changes - show the last 100 changes
  • u - undo changes
  • <C-r> - redo changes

Misc🔗

Search in Key Mappings🔗

Besides the plugin Telescope the command :filter allows to search through all available key mappings.

Search for key mappings in insert mode:

:filter pattern imap

Search for key mappings in normal mode:

:filter pattern imap

To print the content of a mapping:

:verbose map <Leader>x

Clear Search Highlight🔗

With the option :hlsearch all matches of the last search are highlighted. To clear all highlights the command :nohlsearch can be used. Often this command is mapped to C-L. In NeoVim the following default mapping exists:

nnoremap <C-L> <Cmd>nohlsearch<Bar>diffupdate<Bar>normal! <C-L><CR>

Indenting Code🔗

With == or = code can be formatted.

  • == - indent current line
    • >> - increase current indentation level be one
    • << - decrease current indentation level be one
  • 4== - indent four lines (including current line)
    • V3j= - same indentation by using visual selection
  • =ap - indent current paragraph
  • gg=G - indent whole file
  • =% - when cursor is on { or } indent whole code block

Avoid Wrong Formatting During Paste🔗

Set the configuration :set paste before pasting into Vi. Disable this option afterwards with :set nopaste.

Remove Lines Matching a Pattern🔗

  • :g/pattern/d - remove lines that do match the pattern
  • :g!/pattern/d - remove lines that do not match the pattern

Non-Greedy Regular Expression🔗

See: :help non-greedy

{
  "key1": "valueA",
  "key2": "valueB"
}

The regular expression ".*" will select "key1": "valueA" because it is greedy. It tries to match as many as possible characters.

The regular expression ".\{-\}" will select only "key1". It will match the fewest number of characters as possible. The last \ is optional, so the expression ".\{-}" is also possible.