Posts Tagged by Vim

refactoring in Go – rather pleasant actually…

I’ve just finished refactoring a large Go program, and the process was rather…. pleasant.

Static typing catches all those obscure errors I wouldn’t think about in a scripting language (Python, Perl, Ruby, etc). My process is:

  • type :make in vim (I have a dummy Makefile in my Go project just for vim)
  • vim jumps cursor to error (vim quickfix list)
  • “oh, I shouldn’t do that” – fix (type type type)
  • start again

Finish rather sooner than expected, run tests, smile in knowledge program is working properly.

Update

To quickly setup the make command for Go, type this in a Vim window:

:setlocal makeprg=go\ build\ \.

Or even better configure vim via your ~/.vimrc, for example:

autocmd BufRead *_test.go setlocal makeprg=go\ test\ \.
autocmd BufRead *.go setlocal makeprg=go\ test\ \./..

Thanks Martin for the comment!

vim – bufexplorer

Bufexplorer – my new favourite add-on for vim.

Why do I like it? It allows you to emulate the buffer list feature of emacs, as well as switch between horizontal/vertical buffer splits and find recently edited files. All this can already be done with vim buffers, but bufexplorer makes it easy.

Why don’t I just use emacs? Because as sysadmin/devop I’m often working on other people’s servers, and I don’t want to go installing buckets of stuff everywhere – vim is usually installed.

I found the best way to use bufexplorer is to open up all possible source files (eg vim src/*.go other/*.go) at the start of an editing session, then just skip between them (shown here with the excellent xMonad window manager, GNU Screen, and Gnome Terminal):

Vim Buffers Cheatsheet

A small Vim Buffers cheatsheet:

:bu <tab>    - select a buffer
:buN         - select buffer N
:ls          - list buffers
:sb tab      - split screen on another buffer
:sbN         - split screen on buffer N
:only        - make this the only buffer (ie maximise)
^w^o         - make this the only buffer (ie maximise)
:ball        - split screen on all buffers
:hide        - hide this buffer
:bdel        - remove buffer from list

Vim buffers don’t seem as flexible as the Emacs equivalent, but then nothing is as flexible as Emacs:

Xkcd Emacs

http://xkcd.com/378/

Vimdiff – refresh or update after changes

I little command I always have to search for when using vimdiff – :diffupdate

After you’ve made changes in vimdiff :diffupdate will recalculate the diffs.

Vimdiff in Action

Vimdiff in Action

vim folding

A concise article on Linux.com (that I keep referring back to) about vim folding. I’ve been using vi/vim for years and vi keystrokes just “happen”, but it’s always nice to learn a few more tricks, especially when working with larger scripts.

A brief summary of folding:

  • highlight some text using v visual mode, zf to fold, zo to unfold (ie open)
  • put cursor on an opening curly bracket, and  zfa} to fold until closing curly bracket (or zfa) or zfa] for parentheses, square brackets)
  • add this to your .vimrc to remember folds:
au BufWinLeave * mkview
au BufWinEnter * silent loadview

Lots more commands in the article.

Next Page »