21 July, 2012

i've been using macvim as my development environment for groovy/grails at work and i'm slowly building up my toolbox of functionality. Here's one I put together based on based on a RunShellCommand method

function! s:RunShellCommand(cmdline)
  echo a:cmdline
  let expanded_cmdline = a:cmdline
  for part in split(a:cmdline, ' ')
     if part[0] =~ '\v[%#<]'
        let expanded_part = fnameescape(expand(part))
        let expanded_cmdline = substitute(expanded_cmdline, part, expanded_part, '')
     endif
  endfor
  botright new
  setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
  call setline(1, 'You entered:    ' . a:cmdline)
  call setline(2, 'Expanded Form:  ' .expanded_cmdline)
  call setline(3,substitute(getline(2),'.','=','g'))
  execute '$read !'. expanded_cmdline
  setlocal nomodifiable
  1
endfunction

i found out about this here: http://vim.wikia.com/wiki/Display_output_of_shell_commands_in_new_window


then, i added some commands to my vimrc:


this one searches all my groovy files for the word under the cursor (the thing is the under-the-cursor magic):

command! Srchwg call s:RunShellCommand(
'find . -name \*.groovy -exec grep -H --regexp="' .expand("<cword>").'" {} \;')

this one, takes an argument for a filename extension and will search all files of that type for the word under the cursor:

command! -nargs=+ Srchw call s:RunShellCommand(
'find . -name \*.'' -exec grep -H --regexp="' .expand("<cword>").'" {} \;')

now, i can have my cursor on a line and type :Srchwg which will open a window with a list of files that contain the word. or, if i type :Srchw js, i'll search all js files in my project for the word under the cursor. nice.

i was thinking of writing a function to, then, open files based on the buffer opened by the RunShellCommand method, but i found that vim already has functionality to do that. all i needed to use was the handy-dandy gf functionality!

anyway, one of these days, i'll package up all of my vim tricks into either a vimball or just a vimrc that you can source into your vimrc and open source it on github.

No comments: