History Report a problem
Article Edit this page Discussion

Execute commands without changing the search register

From Vim Tips Wiki

(Redirected from VimTip509)
Jump to: navigation, search

Tip 509 Previous TipNext Tip

Created: July 15, 2003 Complexity: intermediate Author: Salman Halim Minimum version: 6.0 Karma: 5/5 Imported from: Tip#509


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.

[edit] Comments


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pixar
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Flash Gordon
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Execute commands without changing the search register"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation


.