You could try calling a function from your mappings, which would save the current value of the relevant option, change the option, set an autocmd
(with events such as ShellCmdPost
/QuickFixCmdPost
for grep/vimgrep, and CursorMoved
/CursorMoved
for the visual block), and then run the original command.
But note that there may be better alternatives than switching those options. For example, if are creating a mapping for :grep
and wishes to disable 'ignorecase'
(because as far as I understand 'incsearch'
won't affect this command) then you could add \C
in the begging of the pattern and leave the option unchanged.
There are also some options which are commonly changed during a workflow. For those, you could try using unimpaired plugin:
OPTION TOGGLING unimpaired-toggling
On Off Toggle Option
[ob ]ob cob 'background' (dark is off, light is on)
[oc ]oc coc 'cursorline'
[od ]od cod 'diff' (actually |:diffthis| / |:diffoff|)
[oh ]oh coh 'hlsearch' (coh: first |:nohlsearch|, then |'nohlsearch'|)
[oi ]oi coi 'ignorecase'
[ol ]ol col 'list'
[on ]on con 'number'
[or ]or cor 'relativenumber'
[os ]os cos 'spell'
[ou ]ou cou 'cursorcolumn'
[ov ]ov cov 'virtualedit'
[ow ]ow cow 'wrap'
[ox ]ox cox 'cursorline' 'cursorcolumn' (x as in crosshairs)
Edit:
If you are looking for native solutions, them you should look in the built-in documentation. There are some commands for a few options, such as :noswapfile
:
:nos[wapfile] {command} *:nos* *:noswapfile*
Execute {command}. If it contains a command that loads a new
buffer, it will be loaded without creating a swapfile and the
'swapfile' option will be reset. If a buffer already had a
swapfile it is not removed and 'swapfile' is not reset.
There is also :noautocmd
, which disables autocmds. To search for more, you could try :help :no<ctrl-d>
to list the possible matches in the documentation that starts with :no
, or use :helpgrep
.
In addition to the mapping+function workaround suggested above, you could implement a custom commands for options not covered by the built-in functionality -- this approach is used by some plugins, such as ProjectRoot.
nnoremap <silent> K :setl nois<CR>:grep! "\b<C-R><C-W>\b"<CR>:setl is<CR>
as you see i have to turn off incsearch and turn it on after searching.virtualedit
a while ago but I have never really find a use case where I'll need to use it. So, Can you may be tell me more about what you use this feature for and how that is useful with theblock
setting on compared to the default?:set ve=block
-- isn't that what it's for?