6th January 2009

Preserve formatting when pasting in vi/vim

Thursday, July 10th, 2008

When copy and pasting into vi or vim, you can often end up with badly formatted text. This is often referred to as “the staircase effect”. It’s easily prevented by entering :set paste before you paste, when you have finished turn it off again with :set nopaste.

Automatically backing up files before making changes

Sunday, June 1st, 2008

It’s best practice (and common sense) to make a backup of a file before you edit it. Unfortunately it’s easy to forget to do this. We use this simple script below to make a time/date stamped copy of a file before launching the editor (in this case vim). We create it as /usr/local/bin/bvi.


#!/bin/bash
[[ -r $1 ]] && cp $1{,.`date +%Y%m%d-%H%M`} || echo “$1 is a new file”

vim $1

We then add the following two aliases to our ~/.bashrc file to make sure it’s run automatically when we call vim.

alias vi=/usr/local/bin/bvi
alias vim=/usr/local/bin/bvi

This isn’t a replacement for good version control of important files, but it’s a good safety net. It’s also worth noting that this can leave a lot of old copies of files laying about, so it’s work cleaning out old copies every now and again.