22

My terminal doesn't allow me to type Ctrl+W, because that's a shortcut for closing a terminal tab.

I like working with splits, but I can't find any way to do so without using C-W. This forced me to use tabs, because I can switch between them with gt, gT or #gt where # is a number, but I find this less convenient than splits as I can only see the contents of one file at a time.

I can't use mouse=a either since my terminal doesn't support that.

How can I work with splits in Vim without the Ctrl+W combo available?

  • 1
    Use mappings to wincmd For example you can map: :nmap wj :wincmd j<cr> it's acts same as CTRL-W j (move to window below). :help wincmd – Alex Kroll Jun 27 '15 at 14:06
  • @AlexKroll Why don't you post that as an answer? :-) – Martin Tournoij Jun 27 '15 at 14:16
  • 1
    @Carpetsmoker It's not full cover for this problem I think. CTRL-W j works with counters (go to n-th window below from current) my solution does not. – Alex Kroll Jun 27 '15 at 14:22
  • 2
    I don't normally appreciate advice like this, but I would look for another terminal. <C-W> is too useful a shortcut (not only in many places in Vim, but also in bash) to forfeit. I'd be curious what other key bindings are consumed by the terminal -- <C-T>? At the very least, I'd look into whether these are configurable in the terminal. – tommcdo Jun 27 '15 at 19:38
22

You can just remap <C-w> to another combination, for example:

:nnoremap <C-e> <C-w>

You can now use <C-e> and it will act as if you've pressed <C-w>.

This will overwrite the default <C-e> mapping (scroll down). Vim already uses every key on the keyboard, and the only way to prevent this is using the leader key, which acts like a "prefix":

:nnoremap <Leader>w <C-w>

Unless you've remapped <Leader>, you can now use \w (after each other). I actually prefer this since I don't like CTRL+key combinations.

See What is <Leader>? and How can I find out what <Leader> is set to? And is it possible to remap <Leader>?.

  • Solves my problem. Added the mapping to my vimrc and tested a bit with no issues. – Mateon1 Jun 27 '15 at 14:16
10

I actually use :wincmd more often than Ctrl+W because I find it easier to type the abbreviated :winc.

For example, the equivalent to Ctrl+W L is :winc l.

7

I have set up the following remaps in my .vimrc:

nmap <C-h> <C-w>h
nmap <C-j> <C-w>j
nmap <C-k> <C-w>k
nmap <C-l> <C-w>l

Now I can use Ctrl+h to move to the left window, Ctrl+l to move to the right window, etc. It just seems to logically fit with the normal navigation of Vim.

  • 4
    Note that mapping Ctrl+h in a terminal doesn't always work, as this is often the backspace key. See this answer and this question. – Martin Tournoij Jul 3 '15 at 15:29
  • Also, you may use nnoremap <cr> <c-w>w, since the return key is not used much in normal mode. Then it is effortless to cycle through the splits. – Hope Mar 12 at 15:00
5

To deal with splits I use the submode-plugin which is actually pretty convenient: It allows you to create a new mode ("mode" like in normal mode, visual mode, insert mode, etc...) where you define new key bindings.

With that you don't have to use Ctrl+w anymore and as you are in a new mode all the keys are free to bind: no more problems like "Damn I can't find a key combination which as a signification and isn't mapped yet"

You can install the plugin with any regular plugin manager, and here are some of my configuration to give you some inspiration:


I created a new mode that I called WindowsMode in which I manipulate my splits:

call submode#enter_with('WindowsMode', 'n', '', '<Leader>k', ':echo "windows mode"<CR>')
call submode#leave_with('WindowsMode', 'n', '', '<Leader>')

These lines means that I enter in the mode with Leaderk and I exit the mode with Leader: After I hit Leaderk all the keys I hit will:

  • Do nothing if I didn't defined a mapping for them in this mode
  • Do what I decided to map them in this mode (see the rest of the answer)
  • Go back to normal mode if I hit Leader

Once I'm in WindowsMode these lines make hjkl navigate through the splits like Ctrl+whjkl

" Change of windows with hjkl
call submode#map('WindowsMode', 'n', '', 'j', '<C-w>j')
call submode#map('WindowsMode', 'n', '', 'k', '<C-w>k')
call submode#map('WindowsMode', 'n', '', 'h', '<C-w>h')
call submode#map('WindowsMode', 'n', '', 'l', '<C-w>l')

I used these lines to split the windows with / and !:

" split windows with / and !
call submode#map('WindowsMode', 'n', '', '/', '<C-w>s')
call submode#map('WindowsMode', 'n', '', '!', '<C-w>v')

And q close a split:

call submode#map('WindowsMode', 'n', '', 'q', '<C-w>c')

Move the splits with Ctrlhjkl:

call submode#map('WindowsMode', 'n', '', '<C-j>', '<C-w>J')
call submode#map('WindowsMode', 'n', '', '<C-k>', '<C-w>K')
call submode#map('WindowsMode', 'n', '', '<C-h>', '<C-w>H')
call submode#map('WindowsMode', 'n', '', '<C-l>', '<C-w>L')

Vim-submode allows to do a lot of powerful things, see the doc for more details.


EDIT Another approach of the Window submode is described here. I think the method used by the author is even closer to answer the question: Here is used to create a new mode but all the <c-w>something mappings are available with the same keys in the submode:

" Go through every letter
for key in ['a','b','c','d','e','f','g','h','i','j','k','l','m',
\           'n','o','p','q','r','s','t','u','v','w','x','y','z']
  " maps lowercase, uppercase and <C-key>
  call submode#map('window', 'n', '', key, '<C-w>' . key)
  call submode#map('window', 'n', '', toupper(key), '<C-w>' . toupper(key))
  call submode#map('window', 'n', '', '<C-' . key . '>', '<C-w>' . '<C-'.key . '>')
endfor

" Go through symbols. Sadly, '|', not supported in submode plugin.
for key in ['=','_','+','-','<','>']
  call submode#map('window', 'n', '', key, '<C-w>' . key)
endfor

This way all the mappings <c-w>lowerCaseKey, <c-w>upperCaseKey, <c-w><c-Key> and the mappings <c-w>=_+-<> are all accessible in the window mode without the prefix <c-w>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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