Abstract
This document collects some of my favorite tricks with the VIM text editor. If you are an advanced VIM user, you are most likely to know most or all of them already.
I am using VIM as a console application without mouse features
enabled. Some of the tricks are thus for interfacing with the GUI world.
I know there is mouse support in VIM and I also know there is
gvim
, but I still think I am faster with my approach
because having VIM integrate natively into whatever Terminal window I am
currently in is an important feature I use a lot.
Top Tricks
Input | Mode | Effect |
---|---|---|
o |
Normal | Open a new line after the current one and switch to insert mode. |
0 |
Normal | Move to begin of line. Easier to type than
^ |
xp |
Normal | Exchange character with the one behind it. Fix fat-fingered typos. |
G |
Normal | Move to end of file |
gg |
Normal | Move to begin of file |
mx |
Normal | Mark the current cursor position with
x |
'x |
Normal | Jump back to mark x |
'' |
Normal | Jump back to previous jump position |
ci{ |
Normal | Remove text inside the current curly braces and enter insert mode. |
CTRL-R 0 | Ex/Insert | Recall register value. You can use this in
/ searches, too! |
:!cmd |
Ex/Visual | Invoke command. In visual mode: Filter selection through command. |
Very nice for :!sort on a selection or
interacting with external |
||
clipboard programs the Q&D way | ||
:set nonumber |
Ex | Temporarily disable line numbers for Q&D copy+paste via terminal |
* |
Normal | Search forward for word under cursor. |
# |
Normal | Search backward for word under cursor. |
J |
Normal | Merge two subsequent lines. |
n |
Normal | Move to next search result |
N |
Normal | Move to preceding search result |
CTRL-V | Normal | Enter block visual mode. Beware that a y
and p will block-paste! |
ZZ |
Normal | Save and Exit. Easier to type than
:wq |
ZQ |
Normal | Discard and Exit. Easier to type than
:q! |
:tabe . |
Ex | Open directory browser in new tab and VIM’s PWD |
:tabe %:h |
Ex | Open directory browser in new tab and current file’s directory |
:e %:h/file |
Ex | Replace currently shown file by another one from same directory |
/regex |
Normal | Search |
:%s/se/re/g |
Ex | Replace all occurrences of se with
re in current file |
:!mousepad % |
Ex | Open current file in mousepad (think GUI copy&paste operations) |
:syntax off |
Ex | Temporarily disable syntax highlighting |
:e |
Ex | Reload file |
:next , :prev |
Ex | Navigate buffers (for minimalist vi w/o
tabbing/windowing). |
A nice trick for the BASH-shell is set -o vi
which
enables a sort of normal mode for command line prompts
reachable with [ESC]. You can then navigate the history with J/K and
search it with /
and ?
. If you press
v
for visual mode, it will open the currently typed in
command line in VIM and execute it as soon as you save&exit VIM.
Useful Plugins
I use few plugins because VIM is already very powerful by itself and some problems solved by plugins are better solved by learning the respective native VIM functionality.
I’ve found the following plugins to be useful for certain special purposes:
DrawIt! ASCII Drawing Plugin
https://www.vim.org/scripts/script.php?script_id=40
The nice thing about this plugin is that it can newly do UTF-8 lines, too. It follows a multi-cursor input paradigm where you can use the arrow keys to draw lines and H/J/K/L keys to move around (all in normal mode).
vim-niji: Yet another rainbow parentheses plugin
https://www.vim.org/scripts/script.php?script_id=4580
This plugin does syntax highlighting for the myriads of nested parentheses one may face when doing anything Lisp/Scheme related.
Tricks to be mastered
I think these are useful tricks to, but I use them far too rarely :)
- The power of
.
- Jump Cursor to bottom, middle, top
- Scroll current line to bottom, middle, top:
zb
,zz
,zt
- Scroll pages
- Wider movements than
W
andB
Related Programs
VIM is not only a text editor. There is also an ecosystem of applications adhering to the H/J/K/L movement keys which go well with VIM. Here are some of my favorite VIM-like programs:
- vifm(1) – File manager
- sc-im – Spreadsheet calculator
- zathura(1) – PDF viewer
- ncdu(1)
– TUI
du
alternative - i3(1) – Tiling Window Manager. It is not very VIM-ish, but it does support modes and can be configured to use H/J/K/L making it blend in seamlessly.
See Also
Find a description of my VIM and VIFM configurations here: conf-cli(32).