Vim Hands-On
What I would teach people about vim.
Getting started
- vimtutor
- Getting help
:h :help
(great documentation)
- Help on normal mode command
:help CTRL-p
- Help on insert mode command
:help i_CTRL-p
- Help on visual mode command
:help v_CTRL-]
- Help on command line command
:help :s
- Jump to tags with
CTRL-]
, on German keyboards that is AltGr+Strg+9
- Edit file:
:e [file]
- Edit new (unnamed) file:
:enew
or :tabnew
- Quitting:
:q :qa :q! :wq :wqa! ZZ :x
Starting it
- Commands
- Parameters for multiple files:
- Vertical split:
vim -O [files]
- Horizontal split:
vim -o [files]
- Open in tabs:
vim -p [files]
Modes (and how to get there)
- Normal:
Esc CTRL-C
- Insert:
i I a A c C
- Visual:
v V CTRL-V gv
- Command line:
:
- Ex: Don’t do it (
Q
)
Insert Mode (Writing)
- Start insert mode:
i I a A c C
- Completion
- Repetition:
CTRL-P, CTRL-N, CTRL-x CTRL-p
- File or path:
CTRL-x CTRL-f
- Exact line:
CTRL-x CTRL-l
- Abort completion:
CTRL-e
- Others:
:help ins-completion
- Replace/Overwrite:
r R
- Undo/Redo
u CTRL-R U
:earlier :later g+ g-
Motions
- In normal mode: move the cursor or define range for a command
- Moves selection in visual mode
- Characterwise:
h j k l (gj gk)
- Word-wise:
- With count:
17h
- Within Line:
0 ^ $ f F t T
- In File:
gg G [count]G
- Move Cursor in Screen:
H L M
- Move Screen (Scroll):
CTRL-f, CTRL-b, CTRL-e, CTRL-y, zz, zb, zt
- Other Blocks:
{ } ( ) [ ]
- Matching parens
%
- Back and forth jumps:
CTRL-O CTRL-I/TAB
- Last edits:
g; g,
- Marks:
- Set:
m[letter]
- Goto:
'[letter]
[letter]`
- Goto first occurence of word under cursor
] Ctrl-I
after current position
[ Ctrl-I
from start of file
Ctrl-W Ctrl-i
same as above, but open new window with cursor at that position
Cut, Copy, Paste:
y[motion] d[motion] p P, Y D, yy dd, [count]d[motion], x, xp, ddp deep
- Substitute:
c[motion]
- Text objects a[something] i[something]
- Registers
"[letter]dd"
- Special registers ("*+%)
- Copying registers
:let @+=@%
- In insert mode/command line:
CTRL-R [register]
:put
Selections
- Visual modes: regular, full-line, block:
v, V, CTRL-V
- With commands as “motion”
- With command line
- go to other side:
o O
- Repeat selection
gv
- Select next search match:
gn gN
Command Line Mode
- History:
CTRL-p, CTRL-n
- Editing:
CTRL-f
- Filename substitions and modifiers:
% %:r, %:h
- Shell Execution:
:![command] [args]
:%![command] [args]
or with visual range
:r [file]
:r![command] [args]
Windows, Tabs
- Open/close splits:
:sp, :vs, CTRL-W v, CTRL-W s, CTRL-W n, CTRL-W c
- Arranged splits:
:bo :to
- Rearrange splits:
CTRL-W [HJKL], CTRL-W T
- Move between windows:
CTRL-W [hjkl], CTRL-W wW CTRL-W p_
- Rearrange windows:
- Rearrange tabs:
:tabmove
- Move between tabs:
[count]gt gT
- Resize
CTRL-W =, CTRL-W [count]<>+-, CTRL-W |__
- Execute command for all windows/tabs:
:windo, :tabdo
- Diffing
:diffthis
(+ @:
in next window)
:windo diffthis
:diffup
Neat stuff you should know
- In normal mode
- Repeat last edit:
.
- Repeat last command line:
@:
- Join lines:
J
- go to file under cursor:
gf, gF
in same window (the latter with line number: filename.txt:123)
CTRL-W [fF]
in new split window
CTRL-W g[fF]
in new tab (CTRL-W f, CTRL-W T
works too)
CTRL_-^
go back to previous file
- Macro recording and playback q[letter] q @q @@
- Formatting:
gw/gq[motion] gwap
- Suspend:
CTRL-Z
(gvim: minimizes window)
- Increment/decrement the next number in the line:
CTRL-x, CTRL-a
- In command line
- Start Terminal:
:term, :term [command]
- Back to “normal mode”:
CTRL-W N
- Window commands (switching etc) move to a window in “outer” vim
- Use
:wincmd [hjkl]
to move in “inner” vim
- Hack to edit a file as root:
:term sudo vim [filename]
(or edit normally and save via %!sudo tee [filename]
)
- To edit the current file as root:
:term sudo vim %
- Colorschemes
:colo [name]
(tab completion works for name)
- Other
- You can directly open a directory (
:e [path]
) for browsing
- You can open a GUI dialog for open/save commands using
:browse
, e.g. :browse e
- can state directory
:browse tabe include
- for opening files: e, vs, sp, tabe, diffsplit, …
- for saving files: w, sav, wq, …
Search & Replace
/ ? n N
(case settings)
* # g* g#
:g!/xxx/d
:%s/xxx/yyy/g
[i ]i [I ]I
- quick display of occurences of the word under cursor
Options
- Billions of options
:options
provides overview, current settings and documentation
- let/set -> go to .vimrc.md
set [option]
and set no[option]
Programming/IDE
:syntax
:set syntax=cpp
:make
:set makeprg
copen
- Manual Indent:
< >
- Autoindent:
=[motion] ==
- Comment block: Block selection +
I//Esc
- Folding
- set foldmethod=syntax (or indent)
zr zR zm zM za zA
- set foldmethod=manual,
zf
- See ~/.vimrc options
:help path
for going to include files with gf
Plugins
- vim-plug
- vim-fugitive
- YouCompleteMe
- tagbar (sudo apt install exuberant-ctags)
- nerdtree
- indentLine
- go to ~/.vimrc
Other stuff
- Leader mappings
- man pages/help:
K
- Upper/Lowercasing:
gu[motion] gU[motion]
- ROT13:
g?[motion]
CTRL-V
for verbatim entry
:r![command]
:set autoread
- Change the working dir with
:cd
, e.g., :cd ..
- Spelling
:set spell
:set spelllang=de
- wget -P ~/.vim/spell/ http://ftp.vim.org/vim/runtime/spell/de.utf-8.spl
- wget -P ~/.vim/spell/ http://ftp.vim.org/vim/runtime/spell/de.utf-8.sug
- Tags (exuberant-ctags)
- in shell, create tagfile of
- current directory in current directory (available when in that directory):
ctags --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ $(find . -regex .*.hp*)
- other directory (here /opt/ros/crystal) in directory “.vim/tags” (always available):
ctags -f ~/.vim/tags/ros2_install --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ $(find /opt/ros/${ROS_DISTRO:-crystal}/include -regex .*.hp*)'
- Jump to tag under cursor with
CTRL }
or CTRL-W }
(i.e. search word under curser in tagfile and open containing file)
- Use tag-completion:
CTRL-X CTRL-]
Home