# HG changeset patch # User Steve Losh # Date 1481991668 18000 # Node ID 0baa9f04528392c6b4343ff91de66a71624121f9 # Parent 8d3cdc4c9c2a10316c5d5024435bc49d20ef952d# Parent aef3ee7d2cbe3b2d85f02814000bb7b7733109c2 Merge. diff -r 8d3cdc4c9c2a -r 0baa9f045283 chapters/34.markdown --- a/chapters/34.markdown Thu Dec 15 15:22:41 2016 -0500 +++ b/chapters/34.markdown Sat Dec 17 11:21:08 2016 -0500 @@ -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