Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight all previously deleted text after an undelete

I often use a command or script to delete text and after I do an undelete (u) I want to see what has been previously deleted by the command/script.

Is it possible to highlight the previous deleted text when use the undelete command? (or even better match the previous deleted text in a find "/" command)

like image 339
Reman Avatar asked Jun 16 '11 14:06

Reman


1 Answers

If you just deleted some text using d, you can use /<CTRL-R>" to match the text you just deleted (even if you just undid the delete with u).

This won't work if the deleted text contains newlines or regex meta-characters (like \ or [). If that's likely, try:

/\V<CTRL-R>=substitute(substitute(getreg('"'), "[\\/]", '\\\0', 'g'), "\n", '\\n', "g")
  • \V - very nomagic - turns off most regex meta-characters
  • <CTRL-R>= - insert the evaluation of a vim expression
  • substitute(..., "\n", '\\n', "g") - escape all the newlines in the given string
  • substitute(..., "[\\/]", '\\\0', 'g') - escape all slashes and backslashes in the given string
  • getreg('"') - get the contents of the " register, which contains the most recently yanked and/or deleted text

This is a bit wordy, so if you find yourself needing to do it often, you can bind it to a command in your ~/.vimrc:

" use ,/ in normal mode to match the most recently deleted or yanked text
nmap ,/ /\V<C-R>=substitute(substitute(getreg('"'), "[\\/]", '\\\0', 'g'), "\n", '\\n', "g")<CR><CR><CR>
like image 164
rampion Avatar answered Oct 01 '22 05:10

rampion