4

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

6

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.

| improve this answer | |
  • 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 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 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 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 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 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.

| improve this answer | |
  • 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. – Martin Tournoij Apr 10 at 12:08
  • Agree, it actually has several corner cases so I used the mode that only changes the status line – Tae Apr 10 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>.

| improve this answer | |
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.

| improve this answer | |

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.