Vim
last updated 2013-08
delete all lines matching a pattern
delete all empty lines:
:g/^\s*$/d
delete all single-line comments in a C++ file:
:g/^\s*\/\//d
delete all lines that do not match the given pattern (:g! is equivalent to :v):
:g!/pattern/d
directly type HTML entities
the main power of Vim is that all of its commands and macros are represented by "normal" keystrokes. that way, you can map certain characters to direct input (or to commands, etc)
to have accented characters directly translated to HTML entities, you can just add mappings like this to ~/.vimrc (or define them when needed using the :imap command):
imap Č Č
imap č č
imap Š Š
imap š š
imap Ž Ž
imap ž ž
" ... and so on ...
then, when you type Č, Vim will automatically insert Č instead
reformat a plain text paragraph
Vim includes full support of plain text editing tools
one of the most helpful ones is the textwidth variable, which tells Vim how long the lines should be, and while typing it automatically breaks lines to the given width (of course, preserving full words)
but when you edit text within an already formatted paragraph, or if you have existing paragraphs and want to reformat them, you have to do this yourself. fortunately, Vim helps you with this too - simply place the cursor within the paragraph, and in normal mode (press Esc if you're in insert mode) press
vipgq
the vip combination simply selects the paragraph (visual inner paragraph), and gq reformats it
for ease of use, you can define a shortcut for use from insert mode:
imap <C-v>r <Esc>vipgqi
now, pressing Ctrl-v and r in insert mode will reformat the current paragraph, and return to insert mode
command line and encryption
$ vim -x filename - open filename for editing, and save it encrypted (will prompt for a key)
:X - set encryption for current file; use ':set key=' to disable encryption
- put "set cm=blowfish2" in your .vimrc to enable safer encryption by default
$ vim +10 [file] ; vim +/hello [file] - open [file] at line 10, or at first occurrence of pattern 'hello' in file
:w !sudo tee % - save file via `sudo` (as root)
update 2026 - the old default zip and plain blowfish methods are broken, blowfish2 is the least you want. for anything that matters, a gpg -c encrypted file is the safer bet