My favorite VIM tricks

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 :)

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:

See Also

Find a description of my VIM and VIFM configurations here: conf-cli(32).


Ma_Sys.ma Website 5 (1.0.2) – no Flash, no JavaScript, no Webfont, no Copy Protection, no Mobile First. No bullshit. No GUI needed. Works with any browser.

Created: 2021/06/16 21:55:20 | Revised: 2022/09/18 20:49:41 | Tags: vim, mdvl, note, tricks, kb | Version: 1.0.0 | SRC (Pandoc MD) | GPL

Copyright (c) 2021, 2022 Ma_Sys.ma. For further info send an e-mail to Ma_Sys.ma@web.de.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.