Vim
Help
:h[elp] <command>: Help for a specific command,:qto quit
Modes
Command Mode
- Use
TABkey to autocomplete file name from wherevimis run. - Use
Ctrl + Dto display options for the command
Set options
-
Options
-
List all available options
:h set-option -
boolean: on or off-
:se numberTurn on line numbers -
:se nonumberTurn off line numbers -
:se invnumberor:se number!Toggle line numbers -
:se number&Set option to default value -
:se number?Show current value of the option
-
-
number: an integer value (e.g.:help textwidth) -
string: a string value (e.g.:help 'backspace')
-
-
Backup
:se nobackup:se nowritebackup:se backupdir=$TEMP- Auto-backup your configuration files with Vim (opens in a new tab)
-
:se mouse=a -
Color Scheme
-
GitHub - flazz/vim-colorschemes (opens in a new tab)
Download
Vimcolor schemes intoVimconfig directorygit clone https://github.com/flazz/vim-colorschemes.git ~/.vim -
To see a list of ready-to-use themes
:colo[rscheme] <SPACE> <Ctrl + d> -
Change color scheme for the session
:colo[rscheme] <colorscheme_name> -
Change color scheme permanently
append
colo[rscheme] <colorscheme_name>into~/.vimrc -
Resources
-
Files
Undo all changes since last disk write
- Use
:e!
Open a file in terminal searching for a string
-
vim +/<string> filenameCursor at first occurrence of string
Open a file in terminal with particular options
-
vim +18 filenameCursor at a specific line
Diff text
vim -d -R 1.sql 2.sqlvim -d is equivalent to vimdiff
Output Redirection to Vim
vim <(diff 1.sql 2.sql) -RBash process substitutiondiff 1.sql 2.sql | vim -R -Using-to open a new buffer
Use vim <archive file> to view contents of an archive file
<archive file>could betar.gz,tgz,zip, etc.
Piping from stdin
docker inspect 547a7778d5f0 | vim -
Copy & Paste
-
Vim's unnamed/default register is now the system clipboard, which means if you yank some words in Vim, you can
Ctrl + vit somewhere. Reversely, if youCtrl + csome text, you canpthem in Vim as well. -
When using named registers, just insert
"<name of register>beforeywhen copying andpwhen pasting. Note the double quote before the name. For example:- Select the text you want to copy
- type
"<register_key>y(<register_key>must be a single character) - Move to where you want to paste it
- type
"<register_key>p
-
View current registers
:reg -
Paste in
NORMALmode"<register_key> -
Paste in
INSERTmode<Ctrl + r>+<register_key>
Navigation
Occurrence of character in the current line
t<character>moves cursor before the next occurrence of characterT<character>moves cursor after the next occurrence of characterf<character>moves cursor at the next occurrence of characterF<character>moves cursor at the previous occurrence of character;pushes the search forward,pushes the search backward
Navigate to the matching bracket
- The
%key can be used for the following:- To jump to a matching opening or closing parenthesis, square bracket or a curly brace:
([{}]) - To jump to start or end of a C-style comment:
/* */. - To jump to a matching C/C++ preprocessor conditional:
#if,#ifdef,#else,#elif,#endif. - To jump between appropriate keywords, if supported by the ftplugin file, eg: between begin and end in a Pascal program.
- To jump to a matching opening or closing parenthesis, square bracket or a curly brace:
Reload the current file
:e[dit]
Using tabs
-
Open file(s) in new tab(s)
-
From shell
vim -p <file...>Could open multiple files
-
In Vim
:tabe[dit] <file>Edit specified file in a new tab, one file only
-
-
Commands
-
List all tabs including their displayed windows
:tabs -
Go to next tab
gt / :tabn / Ctrl + PgDown -
Go to previous tab
gT / :tabp / Ctrl + PgUp -
on macOS, pageUp and pageDown might be missing
-
Custom key bindings
map <C-]> :tabn<CR>map <C-[> :tabp<CR>
-
-
Netrw
-
:E[xplore]Open file explorer (
Netrw) in the current pane -
:Le[xplore]Open file explorer in the left pane, and file explorer will open file in the right pane
-
EnterOpen file in the right pane
-
pPreview file in the right pane with focus still in file explorer
-
Ctrl + w + zClose file preview
-
%Create a new file
-
-
Resources
File system interaction
-
:e <file>Open a file in the current pane
-
Split
-
:sp <file>Open a file in a new
bufferand split windowhorizontally -
:vsp <file>Open a file in a new
bufferand split windowvertically -
Ctrl + w + (h / j / k / l)- move cursor to anotherpane(using the move direction key) -
Split resizing
Ctrl + w + [number] + "+"/"-"
-
-
List/Open recent files
:bro[wse] ol[dfiles]
Search & Replace
-
Settings
-
By default, searching is
case sensitive. -
:se ignorecaseTurn on case insensitive
-
:se smartcaseOnly works when
ignorecaseis set, automatically switch search tocase-sensitivewhen search query contains anuppercase letter -
:se incsearchShow match as search proceeds
-
:se hls[earch]Turn on highlighting for search match
-
-
Search
-
Before search
-
Search
forwardfor pattern/PATTERN -
Search forward for pattern in
case-sensitive(\C) orcase-insensitive(\c) mode, overriding default setting-
/PATTERN\Cor/\CPATTERN -
/PATTERN\cor/\cPATTERN
-
-
Search
backward?PATTERN -
very magicpatternAny
Vimsearch pattern can include the special sequence\v(very magic), and this will make every following character interpreted as specialregexsymbols (no escaping needed) except alphanumeric characters (a-zA-Z0-9and_).\vPATTERN -
no-magicpattern: Using\Vhas the opposite effect: all characters have their literal meaning and must be preceded by\to activate their special meaning./\VPATTERN -
Search whole word
Specify the word to be searched in the angle brackets, and the brackets must be escaped
/\<PATTERN\>
-
-
After search
n- repeat search forwardN- repeat search backward*- can be used to search the word under cursorg*- search for partial word under cursor (repeat withn)Ctrl + o, Ctrl + i- go through jump locations[I- show lines with matching word under cursor:noh- turn off highlighting until next search- Type
/and use⬆and⬇to access history
-
-
Replace
:%s/<search>/<replace>/gc%means search the entire file.- Alternatively,
%can be replaced with line range:8,10 - If neither
%nor8,10is supplied, only the current line is searched and only the first occurrence is matched. gmeans global, which tells Vim to replace every occurrence on a line, and not just the first occurrence.cmeans confirm, with which Vim will give you a prompt before replacing.
Terminal
Run a shell command without closing Vim
:!<shell command>
Open a terminal window
:ter[minal]
Paste register in terminal
CTRL-w+"+<Register>
Language support
-
Change syntax coloring
:se syntax=perl
Editing
-
Change case of characters
-
Normalmode- Toggle case
HellOtohELLowithg~then a movement (for this example,g~e) - Uppercase
HellOtoHELLOwithgUthen a movement (for this example,gUe) - Lowercase
HellOtohellowithguthen a movement (for this example,gue)
- Toggle case
-
Visualmode- Select text then press
~to toggle case, orUto convert to uppercase, oruto convert to lowercase.
- Select text then press
-
Resources
-
Vim Config
-
GitHub - mhinz/vim-galore (opens in a new tab)
A collection of notes, tutorials and guides on Vim
Neovim
LazyVim
-
Tutorial
-
Installation
# Install LazyVim g clone https://github.com/LazyVim/starter ~/.config/lazyvim # Remove the git history rm -rf ~/.config/lazyvim/.git # Activate LazyVim and launch Neovim NVIM_APPNAME=lazyvim nvim