2

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:

print_r($test->get_test_var());

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:

print_r(array($test->get_test_var()));

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

print_r($test_var);

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.

  • 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
0

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.