-
Notifications
You must be signed in to change notification settings - Fork 0
vim
Marcel Schmalzl edited this page Nov 20, 2024
·
13 revisions
- Exit:
:q
- If there are changes you want to discard - exit without saving:
:q!
- If there are changes you want to discard - exit without saving:
- Save:
:w
- Exit and save:
:wq
- Exit and save:
- Reload:
:e
/:edit
(reloads the current file)-
:e!
: discard current changes and reload
-
- Jump in line to
-
beginning:
^
-
end:
$
-
beginning:
- Alternative to arrow keys ("
vim
"-way to navigate because you don't have to move your hand)-
h
: ← -
j
: ↓ -
k
: ↑ -
l
: →
-
- Move to word:
-
next:
w
(W
after the whitespace) orShift
+<arrow key>
-
previous:
b
(B
before the whitespace)
-
next:
- Move/scroll screen 1/2 page
-
up:
Ctrl/Control
+u
-
down:
Ctrl/Control
+d
-
up:
- Move to beginning of file:
:0
- Move to end of file:
:$
Enter INSERT mode with:
- At current position of the cursor:
i
-
After the current position of the cursor:
a
Exit: ESC
- Repeat last command:
.
- Delete character (in normal mode):
x
- Convert tabs to spaces:
:retab
(note that the.vimrc
optionexpandtab
does not apply for existing tabs)
- Absolute numbers:
:set number
(or::set nu
) - Relative numbers:
:set relativenumber
(or:set rnu
) - Hybrid mode (relative and current line is absolute):
:set number relativenumber
(or::set nu rnu
) - Disable with
:set nonu nornu
or:set nonumber norelativenumber
(absolut + relative = hybrid)
- List changes:
:undolist
- Undo:
u
- Undo last n changes:
2u
(undo last 2 changes)
- Undo last n changes:
- Redo: Ctrl + r (on Mac use control instead of Ctrl)
For more commands see: https://www.cyberciti.biz/faq/vim-undo/
- Cut line:
dd
- Cut n lines:
ndd
(3dd
to delete 3 lines) - Cut until
- beginning of line:
d^
- end of line:
d$
- beginning of line:
- Yank/copy line:
yy
- All mentioned earlier for cut works for yank as well
- Paste:
p
(after current position/line);P
(before current position/line)- Paste, writes the buffers from cut or yank
- Cut word under cursor:
diw
(delete in word) - Yank word under cursor:
yiw
(yank in word)
- Indent current line:
>>
- Unindent current line:
<<
- Indent multiple lines:
5>>
(indents 5 lines by one level)
Enter VISUAL BLOCK mode by hitting: Ctrl/Control + v
- Place cursor
- Enter visual mode
- Navigate to select multiple lines/characters/... (via
hjkl
or<arrow_keys>
) - Press
I
to get into INSERT mode (notei
will not work)- Alternatively use other commands like deletion (e.g.
4d
, ...)
- Alternatively use other commands like deletion (e.g.
- Add the text you want to insert (you'll see it only for the first line)
-
<ESC>
to finish (only now you'll see the changes on all lines)
Vim works with registers storing (temporarily) information for text processing automatically or by user interaction.
- View currently assigned registers and their content:
:reg
- Pasting from a register is as simple as
"0p
(in command mode)- You will only see the paste after hitting
p
(no leading:
)
- You will only see the paste after hitting
Register cmd | Description |
---|---|
"" |
Unnamed reg; default for yank, delete, ... (d , D , x , X , c , C , s , S ) |
"0 |
Default reg: default for yank |
"1 |
Slot 1 in register stack (will get the value of "0 after next yank, ...) |
... | ... |
"9 |
Slot 9 (last slot) |
"a |
Named register a (possible values a-z ) |
"_ |
Black hole register () |
". |
Last inserted text |
": |
Last exec command |
"% |
Name of current file |
"/ |
Last search text |
"* |
Current clipboard content |
Notes:
- For a yank
""
and"0
have the same content -
p
will paste from the default register (""
) - Registers persists session-wide
- Delete a line without adding it to a register:
"_dd
- Clearing a register:
:let @a=@_
(assign the black whole register (_
) to registera
) - Put (store) to register
"4yy
(yank line to register4
) - Append to register (only
a-z
): Use capital letters (A-Z
)
-
Fwd:
/keyword
-
Bckw:
?keyword
-
Repeat (same direction):
n
-
Repeat (opposite direction):
N
-
Stop text highlighting from search:
:noh
- Search/replace globally in file (->
*
)::%s/searchPattern/replacePattern/g
Useful options:
" Activate syntax highlighting
syntax on
" Set relative/absolute line numbers (here: hybrid mode = both)
set number relativenumber
" Disable with in vim session via `:set nonu nornu` or `:set nonumber norelativenumber`
" Set tab widths
set tabstop=4 " (ts) width (in spaces) that a <tab> is displayed as
set shiftwidth=4 " (sw) width (in spaces) used in each step of autoindent (aswell as << and >>)
" Replace tabs with spaces and set tab width
set expandtab " (et) expand tabs to spaces (use :retab to redo entire file)
""" File specific indendation settings
" for yml files, 2 spaces
autocmd Filetype yml setlocal ts=2 sw=2 expandtab
autocmd Filetype yaml setlocal ts=2 sw=2 expandtab
" ts = number of spaces for tab
" sts = number of spaces for tab while editing
" sw = number of spaces for auto-indent
" -> https://vimdoc.sourceforge.net/htmldoc/quickref.html#option-list
" Compensate for typo `:Wq` instead of `:wq` ans silently do the right thing (-> save and exit)
cnoreabbrev <expr> Wq ((getcmdtype() is# ':' && getcmdline() is# 'W' && getcmdline() is# q)?('Wq'):('wq'))
Provides a kdiff3-alike 3-way merge tool within your terminal (see here for an example.
Line backgrounds:
- Red/magenta: partial match
- Magenta: common text
- Red: differences
- Blue: additional text
- Cycle windows:
Ctrl + w
- Jump to next diff:
[c
- Jump to previous diff:
]c
- Select diff code from:
- local:
:diffget LO
- = theirs = local changes (current branch) (merge)
- = ours = upstream (rebase)
- Explanation about the role inversion in rebase vs merge: "Note that a rebase merge works by replaying each commit from the working branch on top of the branch." (from git man).
- base:
:diffget BA
- = common base
- remote:
:diffget RE
.- = ours = changes from remote/upstream (merge)
- = theirs; local changes from your branch (rebase)
- If you want to edit yourself select a version from above first and edit as usual.
- local:
- Save and exit:
:wqa
(a
for quitting all windows (3 diffs + editor)
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License *.
Code (snippets) are licensed under a MIT License *.
* Unless stated otherwise