45

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?

muru
  • 24,838
  • 8
  • 82
  • 143
Mateon1
  • 553
  • 1
  • 4
  • 7
  • 4
    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
  • 1
    @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
  • 4
    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

4 Answers4

42

You can 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>?.

You don't need to use the <leader> key, you can use anything as a prefix, for example ; works just as well:

:nnoremap ; <C-w>

And now ;w, ;s, etc. will work.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
26

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.

user530873
  • 648
  • 4
  • 8
13

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.

Yep_It's_Me
  • 231
  • 2
  • 5
7

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>

statox
  • 49,782
  • 19
  • 148
  • 225