7

Does Vim have functionality similar to tmux's display-panes command for selecting (goto) a specific window by number?

Sometimes when I've many windows open, it can be a pain to navigate them by C-w h (left), C-w j (down), C-w k (up), and C-w l (right). In that case, I miss functionality similar to tmux's display-panes command, which lets me select (goto) a specific pane by its number:

bind-key    -T prefix       q                     display-panes

Does vim have similar functionality?

enter image description here

0

6 Answers 6

7

I'm not aware of any Vim feature or even plugin that does that; in fact, up until recently it would be rather hard to display it like that, but with Vim 8.2's popup windows it wouldn't be too hard; you just have to loop over all visible windows and create a new popup window displaying the window number.

The closest thing you can get, which may actually be enough, is displaying the window number in the statusline by adding %{winnr()}; you can then use [nr]<C-w>w to switch to a specific window.

5
  • Thank you. Do you know of any approximate functionality, like labeling windows and then moving to a window by its label, or is there a command for moving to the last-visited window (i.e., jump between two windows)?
    – Shuzheng
    Apr 8, 2020 at 9:28
  • 1
    @Shuzheng You can use CTRL-W p to jump to the last visited window, which will effectively jump between two windows when used repeatedly.
    – filbranden
    Apr 8, 2020 at 10:29
  • @filbranden - Thank you. But there is no way to jump to a specific window except using Ctrl-w h, Ctrl-w j, ...?
    – Shuzheng
    Apr 8, 2020 at 11:08
  • 2
    @Shuzheng If you know the window number (for instance by including it in the status line, like Martin suggests), then you can use <count> CTRL-W w to jump to that window directly. Even if you don't have the number in the status line, windows are numbered from top-left to bottom-right, so you should be able to count to the one you would like to jump too...
    – filbranden
    Apr 8, 2020 at 11:17
  • 1
    @filbranden Window number is determined by layout, it's totally possible to number window by top-left, top-right, bottom-left, bottom-right. See end of vi.stackexchange.com/a/22545/5017 for an example.
    – dedowsdi
    Apr 8, 2020 at 12:27
4

You are looking for Choosewin, sadly it's not being actively developed anymore, but for the simplest use cases it works great.

2
  • Looks like that plugin works by creating a new buffer with the same text, and then adding and highlighting some characters to draw a number. It then switches the actual buffer out for the new one, and back when you select it. Clever, but rather ugly, and apparently with some side-effects. With popup windows in Vim 8.2 it's no longer needed, so that plugin can be simplified a lot now. Apr 10, 2020 at 12:08
  • Agree, it actually has several corner cases so I used the mode that only changes the status line
    – Tae
    Apr 10, 2020 at 16:21
2

With vim only

There's a few options to make navigation between files less tedious in vanilla vim.

First, you probably want to have set switchbuf=useopen in your vimrc (:h 'switchbuf') to reuse a split, instead of opening the buffer in the current split.

If you have the buffer number in 'statusline' (i.e %n), you can then easily switch to a split by number with :sbuffer buffnumber.

This will also work if you give a unique pattern from the name. If you are in the split 'foo' and have two more splits 'bar' and 'baz', :sbuffer az will switch to the 'baz' split.

You can have a mapping to make it easier: nnoremap <leader>b :sbuffer (there's a space at the end).

Also, those mappings help switch to different splits (:h windows):

  • <c-w>t -> top left split
  • <c-w>b -> bottom right split
  • <c-w>p -> last used split

With FZF

Besides, could use fzf.vim plugin (requires fzf) and its :Buffers command.

Similarly to switchbuf=useopen, you'll want let g:fzf_buffers_jump = 1 in your vimrc to reuse an open split.

The command will show a list of buffers which you can strip down with fuzzy search, and navigate with <c-j> and <c-k>

<Enter> will then switch to the split if already open, or else use the current one. <c-v>, <c-x> or <c-t> will open the buffer in a new vertical split, horizontal split or tab.

You could have a mapping to launch :Buffers like nnoremap <leader>b :Buffers<cr>.

2
  • 1
    :Windows is another fzf command that shows the window numbers along with tabs.
    – jsuth
    Jul 4, 2021 at 20:12
  • FZF for the win here! The :Windows and :Buffers commands wth the plugin were really cool. I do wish Vim/Neovim had a way to list split numbers and jump between them easier than having to modify the statusline.
    – FilBot3
    Aug 31, 2021 at 14:57
1

I found a plugin that does exactly this.

I have been using for a whole hour now, so I feel comfortable recommending it to the world.

Look at it go!

https://github.com/t9md/vim-choosewin

In particular, I've been using this configuration:

" Invoke choosewin plugin using tab
nmap  <Tab>  <Plug>(choosewin)
"Use overlay feature for choosewin
let g:choosewin_overlay_enable = 1

But I have not play around with other options, it seems to be pretty configurable, go nuts!

1
0

It might not be a direct answer, but if you just need to jump between buffers you can use :bN, where N is the number of the buffer, which you can check with the :ls command.

I rarely need to use more than 3 panes in one direction and I just use the :bN command. If you work long enough with the files, you will remember their buffer number.

0

If you never have more than 9 windows then with a tiny bit of config you can jump to any window with <leader> + window number. Or you can jump to the previous window with <leader>0.

This relies on your status line showing the window number so you know what each window's number is.

Here's the config:

" Jump to window <n>:
" http://stackoverflow.com/a/6404246/151007
for i in range(1, 9)
  execute 'nnoremap <Leader>'.i.' :'.i.'wincmd w<CR>'
endfor
" Jump to previous window:
execute 'nnoremap <Leader>0 :wincmd p<CR>'

Additionally I map tab and shift-tab to cycle through the windows. Sometimes it's quicker to hammer tab a few times than look at the window's status line and type <leader>7 or whatever it may be.

nnoremap <Tab> <C-W>w
nnoremap <S-Tab> <C-W>W

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.