Skip to content

vim

Unlike WYSIWYG editors which optimize input for writing text, vim optimizes for editing it. Vim offers a composable language for expressing these editing changes whose syntax can be composed into two elements, operations and text objects, which are analogous to verbs and nouns in language. YouTube

The framework of understanding vim's syntax as a language appears to date back to an influential 2011 Stack Overflow post.

On Unix-derived operating systems the main config file for Vim is placed at $HOME/.vimrc. On Windows it is placed at $HOME/_vimrc.

Syntax

normal

Use :normal to define a series of normal-mode commands

Select all lines of a buffer
:normal ggVG

Keybindings

There are two kinds of keybindings in Vim

  • Recursive using command words map, nmap, vmap, etc. In these keybindings, the mapping itself is interpreted.
  • Nonrecursive

There are two types of keycodes:

  • Vim keycodes which are identifiable as being in angle brackets: <Space>, <Return>, etc
  • Terminal keycodes that appear similar to ^[[1;2A. These may or may not be identifiable with the keycodes which the Linux kernel maps to raw keyboard scancodes. [ref][archwiki:Keyboard_input]

The leader key is used to create more complicated keybindings using any arbitrary keypress, for example using , or <Space>.

let mapleader = ' '

Autocommands

Autocommands expose an API that allows handling editor events like BufNewFile, BufReadPost, BufWritePost, BufWinLeave, and especially to implement functionality specific to filetypes.

Highlight added lines in green and removed lines in red in .diff files

filetype on

augroup PatchDiffHighlight
  autocmd!
  autocmd FileType diff syntax enable
augroup END

Turn syntax highlighting on only for certain filetypes

augroup PatchDiffHighlight
  autocmd!
  autocmd BufEnter *.patch,*.rej,*.diff syntax enable
augroup END

Color

; Change the color of ELEMENT
highlight ELEMENT ctermfg=COLOR ctermbg=COLOR guifg=#abc123 guibg=#abc123

; Select alternative colorschemes
:colo[rscheme] <tab>

; Display all available colorschemes
:colo <C-d>

Clear custom color commands

:highlight clear
:hi clear
Set file format to Unix/DOS
:set fileformat=unix
:set fileformat=dos

Completion

Tasks

Invoking to a specific line number

# Open with cursor at line 13
vim .bashrc +13

Configuration

Configuration
" Prevent vim from creating backups files
set nobackup

" Set relative line numbers
set rnu

Search and replace

" Replace foo with bar across all lines, wherever they occur
%s/foo/bar/g

Mapping keys

Map Alt+J and Alt+K to move lines of text up or down
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv

Yanking STDOUT

To run a shell command from the normal mode command line, you simply run the ! ("bang") command in normal mode.

:!env

However to store the output of that command into a register, you must run a command like the following, which stores the output of the shell command into the a register.

:let @a = system('env')
The register signified by @" will be placed into the buffer by the put command (p).
:let @" = system('env')

Alternatively

:put =system('env')

Filetype-associated settings

Set indentation behavior specific to YAML
autocmd FileType yaml setlocal ai ts=2 sw=2 et

Plugins

Vim 8 supports native loading of plugins (put in $HOME/.vim/pack/start/

vim-plug is a popular plugin manager.

Install a plugin to provide Rust language support

Plug 'rust-lang/rust.vim'

Mouse support

From here
set mouse=a

Language definition

Syntax highlighting for various languages are stored in syntax files, stored in /usr/share/vim/vim82/syntax.

Defining highlighting for pymdownx snippets

syn match markdownPymdownxSnippet '^-\{2,}8<-\{2,} .*' " (1)
hi def link markdownPymdownxSnippet Error
  1. Note that the quantifier specifying at least two instances of the preceding hyphen requires the initial brace to be escaped. However, the open angle bracket does not.