akinsho / toggleterm.nvim

A neovim lua plugin to help easily manage multiple terminal windows
GNU General Public License v3.0
4.13k stars 168 forks source link

[Feature] Allow multiple terminals opened in different tabpages #452

Open Bekaboo opened 1 year ago

Bekaboo commented 1 year ago

Hi, thanks for making this awesome plugin, it makes nvim's builtin terminal much more ergonomic and user-friendly.

Sometimes I work on 2+ projects in neovim and keeps them in multiple tabs, and it would be nice to have multiple toggleterms opened in these tabs. Currently if I open a toggleterm in one tab, switch to another tab and call 'toggleterm.toggle_command()` again, toggleterm will close the terminal in the previous tabpage instead of opening a new one in the current tabpage.

I assume this might be a config issue and can be solved by passing proper opts to toggleterm? The following is my config if that's related:

local toggleterm = require('toggleterm')

toggleterm.setup({
  open_mapping = '<C-\\><C-\\>',
  shade_terminals = false,
  start_in_insert = false,
  hide_numbers = true,
  autochdir = false,
  persist_size = false,
  direction = 'horizontal',
  float_opts = {
    border = 'shadow',
    width = function()
      return math.floor(vim.go.columns * 0.7)
    end,
    height = function()
      return math.floor(vim.go.lines * 0.7)
    end,
    winblend = 0,
  },
  size = function(term)
    if term.direction == 'horizontal' then
      return vim.go.lines * 0.3
    elseif term.direction == 'vertical' then
      return vim.go.columns * 0.35
    end
  end,
  highlights = {
    NormalFloat = { link = 'NormalFloat' },
    FloatBorder = { link = 'FloatBorder' },
    WinBarActive = { link = 'WinBar' },
    WinBarInactive = { link = 'WinBarNC' },
    MatchParen = { link = 'None' },
  },
  winbar = {
    enabled = false,
  },
  on_open = function()
    vim.cmd('silent! normal! 0')
  end,
})
akinsho commented 1 year ago

Hi @Bekaboo,

Tab scoped terminals is definitely something that has come up before and is an interesting idea. There are definitely some things to iron out about how it would work though. The simplest solution is that if you set some value on the terminal itself and if true then toggleterm treats it as only existing in the tab it was created in and if false it opens wherever.

I worry about introducing all of this complexity though as one of the first things I imagine this will introduce is a bunch of feature requests to accomodate a bunch of minutiae. I tend to lean more towards keeping the plugin as simple as possible to reduce the maintenance burden in the long run.

TLDR: seems like a cool idea, not clear on all the specifics of how it would work. Don't currently have the time to build it and I'm worried it will lead to more complexity.

Bekaboo commented 1 year ago

OK, thanks for your response. should I close this issue?

akinsho commented 1 year ago

@Bekaboo no we can leave it open, someone might decide to pitch in since it's a popular idea I've heard before. If not maybe when things calm down at work I'll take a look at it.

sleewoo commented 11 months ago

definitely a must, imo. if i have a terminal open in some tab and need it in another tab, manually closing terminal and open it in new tab.

sort of awkward so was trying autocmd BufWinEnter/BufWinLeave to close terminal(s) on leave and reopen(if any) on enter.

not much success though.

coot commented 10 months ago

It would be something really handy. I often use multiple tabs for different projects, and toggleterm is not very intuitive when closing a terminal in another tab changing focus to another tab page.

akinsho commented 10 months ago

It's fine to want things but if no one else is willing to put the time or effort in or contribute since this is OSS it won't get done. For better or worse I have a life and a job and this isn't a priority for me over those. Please no more plus one comments.

akinsho commented 10 months ago

Relates #500

anurag3301 commented 7 months ago

I was also recommending an easy way to split the terminal and have more. Like in below example I tried splitting it with <C-W>v image It does seem like we have tow terminals, but they are just copied, If I write to one, it appears on the second to. Feels like could be a nice features. image

tarunaksha commented 7 months ago

I have a solution here....you can check....In my case each terminal is working fine >> https://github.com/akinsho/toggleterm.nvim/issues/533#issuecomment-1913585725

axelhj commented 4 months ago

Hi @Bekaboo!

I have a related issue but I don't feel it is a bug. In the same situation as OP reports I've found that when a toggle-term terminal is currently opened in another tab, then Neovim will switch to that tab if i run :ToggleTerm to close that terminal which is opened in the other tab. I observed it with a minimal Neovim config.

How I hande it to not lose context (my current tab number) is that I use a condition in my keymap;

  local should_restore_mark = require "toggleterm.ui".find_open_windows() and
    vim.o.buftype ~= "terminal"

and based on it I set a global mark and restore it after :ToggleTerm was called. It enables my current workflow. I guess Jumplist navigation could be used instead. I invoke the toggleterm mapping twice to "move" my active terminals into my current tab.

I know OP would like to get separate terminals in the new tab - basically I have another little "hack" for that; call :ToggleTermToggleAll if find_open_windows() gives true before using eg. :ToggleTerm2 if the old terminal had term-number 1, to get a fresh terminal in the separate tab while keeping the old terminal as a hidden buffer. Combined with the above few commands and a some bookkeeping with autocommands around TabNew & TabEnter could make ToggleTerm behave as if there were a separate session in each tab.

I feel that this described autocommand setup could be added as an optional feature into ToggleTerm itself but maybe it is to contrived of an implementation. I guess each terminal buffer should have some meta-data to say which tab they belong to.

erlangparasu commented 1 month ago

https://github.com/akinsho/toggleterm.nvim/issues/593