VIM Tips & Tricks

vim-logo-enVim is my favorite text editor on Linux, Mac OS X and even Windows. The vim editor is an enhanced version of vi. Vi works great over slow network ppp modem connections and on systems of limited resources. One can completely utilize vi without departing a single finger from the keyboard. In this post I describe some handy tricks and tips while using Vim.

Encrypt files with vim

With vim it is very easy to encrypt your files. Start vim with the -x command line parameter and the name of a (new) file. Vim will ask for your encryption key (twice). When you save the file it will be encrypted.

The default encryption method is “zip”. You better set the default encryption method to blowfish because this method delivers much strong encryption. Add a line to your .vimrc with the contents set cm=blowfish

:set cul Highlight the line the cursor is one
:set nu Show line numbers
:set nobackup Do not create backup files
:iab AlP ABCDEFGHIJKLMNOPQRSTUVWXYZ AlP expands after pressing <TAB>
ma Create mark with the name a
‘a Goto the mark with the name a
´a,´bw fname Write contents between mark a and mark b to a file with the name fname
%s/needle/haystack/gc Replace all occurences of needle with haystack in the current file asking for confirmation
echo expand(‘~’) Determine your HOME directory
1,$s/$/XXX/ For all lines in the file, append a tripple X (XXX). You could also use % instead of 1,$
%s/^\(.*\)\n\1$/\1/ Find and remove all duplicate lines
%s/ *$/ Remove trailing spaces on all lines in the buffer (%)
‘t,’b!sort Sort all lines between mark t and mark b

List of VIM plugins can be found here.

Useful regular expressions

:g/profile/d  Delete all lines containing “profile” (remove the /d to show the lines that the command will delete)
 :g/^\s*$/d Deleting all lines that are empty or that contain only whitespace
 :v/error\|warn\|fail/d Delete all lines except those that contain “error” or “warn” or “fail
Share

Leave a Reply

Your email address will not be published. Required fields are marked *