2

I am a Vim beginner. I know you can do :tabnew file1 file2 to have Vim open up a bunch of files name file1, file2 spread across as tabs. I know you can move forward and backward between tabs. My problem is: when I try to open a Vim terminal using :terminal, I can only switch back to 1 of the tab. For example, if I am in file1, I use :terminal , I can only switch back between the terminal and file1, I cannot access file2 unless I close the terminal, which brings us back to the beginning, where we have tabs across the editor. Is it possible to cycle between Vim terminal, file1 and file2? Or I better off just use :! some commands then use that as my way to access the terminal? That is a good idea but due to my work, I would need to see the terminal outputs. Using :! some commands will perform terminal commands, but once you press Enter, you are back to the editor and can no longer see what happens at the terminal.

Please let me know and thank you very much for your help.

  • How did you switch tabs? if you are using gt and gT it works just fine. – Finn Feb 20 at 4:52
  • Well I saw some posts that say just use Control Shift Up and Down to move. By gT, you mean letter "g" then "Tab"? – mle0312 Feb 20 at 4:54
  • In normal mode, just type character g then t to move forward, or g then shift t to move backward, Ctrl Shift Up does the same but it's not the vim way since it's not let the hands on the home row. – Finn Feb 20 at 4:58
  • 3
    Some would say using tabs isn't the vim way. :) Or, at least, using them in ways they weren't intended to be used isn't. (Others dismiss it as just another religious war.) Samplings here, here and here. (FWIW I used to use them before I really knew vim. Now I can't remember the last time I opened one.) – B Layer Feb 20 at 7:39
  • 2
    @BLayer theyve been helpful for me when working across different stages of a compiler (lexer v. parser/ast v. driver—each tab is different set of files from the same proj). But i agree wholly with the spirit of your comments. – D. Ben Knoble Feb 20 at 17:39
2

You can move between tabs by type gt to move forward and gT to move backward in the Normal mode.

Or you can show the buffers (like the opening files) by type the command :buffers then choose the index number to open any buffer you want by typing another command b4 for example to open the first buffer like in the picture.

enter image description here

Take a look at fzf.vim plugin https://github.com/junegunn/fzf.vim#commands. They provides an easy way to access buffers by just type :Buffer.

  • 1
    Just to add.. you can also type ls rather than buffers... – Mantisimo Mar 6 at 14:10
1

I use the following to access :terminal. It searches for terminal buffers, if there is none it opens a new one in a new tab. If there are any it uses the first one it found, if that terminal buffer is currently displayed in a window it changes to that window, else it opens a new tab and displays the terminal buffer therein.

I only used this in NeoVim so far, it should also work in Vim, though

function! TerminalTab()
    " find evey terminal buffer
    let b = filter(range(1, bufnr('$')),
                \'getbufvar(v:val, "&buftype", "ERROR") == "terminal"')
    " if no terminal buffers are available
    if len(b) == 0
        tabnew
        terminal
    else
        " we only care for the first terminal buffer
        let b = b[0]
        let bb = win_findbuf(b)
        " if there is no window in any tab that contains a terminal
        if len(bb) == 0
            tabnew
            buffer b
        else
            " again we only care for the first match
            let bb = win_id2tabwin(bb[0])
            exe 'tabnext'.bb[0]
            exe bb[1].'wincmd w'
        endif
    endif
    " we open a terminal to do something
    startinsert
endfunc
nmap <silent> <leader>s :call TerminalTab()<cr>

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.