toggleterm.nvim icon indicating copy to clipboard operation
toggleterm.nvim copied to clipboard

[Feature idea]: Context-based toggling

Open rish987 opened this issue 1 year ago • 5 comments

Hi there! I've been using this plugin for quite a while (it finally got me off of tmux!), and wanted to ask for your opinion on this idea for a new configuration option + set of external utility functions. Currently, when using :ToggleTerm without a count, it defaults to "smart-toggling", which has the effect of opening the first unhidden terminal if none are open, and closing open terminals one by one otherwise. However, being a frequent user of :tcd [directory], I would find it more useful for terminals to be opened on a per-tab basis, rather than per-session, and there are probably :lcd [directory] users out there who would like to have this effect per-window.

~~So, I'm proposing that we add a new config option toggle_strategy that can be set to "first", "by_tab", or "by_window" that lets the user select their preference. And we could expose the utility functions that work under the hood to find the correct terminal to toggle (based on context) so it is easy for the user to define additional commands if they want to toggle sometimes by tab, sometimes by window, sometimes globally, etc.~~ Edit: change of plans, see #290.

Of course, this is possible already with some amount of configuration work by the user, but I think explicitly supporting this could make things much easier on their end. And we can take the opportunity to generalize/unify some of the logic in the current implementation.

rish987 avatar Aug 08 '22 02:08 rish987

Another smart-toggle helper we could add is one that opens the last-used terminal, which I think could be very useful to avoid having to provide a count before <C-\> when possible, though I think we might have to add some state to make this work. Anyways, I wanted to gauge your opinion on #290 before working on this (in a separate PR).

rish987 avatar Aug 08 '22 17:08 rish987

Hi @rish987

Sorry I haven't much time to properly weigh in at the moment, I'm generally intrigued by the idea but also concerned about introducing complexity that increases the maintenance burden by either adding complexity to the plugins usage of its code. Ultimately whatever change is added would have to be as minimal as possible in the first instance and more importantly easy to maintain. Ultimately the change sounds good but if it means every future change now has the overhead of considering this I'd be less inclined.

akinsho avatar Aug 08 '22 20:08 akinsho

Hi, thanks for the input! This should be fairly self-contained I believe, I tried to keep it minimal in #290. Really all that changes is that instead of hard-coding the smart-toggle strategy, we've abstracted this to allow the user to pick one from a set of given strategies (and which defaults to the original "first non-hidden terminal" strategy). It doesn't touch any core aspects of the code, so any PRs not relating to smart-toggling specifically shouldn't have to even consider it.

I was thinking we could also add the commands :ToggleTermTab and :ToggleTermWin (and eventually :ToggleTermLast) ourselves so that the user doesn't have to make these commands themselves.

rish987 avatar Aug 08 '22 20:08 rish987

To summarize, the overall point of this functionality is to minimize the occurrences of count + <C-\> by the user (the number keys are hard to reach, after all, and terminal numbers are hard to remember!) when they're simultaneously using many different terminals in many different contexts. I figured that the current tab is a good heuristic for figuring out the context, but in the future I think we could generalize this by allowing the user to "pin" terminals to tabs so that multiple tabs can share a terminal (i.e. context). Or rather, a tab will automatically be pinned to the ~~last~~ first terminal that was opened in it, and the default strategy will be to use the last opened terminal if none are pinned (which will consequently pin whatever that terminal was), or the pinned terminal if there is one. ~~So you have to explicitly call :ToggleTermTab to open a tab-specific terminal, from which point on it will be pinned to that tab.~~

It may well be that I haven't explored enough to know if there's a better way, so if you've already found a convenient way around this issue do let me know! So far, this feature has been extremely useful for me.

rish987 avatar Aug 09 '22 00:08 rish987

+1.

For example, let's say I am working on a project in one tab and then a separate project in another tab. Like for example the main thing I'm doing is in the first tab, and then in the second tab I'm modifying my vim config, or something.

In the first tab, my working directory is that of the project, and in the 2nd tab it's ~/.config/nvim.

As you know, in a separate tab, you usually have a completely different split layout, different buffers in each split, a different working directory, my neotree also shows the different directory, ...

In each of these two independent tabs, I'd like to access a toggleable terminal using <leader>t, but given the above context, it only makes sense for it to be a completely different terminal depending on which tab I'm currently in (e.g. because of the working directory).

If you know of a way how to configure this, I'd much appreciate it.

Thanks!

neumannjan avatar May 28 '23 13:05 neumannjan