OK – so I use CodeIgniter as my PHP framework and one of the things that (I kind of like) is the addition of a special PHP comment that they add to the end of their files:
/* Location: ./system/application/controllers/welcome.php */
I keep all of my projects in folders like this:
/home/jon/public_html/<project name>/<project files and folders>
Apache is configured as a local server with virtual hosts (I’ll post on that later)
For a CodeIgniter project, the controllers folder would be:
/home/jon/public_html/<project name>/application/controllers
So I wanted to have a quick way to add the code snippet to the end of the file with a quick couple of keystrokes.
Rather than mess with insert mode mapping – I decided to use the snipMate vim plugin (VERY handy, by the way).
I added this snippet into ~/.vim/snippets/php.snippet
snippet cifooter
/* Location: `substitute(expand("%:p"),"/home/jon/public_html/[a-zA-Z]*/",'.\/','g') ` */
“What does it do?” You might ask…
I’m sure you recognize the /* Comment */ delimiters and the “End of File” and “Location” text – the magic is what’s between the backticks ` `
snipMate has this cool feature that allows you to echo the results of VIM functions (enclosed in ` `) as snippets.
-
expand("%")
this function spits out the current file name. You can also achieve this in insert mode with
<c -R>=expand("%")<cr> -
substitute(expand("%:p"),"/home/jon/public_html/[a-zA-Z]*/",'.\/','g')
This is similar to the
s//ex command – except it acts on an input string rather than the buffer. I’ll break it down:
-
substitute("input string","pattern to match","subtitute for pattern","modifier")
takes the input, finds the pattern, replaces it with the substitue. The ‘g’ modifier means to globally replace all pattern matches:
substitute("/home/jon/folder","jon","blog","g") -> "/home/blog/folder"In this particular case, I’m replacing any instance of “/home/jon/public_html/<any number of alpha characters>/” with a forward slash “/” to produce the path in the code igniter file.
-
expand("%:p")
gives the full path of the file in the current buffer
-
The end result?
If my file is: /home/jon/public_html/foo/application/controllers/welcome.php
I can type
in insert mode and voila!
MUST faster AND easier than manually typing in all that stuff. I can even shorten the snippet trigger – but I just prefer the more descriptive text.










