Mirmo Dynamics

Si tu kiffes pas reunoi, t'écoutes pas et puis c'est tout.

To content | To menu | To search

Keyword - script

Entries feed - Comments feed

Wednesday 18 March 2009

Automatically cd to a symfony project's root in vim

I made a little vimscript to automatically cd to the project root of a symfony project. It makes it easier for me to use the ctags :-)

"-------------------------------------------------------------------------------
"  Description: Finds and cd to the symfony root of the project
"    Copyright: Copyright (C) 2009 Geoffrey Bachelet
"   Maintainer: Geoffrey Bachelet
"      Version: 1.0
"-------------------------------------------------------------------------------

if exists('find_symfony_root_loaded')
  finish
endif

let find_symfony_root_loaded = 1

if ! exists('find_symfony_root_symfony_executable')
  let find_symfony_root_symfony_executable = 'symfony'
endif

" root detection when opening a new vim seems to work
" only if these two events are bound. not sure why.
autocmd BufWinEnter,BufRead * call FindSymfonyRoot()

function FindSymfonyRoot()
  let l:cwd = GetAbsoluteDirname(@%)
  let l:symfony_root = findfile(g:find_symfony_root_symfony_executable, l:cwd.';')
  let l:symfony_root = GetAbsoluteDirname(l:symfony_root)
  if strlen(l:symfony_root) != 0
    execute 'cd '.l:symfony_root
  endif
endfunction

function GetAbsoluteDirname(path)
  let l:path = a:path
  " gets the dirname
  if !isdirectory(l:path)
    let l:path = strpart(l:path, 0, strridx(l:path, '/'))
  endif

  " makes it absolute
  if match(l:path, '/') != 0
    let l:path = getcwd().'/'.l:path
  endif

  return l:path
endfunction

note for later: add vimscript support to geshi

Or see the highlighted version on gist.

Thursday 16 October 2008

Mass vim file opening reloaded

Thinking about it, the snippet I posted earlier was a bit silly as vim can open by itself multiple files way more efficiently. The only benefit from my script is that I don't have to type the --servername and --remote-silent-tab.

So let's add some usefulness ! First, when I need this script, it's often that I first grepped the files, and then decide that I want to edit them all. From this point of view, having to pass them as arguments is not that handy, so we'll add a way to pass them via STDIN.

Also, what if I want to send them to another vim server ? We'll add this ability too.

Here is the resulting script:

#!/usr/bin/env bash

# set a default SERVERNAME
SERVERNAME="ash0"

# look for files on stdin
if ! [ -t 0 ]; then
  FILES=`cat /dev/stdin`
else
  exit;
fi;

# now let's check if we want a specific vim server
if ! [ -z $1 ]; then
  SERVERNAME=$1
fi;

# we can now open the files
/usr/bin/gvim --servername $SERVERNAME --remote-silent-tab $FILES

Tada !

You can now do the followings:

grep foo | sendtovim
grep foo | sendtovim grep0

Tuesday 14 October 2008

Mass vim file opening

Remember my old post about vim and rox-filer ? Well I've got one tiny and silly addition to it now, I can mass open files from any shell command line, with this little script placed in my ~/bin/ (don't forget to chmod +x):

for i in $*; do
  /usr/bin/gvim --servername ash0 --remote-silent-tab $i
  sleep 1
done;

The sleep 1 is necessary as vim does not seem to like being flooded with files.

Example usage:

sendtovim `grep foo *`

Opens in vim all files containing "foo" in the current directory. Who said handy ?

Note: I know have an updated and useful version of this script