Steve Jackson

Ruby, Rails, Vim, and various other musings
22 Aug 2011

A few quick Vim tips

Here’s a few quick vim-isms to throw in your vim settings, if you’re so inclined.

Reindent File

I grew annoyed at hitting gg=G (indent from beginning to end of file) and having my cursor end up at the end of the file. This method saves your cursor position, executes a gg=G, and returns your cursor to its previous position. Easily modifiable for any other similar action.

" re-indent file function
nnoremap <silent> <F4> :call <SID>ReindentFile()<CR>
function! <SID>ReindentFile()
  " Save cursor position
  let l = line(".")
  let c = col(".")

  " Reindent File
  let cmd = "normal!" . "gg=G"
  execute cmd

  " Move cursor back to saved position
  call cursor(l, c)
endfunction

Get Some Ipsum

Find yourself repeatedly going to a lorem ipsum generator while prototyping? Leverage the simple power of abbreviations by shortcutting the first couple paragraphs of lipsum. Why not use Hipster Ipsum?

iab lipsum1 filler text of choice here.

Fix SCSS formatting

If you find yourself editing SCSS, which you increasingly might with it being bundled by default in the upcoming Rails 3.1, Vim doesn’t properly indent the nested brackets. I found a fix released by Tim Pope (clone this file into ~/.vim/indent/). Voila!