Mirmo Dynamics

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

To content | To menu | To search

Keyword - vim

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.

Sunday 8 March 2009

having fun with vim, an event tracker

The idea of this script came by realizing that the main reason I (almost) never fill my timesheet, is because I just forget it. How easier could it be if my editor (namely vim) could fill it for me ? Or at least, help me fill it. This script is meant to help this process. It will hook to every configured event (in the code below, BufRead and BufNewFile) and add a log line to the log file. Easy heh ? Of course, the script it self won't be of much use, I'll have to add some more events to track as well as writting some kind of frontend for it, with reporting & all.

if exists('trackloaded')
  finish
endif

" list of autocmd events:
" http://www.vim.org/htmldoc/autocmd.html#autocmd-events
let trackevents = ['BufRead', 'BufNewFile', 'BufEnter', 'BufWrite', 'BufLeave']
let trackfile   = '/tmp/timetrack.dat'
let trackloaded = 1

for event in trackevents
  execute 'autocmd '.event.' * call Track("'.event.'")'
endfor

unlet event

function Track(event)
  let l:filename = @%
  if l:filename != g:trackfile
    if match(l:filename, '/') != 0
      let l:filename = getcwd().'/'.l:filename
    endif
    silent execute '!echo `date +\%s` '.a:event.' '.l:filename.' >> '.g:trackfile
  endif
endfunction

You might note from this snippet that I'm not very comfortable with vimscripting :-) If you have a better way of doing this (I'm thinking mostly about the file writting here), don't hesitate to tell me.

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

Wednesday 3 January 2007

Remplacer un caractère par un "Carriage Return" dans vim

Par exemple, imaginons qu'on récupère une query string, et qu'on veuille l'exploser sur plusieurs lignes, on peut faire:

:s/&/\="\<CR>"/g

le \="" annonce à VIM que la chaine de substitution sera une chaine a interpréter avant utilisation, et le \<CR> représente tout simplement le Carriage Return.

Tuesday 28 November 2006

Linux + gVim + Rox-filer = Mon IDE.

Une des questions cruciales qui se pose à tout développeur à au moins un moment de sa vie (souvent plusieurs en fait) est le choix d'un environnement de développement. J'en ai testé pas mal, plus ou moins longtemps, et bien que je ne sois jamais complètement satisfait, l'idée de perdre du temps à développer le mien m'indispose. J'ai donc opté pour l'environnement qui me va le mieux: Linux + gVim + Rox-filer.

Note: je ne couvre pas ici les fonctionnalités de débuging avancé, que je n'utilise pas encore, mais pour lesquelles j'ai déjà en tête des solutions qui me conviendront bien mieux que les outils intégrés à un quelconque IDE (je pense fortement à Xdebug).

Continue reading...

Wednesday 12 July 2006

Gestion des charset sous vim

Ne vous attendez pas a trouver un guide exhaustif de la gestion des encodages de caractères sous VIM dans ce billet, d'autres sites et surtout la doc fait ça beaucoup mieux que moi. Je m'en vais juste vous présenter le minimum vital pour vous dépatouiller dans ce foutoire que sont les charset. Le saint graal est consistué de deux variables de configuration: encoding et fileencoding. Les plus perspicaces auront déjà compris les quelques explications qui suivent.

encoding est la variable qui détermine l'encodage des caractères en cours d'édition, tandis que fileencoding contient l'encodage qui sera utilisé à la sauvegarde du fichier.

Concrétement, si vous tapez:

:set encoding=utf-8
:set fileencoding=iso-8859-15

Vous pouvez copier/coller du texte en UTF-8, et quand vous sauvegarderez votre fichier (:w par exemple), vos données seront automatiquement convertie et sauvegardée en ISO-8859-15.

Monday 26 June 2006

Trop utiliser VIM peut rendre dépendant.

geoffrey@PROD000005:~$ ls
bin   Desktop      helloworld.jpg     openvpn      tmp   xml
code  Documents    Network            public_html  tmp2
dc2   Extractions  OnRefaitMatch.xml  tests        :w

Ce n'est pas un fake. Et pour ceux qui ne comprennent pas, tant pis pour vous.

Saturday 8 May 2004

L'édition efficace avec vim

Encore une traduction d'un article de Jonathan McPherson ! Cette fois l'article porte sur vim, l'éditeur de texte des décideurs pressés. Les remerciements vont cette fois à Grégoire Gilbert qui m'a traduit plus de la moitié de l'article quand même :)

Continue reading...