Tips
Help
:h[elp] <command>
: Help for a specific command,:q
to quit
Modes
Command Mode
- Use
TAB
key to autocomplete file name from wherevim
is run. - Use
Ctrl + D
to display options for the command
Set options
-
Options
-
List all available options
:h set-option
-
boolean
: on or off-
:se number
Turn on line numbers -
:se nonumber
Turn off line numbers -
:se invnumber
or: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
Vim
color schemes intoVim
config 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> filename
Cursor at first occurrence of string
Open a file in terminal with particular options
-
vim +18 filename
Cursor at a specific line
Diff text
vim -d -R 1.sql 2.sql
vim -d is equivalent to vimdiff
Output Redirection to Vim
vim <(diff 1.sql 2.sql) -R
Bash 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 + v
it somewhere. Reversely, if youCtrl + c
some text, you canp
them in Vim as well. -
When using named registers, just insert
"<name of register>
beforey
when copying andp
when 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
NORMAL
mode"<register_key>
-
Paste in
INSERT
mode<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
-
Enter
Open file in the right pane
-
p
Preview file in the right pane with focus still in file explorer
-
Ctrl + w + z
Close 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
buffer
and split windowhorizontally
-
:vsp <file>
Open a file in a new
buffer
and 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 ignorecase
Turn on case insensitive
-
:se smartcase
Only works when
ignorecase
is set, automatically switch search tocase-sensitive
when search query contains anuppercase letter
-
:se incsearch
Show match as search proceeds
-
:se hls[earch]
Turn on highlighting for search match
-
-
Search
-
Before search
-
Search
forward
for pattern/
PATTERN -
Search forward for pattern in
case-sensitive
(\C
) orcase-insensitive
(\c
) mode, overriding default setting-
/
PATTERN\C
or/\C
PATTERN -
/
PATTERN\c
or/\c
PATTERN
-
-
Search
backward
?
PATTERN -
very magic
patternAny
Vim
search pattern can include the special sequence\v
(very magic), and this will make every following character interpreted as specialregex
symbols (no escaping needed) except alphanumeric characters (a-zA-Z0-9
and_
).\v
PATTERN -
no-magic
pattern: Using\V
has the opposite effect: all characters have their literal meaning and must be preceded by\
to activate their special meaning./\V
PATTERN -
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,10
is supplied, only the current line is searched and only the first occurrence is matched. g
means global, which tells Vim to replace every occurrence on a line, and not just the first occurrence.c
means 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
-
Normal
mode- Toggle case
HellO
tohELLo
withg~
then a movement (for this example,g~e
) - Uppercase
HellO
toHELLO
withgU
then a movement (for this example,gUe
) - Lowercase
HellO
tohello
withgu
then a movement (for this example,gue
)
- Toggle case
-
Visual
mode- Select text then press
~
to toggle case, orU
to convert to uppercase, oru
to 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