How to select all previously deleted texts after recovery

I often use a command or script to delete text and after restoring (u) I want to see what was previously deleted with the / script command.

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

+6
source share
3 answers

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

This will not work if the deleted text contains newline characters or regular expression metacharacters (for example, \ or [ ). If possible, try:

 /\V<CTRL-R>=substitute(substitute(getreg('"'), "[\\/]", '\\\0', 'g'), "\n", '\\n', "g") 
  • \V - very nomagic - disables most regex metacharacters
  • <CTRL-R>= - insert a vim expression evaluation
  • substitute(..., "\n", '\\n', "g") - avoid all lines of a new line in a given line
  • substitute(..., "[\\/]", '\\\0', 'g') - delete all slashes and backslashes in the given string
  • getreg('"') - get the contents of the register " , which contains the last deleted and / or deleted text

This is a bit verbose, so if you need to do this often, you can bind it to a command in ~/.vimrc :

 " use ,/ in normal mode to match the most recently deleted or yanked text nmap ,/ /\V<CR>=substitute(substitute(getreg('"'), "[\\/]", '\\\0', 'g'), "\n", '\\n', "g")<CR><CR><CR> 
+6
source

How to take diff from one state of a file and compare with another?

 :command! -nargs=0 DiffLastChange exe "norm! u" | vert new | set bt=nofile | r # | 0d _ | diffthis | wincmd p | exe "norm! \<cr>" | diffthis 

Now you can simply run DiffLastChange to see the difference of the latest changes in the file.

Explanation:

  • exe "norm! u" undo the last change in the current buffer
  • vert new vertically splits the new buffer
  • set bt=nofile change buffer type to buffer from scratch
  • r # read the contents from an alternate file, i.e. the buffer we started with
  • 0d _ clear the new buffer by deleting the empty line at the top in the black hole register.
  • diffthis mark current buffer to be part of diff
  • wincmd p switch to the last buffer (back to the buffer we started from)
  • exe "norm! \<cr>" repeat to restore buffers to their original state
  • diffthis mark source buffer separate from diff

After you are done, I recommend doing :diffoff! to disable both diff.

Unfortunately, this command in its current state cannot process unsaved buffers, because :read # will read in the file. The solution is to copy the contents of the buffer into the named register and then paste it into the zero buffer. Unfortunately, this will hide the named register. Refactoring the code into a function will give greater flexibility and will allow you to use a variable to save the contents of the register (and the type of register) and restore the register at the end.

 function! DiffLastChange(...) let a = @a let at = getregtype('a') let c = a:0 == 1 ? a:1 : 1 let ft = &ft try exe "norm! " . c . "u" sil %ya vert new set bt=nofile exe "set ft=" . ft sil pu a 0d _ diffthis wincmd p exe "norm! " . c . "\<cr>" diffthis finally call setreg('a', a, at) endtry endfunction command! -nargs=? DiffLastChange call DiffLastChange(<f-args>) 

In addition to fixing problems with unsaved buffer and problems with clobbering, I added the ability to return to history through the command argument, for example. :DiffLastChange 3 . The command also sets the buffer file type for the buffer in the same way as the original buffers, so syntax highlighting will be enabled for this buffer.

For a much more reliable solution, to see the differences between the parts of the story in the buffer, I agree with Christian Brabandt and suggest Gundo or histwin . For more information about Gundo, see These are vimcasts .

For more help see:

 :h diffthis :h diffoff :h wincmd :h 'bt' :h :r :h :d 
+4
source

Try using the histwin or gundo module. This allows you to distinguish between all undo branches, and impressions also show a single diff for differences in the preview window.

+2
source

Source: https://habr.com/ru/post/890688/


All Articles