1

I like what the shortcut Ctrl+Wf does, but I want to have a vertical split instead of horizontal.

I managed to achieve this by defining the following mapping:

nnoremap <c-w>f :vertical wincmd f<cr>

and in normal mode it works perfectly. But I failed to do the same for visual mode.

What I have tried so far:

" This yields 'E481: No range allowed
vnoremap <c-w>f :vertical wincmd f<cr>

" This just ignores my selection and behaves like <c-w>f in normal mode,
" i.e. attempts to open the file under cursor
vnoremap <c-w>f :<c-u>vertical wincmd f<cr>

How can I do such a thing for the visual mode?

2
  • <C-W>f is only defined for Normal mode. What makes you think it'll be of use in Visual mode too?
    – Matt
    Commented Dec 27, 2019 at 17:18
  • @Matt I tried it without any custom mappings and found out that it works in visual mode. E. g. if you have line /home/ovandriyanov and visually select only /home, then vim opens /home, while in normal mode it opens /home/ovandriyanov. The only thing I would like to change is to have vertical split instead of horizontal. Commented Dec 27, 2019 at 17:27

1 Answer 1

3

After you press : you got into cmdline-mode. And there's no direct way back into Visual from it (except normal! gv and such, but that's indirect through Normal). See :h mode-switching, because it's essential to understand how Vim works.

Therefore, you must do it in a different manner: while still in the Visual yank the selection and put it into the command line.

vnoremap <c-w>f y:vsplit +find<bslash><space><c-r>"<cr>

Note: if file name contains spaces you might also want to use :h escape() function.

BTW. The Normal mode mapping could also be rewritten in a similar manner:

nnoremap <c-w>f :vsplit +find<bslash><space><c-r><c-f><cr>

There's a single problem though: it always splits first, even if a file name was not found in path.

6
  • This is definetely a useful answer which will help me to figure out a proper solution, but I must say that the proposed solution indeed behaves imperfectly when the selected file does not exist: not only it splits the window when the file does not exist, it also starts editing the new file instead of giving the error message about searching for non-existent file. I believe I can work this around with some complex function, though. Commented Dec 27, 2019 at 18:37
  • @OlegAndriyanov Not sure how you got such behaviour. There's E345 if file was not found.
    – Matt
    Commented Dec 27, 2019 at 18:45
  • well I did nothing special, here is the tty capture demonstrating the issue terminalizer.com/view/6253c52a2672 Commented Dec 27, 2019 at 19:32
  • 1
    @OlegAndriyanov You forgot backslash
    – Matt
    Commented Dec 27, 2019 at 19:34
  • You are right. Thanks! Commented Dec 27, 2019 at 19:38

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.