I was looking for a way to set virtualedit=block before executing visual block select with additional binding g<C-v> and after that turning it off.

There are few things in my workflow that require that behavior. I didn't find anything after some research. So currently I have this in my vimrc.

nnoremap g<C-v> :setl virtualedit=block<CR><C-v>
nnoremap <C-v> :setl virtualedit=<CR><C-v>

Another example is to disable incsearch for custom command

nnoremap <silent> K :setl nois<CR>:grep! "\b<C-R><C-W>\b"<CR>:setl is<CR> 

So my question is, is there right or just better than the one I came up with way to set option just for one command and switch it back after command is done?

Update Here is example of function that can help with second case

function! Example()
  let &l:virtualedit="block"
  exe "normal \<C-v>"
  let &l:virtualedit=""

But this way does not work for cases like in first example

  • Why do you need virtual edit in visual block? In case if you need to select till end when some lines are longer than or shorter than each other, ten you can press $. Can you tell me y you need virtual edit? I will know more about it from you :)
    – SibiCoder
    Commented Jul 8, 2016 at 8:27
  • 3
    @SibiCoder It is just some times useful to operate exactly on square space even when there is no data to be selected. But question is not really about visualedit, but about setting option. Because for example i have this 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. Commented Jul 8, 2016 at 8:35
  • @SardorbekImomaliev Hey, I have heard about 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 the block setting on compared to the default? Commented Jul 8, 2016 at 12:00
  • Just :set ve=block -- isn't that what it's for?
    – Antony
    Commented Jul 8, 2016 at 13:16
  • @Antony I am talking about setting it temporarily. Just for one command/movement/action Commented Jul 8, 2016 at 13:29

1 Answer 1


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)


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.

  • Thank you, but this doesn't really answer the question. But yeah, there plenty similar workarounds, I was looking for native solution that will work for all cases, if it exists. Commented Jul 8, 2016 at 11:50
  • @SardorbekImomaliev I've included additional info to the answer.
    – mMontu
    Commented Jul 8, 2016 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.