# HG changeset patch # User Daniel Robert Allen # Date 1365359626 25200 # Node ID aef3ee7d2cbe3b2d85f02814000bb7b7733109c2 # Parent 56683eb09eccdb4a4baf43302761ed348197c1cc Avoid destroying record of last visual selection diff -r 56683eb09ecc -r aef3ee7d2cbe chapters/34.markdown --- a/chapters/34.markdown Thu Apr 04 01:38:14 2013 -0400 +++ b/chapters/34.markdown Sun Apr 07 11:33:46 2013 -0700 @@ -9,11 +9,14 @@ ---------------- By yanking the text into the unnamed register we destroy anything that was -previously in there. +previously in there. Further, by using a visual selection to yank the text in +the case that our operator is applied with a motion, we also destroy any record +of the most recent visual selection. -This isn't very nice to our users, so let's save the contents of that register -before we yank and restore it after we've done. Change the code to look like -this: +This isn't very nice to our users, so let's avoid using a visual selection in +that case and also save the contents of the unnamed register before we yank in +all cases so that we can restore it after we're done. Change the code to look +like this: :::vim nnoremap g :set operatorfunc=GrepOperatorg@ @@ -25,7 +28,7 @@ if a:type ==# 'v' normal! `y elseif a:type ==# 'char' - normal! `[v`]y + normal! `[y`] else return endif @@ -38,6 +41,8 @@ We've added two `let` statements at the top and bottom of the function. The first saves the contents of `@@` into a variable and the second restores it. +Additionally, we've applied yank with a motion rather than a visual selection in +the case that our operator is applied with a motion. Write and source the file. Make sure it works by yanking some text, then pressing `giw` to run our operator, then pressing `p` to paste the text