2

I am currently trying to write myself a function the will let me show or hide (toggle) the :terminal window. The only way to hide it, as far as I currently know, is to just navigate to that window and close it with ZQ, :q or whatever. I have a problem though:

  • I have the buffer number N opened in some split, but I don't know how to navigate to the window that has the buffer N opened.

I thought switchbuf might help, but I found out that it works only for quickfix commands.

Example

In the picture above I have the cursor in buffer number 1. Which command lets me move to the terminal at the bottom (buffer number 15 in my case) instead of opening the terminal in buffer 1 or creating it in new split? The buffer numbers are of course arbitrary.

Thank you in advance.

2

Use :h :wincmd to manipulate window in script, it follow the same rule as :h CTRL-W . e.g. If you want to jump to window 3, you can do it by 3<c-w><c-w> in normal mode, or :3wincmd w in script.

Use :h bufwinnr() to get window number for a specific buffer.

Use :h :execute to combine them together:

:execute bufwinnr(bufnr) 'wincmd w'

Use :h window-functions to find window related functions, use :h CTRL-W to find window related normal mode command.

Another crucial function for a global terminal is to repeat last terminal command, you can see my global terminal gterm for an example, it also remembers it's position and size when you toggle it.

| improve this answer | |
2

With vim only

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

First, you need set switchbuf=useopen in your vimrc (:h 'switchbuf') to reuse a split (if it exists, else it will create it), 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

More in :h windows

With FZF

Besides, you could use the 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 | |
  • @ashrasmun please accept this answer if you're satisfied with it, or comment / update your answer if more details are needed :) – Biggybi May 2 at 22:10
  • Thank you very much for your response! It's very helpful :) I made sure to upvote it, but I don't think I can mark it as the accepted one as it didn't address my question directly and dedowsdi's one did. I am also not sure about the switchbuf=useopen option, as I used it without success. – ashrasmun May 3 at 8:11
  • Well, it should not fail. Are you using the sbuffer command when you have this setting? – Biggybi May 3 at 11:45
  • Oh, it actually works! Thanks for the alternative method! I wish I could mark this as accepted, but I can't choose two answers, sorry :( – ashrasmun May 3 at 18:57
  • No problem, as long as it serves you well! I think it's less 'hacky' thank the accepted answer though (which is a good answer nonetheless), but don't worry about that :) – Biggybi May 3 at 19:19

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.