Vim Tips Wiki
m (Commands that don't clobber the search register moved to Execute commands without changing the search register: Page moved by JohnBot to improve title)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=509
 
|id=509
  +
|previous=507
|title=Commands that don't clobber the search register
 
  +
|next=510
|created=July 15, 2003 8:32
+
|created=July 15, 2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Salman Halim
 
|author=Salman Halim
 
|version=6.0
 
|version=6.0
 
|rating=5/5
 
|rating=5/5
 
}}
|text=
 
I frequently execute commands (mappings, usually) that perform operations that change the value of the search register for the sake of the mapping. They might do a :s or some such that affects the search register. I don't always want this side effect, so I use the following command/function:
+
I frequently execute commands (mappings, usually) that perform operations that change the value of the search register for the sake of the mapping. They might do a :s or some such that affects the search register. I don't always want this side effect, so I use the following command/function:
   
  +
<pre>
 
" Executes a command (across a given range) and restores the search register
 
" when done.
 
function! SafeSearchCommand(line1, line2, theCommand)
 
let search = @/
 
execute a:line1 . "," . a:line2 . a:theCommand
 
let @/ = search
 
endfunction
 
com! -range -nargs=+ SS call SafeSearchCommand(&lt;line1&gt;, &lt;line2&gt;, &lt;q-args&gt;)
 
" A nicer version of :s that doesn't clobber the search register
 
com! -range -nargs=* S call SafeSearchCommand(&lt;line1&gt;, &lt;line2&gt;, 's' . &lt;q-args&gt;)
  +
</pre>
   
 
Basically, :SS followed by any command will execute that command (to simulate keystrokes, use :normal as the command) and restore the search register when it's done. :S is a replacement for :s which works EXACTLY the same way (with or without range, flags etc) but doesn't clobber the search register in the process.
   
 
==Comments==
" Executes a command (across a given range) and restores the search register
 
 
" when done.
 
 
function! SafeSearchCommand(line1, line2, theCommand)
 
 
let search = @/
 
 
 
 
execute a:line1 . "," . a:line2 . a:theCommand
 
 
 
 
let @/ = search
 
 
endfunction
 
 
com! -range -nargs=+ SS call SafeSearchCommand(&lt;line1&gt;, &lt;line2&gt;, &lt;q-args&gt;)
 
 
 
 
" A nicer version of :s that doesn't clobber the search register
 
 
com! -range -nargs=* S call SafeSearchCommand(&lt;line1&gt;, &lt;line2&gt;, 's' . &lt;q-args&gt;)
 
 
 
 
Basically, :SS followed by any command will execute that command (to simulate keystrokes, use :normal as the command) and restore the search register when it's done. :S is a replacement for :s which works EXACTLY the same way (without or without range, flags etc. etc.) but doesn't clobber the search register in the process.
 
}}
 
 
== Comments ==
 
The "without or without range" should be "with or without range". (Probably obvious, but I didn't want anybody to think I was stuttering!)
 
   
Anonymous
 
, July 15, 2003 8:35
 
 
----
 
----
<!-- parsed by vimtips.py in 0.477871 seconds-->
 

Revision as of 06:51, 4 November 2007

Tip 509 Printable Monobook Previous Next

created July 15, 2003 · complexity intermediate · author Salman Halim · version 6.0


I frequently execute commands (mappings, usually) that perform operations that change the value of the search register for the sake of the mapping. They might do a :s or some such that affects the search register. I don't always want this side effect, so I use the following command/function:

" Executes a command (across a given range) and restores the search register
" when done.
function! SafeSearchCommand(line1, line2, theCommand)
  let search = @/
  execute a:line1 . "," . a:line2 . a:theCommand
  let @/ = search
endfunction
com! -range -nargs=+ SS call SafeSearchCommand(<line1>, <line2>, <q-args>)
" A nicer version of :s that doesn't clobber the search register
com! -range -nargs=* S call SafeSearchCommand(<line1>, <line2>, 's' . <q-args>)

Basically, :SS followed by any command will execute that command (to simulate keystrokes, use :normal as the command) and restore the search register when it's done. :S is a replacement for :s which works EXACTLY the same way (with or without range, flags etc) but doesn't clobber the search register in the process.

Comments