I'm using QuickRun to execute php files or part of php code by selecting it in Visual Mode. I have this mapping in my .vimrc

nnoremap <leader>r :QuickRun<CR>
vnoremap <leader>r :QuickRun<CR>
inoremap <leader>rr <Esc>:QuickRun<CR>

It does work great. But to make it work with selected code in Visual Mode I have to add <?php before selected text. For example I have code like:

echo 'first line test';
echo "\n";echo "\n";
echo 'second line test';
echo "\n";echo "\n";

I select just this 2 lines:

echo 'second line test';
echo "\n";echo "\n";

and by pressing <leader>r how can I add <?php tag before selected lines and then reselect it again to run it by :QuickRun<CR>?


1 Answer 1

  • The visual mode range is stored in the '< and '> marks. You can set this with m< and m>.
  • You can start visual mode with the last selection with gv
  • You can add lines to the buffer with the append() function.

Combining this, we could use something like:

" The range keyword makes sure we run this function over the
" entire range, instead of over each line in the range
fun! SetRange() range
    " Append the text before the visual selection
    call append(line("'<") - 1, '<?php')

    " Expand visual selection by one line
    execute ':' . line("'<") - 1
    normal m<

    " Reselect
    normal gv

    " Run your function

noremap <Leader>r :call SetRange()<CR>

If you also want to remove the added line, you could use the normal mode dd command. Expanding this function to do that is left as an exercise for the reader ;-)

  • 1
    +1 Thank you, thank you, thank you very much. I just had to change QuickRun to execute "'<,'>QuickRun" or I would get an error "PHP Parse error: syntax error, unexpected '<' in /tmp/vduL2VG/10 on line 5" Again, thank you very much. It does work great.
    – whitesiroi
    Commented Feb 25, 2016 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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