3

Right now, when I split a file in VIM, I need to use CTRL-W + h/j/k/l to switch between them.

In spacevim (https://spacevim.org/), each file has a number visible in the bar (see picture), and I can just press SPACE + <number> in command mode to go to that file. I find that way more convenient than the default CTRL-W option. However, I don't want to install spacevim just for this, because it do a lot of other config that I don't like.

enter image description here

So how could I configure my vimrc to do the same?

Thank in advance for your help.

  • Note that "window numbers" (unlike "window ids") are assigned depending on current layout (top-left to bottom-right). So even if they are not visible, the window on the left is still "1", and one on the right is "2", – Matt Nov 21 '19 at 11:22
5

To see window numbers you have to setup statusline (:h statusline), for example:

set statusline=[%{winnr()}] 
set statusline+=%([\%R%M]\ %)
set statusline+=%<%f
set statusline+=%(\ %y%)
set statusline+=%=
set statusline+=%4(%p%%%)

See winnr() function in the first line.

To switch to the window with that number you can use built-in 2 C-ww

You can add mappings:

nnoremap <space>1 1<C-w>w
nnoremap <space>2 2<C-w>w
nnoremap <space>3 3<C-w>w
nnoremap <space>4 4<C-w>w
nnoremap <space>5 5<C-w>w
nnoremap <space>6 6<C-w>w
"...

Probably it could be done the other way.

enter image description here

| improve this answer | |
  • 1
    Thank you. I can successfully switch with SPACE but I don't see the number in the status line. Could it be because I use the airline plugin? – Nakrule Nov 21 '19 at 11:06
  • 1
    Yes, you have to setup airline to show window numbers (my example is for a standard statusline) – Maxim Kim Nov 21 '19 at 11:11

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.