0

I have in my .vimrc the following lines, which lets me switch windows with ctrl+hjkl:

nnoremap <C-h> <C-W>h
nnoremap <C-j> <C-W>j
nnoremap <C-k> <C-W>k
nnoremap <C-l> <C-W>l

These are fine for my desktop computer, but on my netbook, I want to have the active window completely fill the tiny screen. This means typing ctrl+w _ and ctrl+w | after each window change. The logical step would be to add those keystrokes to the mapping, yielding:

nnoremap <C-h> <C-W>h<C-W>_<C-W>|
nnoremap <C-j> <C-W>j<C-W>_<C-W>|
nnoremap <C-k> <C-W>k<C-W>_<C-W>|
nnoremap <C-l> <C-W>l<C-W>_<C-W>|

But that fails, consistently, when in a mapping, despite working when I simply type the required keys; and (as I have set 'showcmd') it seems to leave a trailing <C-W>.

I have also tried using :wincmd:

nnoremap <C-h> :wincmd h<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-j> :wincmd j<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-k> :wincmd k<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-l> :wincmd l<cr>:wincmd _<cr>:wincmd |<cr>

But that complains about trailing <cr> whenever my vimrc is sourced, so I'm not going to pursue that further without more research.

Any ideas?

Zoey Hewll
  • 4,788
  • 2
  • 20
  • 33
  • 1
    It looks like you don't want to use windows at all in your netbook so why bother? Use buffers instead. – romainl May 07 '16 at 06:45

2 Answers2

1

Try using <Bar> instead of |. ie:

nnoremap <C-h> <C-W>h<C-W>_<C-W><Bar>
nnoremap <C-j> <C-W>j<C-W>_<C-W><Bar>
nnoremap <C-k> <C-W>k<C-W>_<C-W><Bar>
nnoremap <C-l> <C-W>l<C-W>_<C-W><Bar>
Greg Hurrell
  • 5,177
  • 23
  • 27
1

| are used to have multiply commands on one line and you will need to be escaped with a backslash when used literally:

nnoremap <C-h> <C-W>h<C-W>_<C-W>\|
nnoremap <C-j> <C-W>j<C-W>_<C-W>\|
nnoremap <C-k> <C-W>k<C-W>_<C-W>\|
nnoremap <C-l> <C-W>l<C-W>_<C-W>\|

On the other hand | can be useful:

nnoremap xxx :if 1 == 2 | echom "hello" | endif
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123