Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Execute commands without changing the search register
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Execute commands without changing the search register
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{review}} {{TipImported |id=509 |previous=507 |next=510 |created=July 15, 2003 |complexity=intermediate |author=Salman Halim |version=6.0 |rating=5/5 |category1= |category2= }} 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(<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>) </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==
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Navigation
(
view source
)
Template:Review
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab