Vim
Comands
- Replace tabs with spaces:
:retab
- Make and source session file to restore vim state.
- Make (create):
:mks session.vim
- Source (restore):
vim -S session.vim
or:source session.vim
- Make (create):
- Change case:
gU
(to uppercase) orgu
(to lowercase) followed by a movement. :noh
clear highlighting.:g/^/m0
reverse all lines./[^\x00-\x7F]
find non-ascii characters.- Multiline editing:
C-v
Visual-block mode; Highlight area;I
Multiline Insert. Can also highlight multiple lines and then user:normal
command to apply normal commands to the selection. i
Switch to edit mode, insert before cursor (a
Insert after cursor;I
Start of line;A
End of line;o
Insert new line after;O
Insert new line before).:sort
Sort all or Visual selection.:[r]s/foo/bar/[a]
Replace. (e.g.:%s/foo/bar/g
replace all 'foo' with 'bar'); Add 0 prefix to all lines beginning 2-9::%s/^[2-9]/0&/g
; Replace double-space with tab:%s/ /\t/g
. Remove carriage returns (^M
):%s/^M//g
(UseC-Q
C-M
to enter^M
).[r]
Range:nothing
Current line;number
Line to change;%
Entire file.[a]
Arguments:nothing
Replace first occurrence;g
Replace all occurrences;i
Ignore case;I
Don't ignore case;c
Confirm (y
,n
to skip this match,a
All,q
Quit substitution).s
uses\r
to represent newline character. To put a new line before "rate"::%s/"rate"/"tier_from": "0",\r "rate"/g
- Activate spell checker (en-uk by default):
:set spell
. Usez+=
in command mode to get correction suggestions. - Disable smart indentation:
:set paste
; Re-enable::set paste!
. - Vim help: Follow link with
C-]
. Find links with/](.\{-}](
- Multiple Files:
- Open multiple files at once:
vi *.json
- Show current buffers:
:ls
- Add another file:
:arga
(or remove/delete one with:argd
) - Run command on all files:
:argdo [command]
(e.g. change cmd to command and save withargdo %s/cmd/command/ge ]( w
) - Tabs:
:tabe [filename]
New tabWith;gt
Next tab;gT
Previous tab. - Split screen:
:sp [filename]
Open new split;C-w
thenw
For next split;W
for previous split.
- Open multiple files at once:
.
Replay last command. For more than one command use macros.- Recording Macros:
q
Start macro-recording. Next0-9a-zA-Z
is macro/buffer name (q1
records the macro to buffer "1").q
again ends the recording. Replay the macro at any time using@1
. - Convert a dos/windows file (with CRLF/x0d0a line endings) to unix (Check file format with
:set ff? ffs?
)::e ++ff=dos
Edit as dos file (if not doing so already).:setlocal ff=unix
:w
- Delete all lines:
- Which do not contain
{{
::v/{{/d
. Variant of:[range]v[global]/{pattern}/[cmd]
. - Matching pattern:
:g/[pattern]/d
(e.g.:g/@login_required/d
). - Which do not begin with
-
::g!/^[-]/d
- Which do not contain
- Save readonly file as root:
:w !sudo tee %
. - Command history: Enter
:
or/
and use up and down arrows to navigate history. Command line window: Typeq:
for commands, orq/
for searches;C-c C-c
to exit. History is stored in~/.viminfo
, if no history is found then check you have update access to that file as it may have been created byroot
. /
Search forwards;?
Search backwards;n
Next match;N
Previous match.:%s/[pattern]//gn
Count matches in file.*
Search for word under cursor (#
Reverse search).:g/[pattern]/#
Find all occurrences.:browse old
List recently opened files.u
Undo the last action (U
Undo all the latest changes that were made to the current line;C-r
Redo).v
Select block by character (V
By line;C-v
Block visual mode).- On selected text:
~
Changecase. (>
Shift right;<
Shift left;c
Change the highlighted text;cc
Change line;y
Yank;yy
Yank line;d
Delete;dd
Delete line;p
Paste after;P
Paste before;"_d
Delete and place in black-hole_
register to reserve what had been yanked before). - In Edit mode:
C-t
Increase indent;C-d
Decrease indent. 0
Start of line;^
First non-blank character of the line;w
Next word;W
Next word, ignore punctuation;e
Word-end;E
Word-end, ignore punctuation;b
Word-beginning;B
Word-beginning, ignore punctuation;ge
Previous word-ending;gE
Previous word-ending, ignore punctuation;g_
Last non-blank character of the line;$
Last character of the line.G
Go to end of file;:1
Start of file;:[n]
Line number;H
First line of the screen;M
Middle line of the screen;L
Last line of the screen.:![command]
Run external command::![command] %
Filter file through external command.:r![command]
Insert command output.:%!xxd
Run file through hexdump filter (:%!xxd -r
Convert dump from hex back to binary).
:e .
File explorer (:Sex
Open explorer in new window):set nu
Show line numbers;:set nu!
Hide line numbers.
Completion
While you are typing, it's pretty common to use the same word over and over again. Using C-p
Vim searches the currently typed text backwards for a word starting with the same characters as already typed. C-x C-l
completes the whole line.
If you're not sure how to type some word and you've enabled spell-checking (:set spell
), you can type C-x C-k
to do a dictionary lookup for the already typed characters.
Use ^n
and ^p
to go back and forth in the suggestion list:
^x^n
for JUST this file^x^f
for filenames^x^]
for tags only^x^l
whole line^n
for anything specified by thecomplete
option
Buffers and Windows
:b [N]
Switch to buffer[N]
.:ls
or:buffers
Show buffer list. Explanation:%
Current window.#
Alternate buffer (switch using:e#
or:b#
).a
Active (loaded and visible).h
Hidden (loaded but not visible).+
Modified.
:bd
Unload the buffer and remove it from bufferlist (don't close Vim, even on the last buffer).:bun
unload the buffer but stay in bufferlist.:sp [N]
split current window and edit buffer[N]
.:w
Write the current buffer to disk.:e file
Load a file from disk.:q
Closes current window (and Vim if it's the last one).:new
New empty window.:on
close all windows but the active one (C-w o
).C-w
andh j k l
Move between windows.C-w =
equalise window sizes.
Marks
Vim automatically sets various marks like
0-9
Last 10 positions of closed files (0
the last,1
the last but one).<
and>
Left and right position of marked texts.(
and)
Start or end of the current sentence.{
and}
Start or end of the current paragraph.[
and]
First or last character of the last yanked or changed text..
Last change.'
or`
Position before the last jump."
Position before the last exit of the file (local to a file).^
position of the last insert-stop.
To set a manual mark, use m[a-zA-Z]
. To jump to one of the marks (manual or automatic) you can choose between '
and `
:
'
Sets the cursor to the first non-blank character in the marked line.`
Sets the cursor to the exact position where the mark was set.
There is a little difference between lower-case and upper-case characters:
a-z
Are local to a file.A-Z
Are stored and available over sessions (associated with a file).
You can use L
for your work-log and T
for your time-table for example, and quickly update the information there.
For example you can jump to the last known position of a file before it was closed by typing `"
(it's easy to configure Vim to do it automatically at start).
To get a list of all marks Vim knows about type :marks
. To delete marks use :delmarks
(:delmarks a b c
removes marks a
and b
and c
, to delete all marks use :delmarks!
).
vimdiff
vimdiff file1 file2
highlights differences between files.
Commands
]c
jump to next change ([c
previous change).:diffget [buffer_id]
replace current change with equivalent in other buffer.C-W C-W
switch to the other buffer.:diffupdate
refresh display.:colorscheme evening
change colours.:ls
list buffers.zo
open folded text.zc
close folded text .:syntax off
- If using only two buffers:
do
get change from other buffer.dp
put the changes from current buffer into the other.