Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a vim command to easily insert a debug statement on the variable name under the cursor. I would at least like to handle some common cases so it doesn't have to be perfect but the following should all work:

$test_var = array($test->get_test_var()) || $test->get_test_var() || $test_var;

Having the cursor over any of these variable names and running the command should insert a line above the call with the value of the expression inside. E.g. place the cursor over anything in '$test->get_test_var()' and then the following is inserted above on a new line:


Placing the cursor over 'array($test->get_test_var())' should insert the same thing and not include the array. Placing it over array should do this:


And finally, placing it over '$test_var;' should insert the following (not including the semicolon):


Obviously I'm working on this myself, but some of you could have insight on how to do this easily that I will miss as a relatively new user of vim.

share|improve this question
It's pretty easy to do if you're willing to visually select the text you want to insert (see ":help 04.4" and ":help visual-mode") or the text is something Vim recognizes as a text object (see ":help 04.8" and ":help text-objects"), but Vim doesn't understand PHP expressions, so there is no easy way that I know of (short of writing a PHP parser in Vim script) to select a PHP variable or expression after putting the cursor in it as you would like. – garyjohn Oct 21 '11 at 19:48

If you put something like this in your vimrc, you'll autoreplace %DATE% with the format:

    iab %DATE% <c-r>=strftime("%a %d %b %Y")

For simpler stuff, put in:

    iab br best regards,<CR><CR>Your Name

This will replace "br" with best regards etc.

So you could try something like:

    iab debugr ...

Not sure if you need to press enter in order to replace, but give it a try.

share|improve this answer
I already came up with a command like that to insert the date (map <silent> <F9> :r ! date "+\%m-\%d-\%Y"<CR> map <silent> <s-F9> :r ! date "+\%m-\%d-\%Y \%H:\%M:\%S"<CR>) but I'm thinking you mistakenly posted that here. – chris.ritsen Oct 25 '11 at 12:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .