Archive for the ‘Linux/UNIX’ Category

How to Make the Command Line Behave Like vim

Tuesday, May 4th, 2010

If you use vim, you’ve probably found yourself wishing you could use vim commands in more places. Well, the command line is one place where you can. Simply run this simple command and you’ll have vim commands at your command on the command line:

set -o vi

Better yet, add that line to your ~/.bashrc on Linux or your ~/.bash_profile on Mac OS and you will have permanently added this wonderful ability to your terminal.

How to delete just directories in UNIX

Friday, April 2nd, 2010

It took me a long time to find any good answers online for how to delete just directories in UNIX, and the solutions I found didn’t work when the directory names contained spaces. The following line of code works even when the directory names contain spaces.

find * -type d | while read line; do rm -rf "${line}"; done

How To Get Rid of vim’s Swap Files After a Crash

Wednesday, December 30th, 2009

When you open a file in vim, it creates what’s called a swap file for the file on which you’re working. So if you’re working on two files called temp1 and temp2, you’ll end up with two swap files like this:

$ find . -name '.*.swp'
./.temp2.swp
./.temp1.swp

Once you close vim, those files will go away, unless vim closes unexpectedly (e.g. you lose your ssh connection). To get rid of those files, we can use a little tool I like to call xargs:

$ find . -name '.*.swp' | xargs rm

When you pipe find through xargs rm, you’re saying, “Take the output from find and run an rm command on each one of those items.” If you run find again, you’ll see that those pesky swap files are gone:

$ find . -name '.*.swp'
$

xargs has, of course, many other applications, but this particular technique is helpful if you’ve ever encountered this problem in vim.