Archive for the ‘vim’ 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 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.

.vimrc for Dvorak

Tuesday, December 29th, 2009

I use vim with the Dvorak keyboard layout. It was a big hassle at first but I’ve come up with (and lifted) some combinations of .vimrc directives that make things pretty easy with not too much disruptive key remapping. Some of this stuff I just copied and pasted from a co-worker’s .vimrc and I’ll admit that I’m not sure what 100% of this does. If you have any questions, ask and I’ll add some better comments to the file.

colors default
syntax on
au BufNewFile,BufRead *.module set filetype=php
au BufNewFile,BufRead *.inc set filetype=php
set background=dark
set wh=60
set expandtab
set sw=4
set smarttab
set smartindent
set autoindent
set number
set nowrap
set ic
set ts=4
set sts=4
set sta
set ai
set et
set cin

"Dvorak stuff
noremap t j
noremap n k
noremap s l
noremap j n
noremap gn gk
noremap gt gj
noremap - $
noremap _ ^
no S L

Vim Search and Replace

Tuesday, December 29th, 2009

Vim search and replace is pretty simple but the top Google results for “vim search and replace” don’t really give it to you straight. So here it is:

:%s/search string/replace string/g

Example: if you wanted to replace all instances of “cat” with “dog,” you would do this:

:%s/cat/dog/g

You can find a more thorough, verbose (and potentially confusing) explanation here. Chances are, though, that you’ll only be using the above method 99.9% of the time.