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

Custom tab names don't persist across sessions

Open calops opened this issue 2 years ago • 6 comments

When renaming a tab through :TabRename, it won't persist after closing the editor and recovering the previous session. Instead, the default behavior is back, with the focused buffer's name as the tab name.

I don't actually know if it's possible to persist this information, I have very little insight into how vim sessions work.

calops avatar Jul 08 '22 12:07 calops

Yes, this is a known issue. The session won't store the tab variable, I tried some methods but failed or too complicated.

nanozuki avatar Jul 12 '22 02:07 nanozuki

As a side comment, I really enjoy tabby but wish this feature existed as well. I know that Taboo.vim supports this but as you said, I am not sure how it does that @nanozuki unless if you perhaps integrate the plugin with something like Obsession to restore tab names...

Either way, thanks for the great plugin!

TheCedarPrince avatar Jul 31 '22 20:07 TheCedarPrince

Good call about Taboo.vim. I looked around and they use global variables to remember tab names: https://github.com/gcmt/taboo.vim/blob/caf948187694d3f1374913d36f947b3f9fa1c22f/plugin/taboo.vim#L23

Seems a bit tedious as you would need to serialize all the names as a string on session save, and parse it on load. But perfectly doable. I may try to implement this whenever I have some time.

calops avatar Jul 31 '22 22:07 calops

Amazing @calops - if you end up making a PR, please ping me there and I will give a review as well as test it out. :smile:

TheCedarPrince avatar Aug 01 '22 03:08 TheCedarPrince

I've been a bit busy lately and just recently saw the messages. I was struggling with how to store an array to a global variable. After looking at the code in Taboo.vim, I was inspired that we can encode it as a string.

nanozuki avatar Aug 10 '22 05:08 nanozuki

No worries at all @nanozuki ! Happy to provide a demo or this functionality working in Taboo. Thanks for the package and hope you are having a great day. :)

TheCedarPrince avatar Aug 10 '22 14:08 TheCedarPrince

FYI: nyngwang/suave.lua is a lightweight auto project-session plugin that supports this (and more). There is a DEMO in the README and an example setup config in the Wiki.

nyngwang avatar Feb 07 '23 10:02 nyngwang

@nyngwang Thanks! I'll read that. This is an excellent reference. I found that nvim has API to encode/decode JSON; it also may be helpful. Because of some personal reasons and my new year holiday, I'm late! However, I want to implement this feature this month.

nanozuki avatar Feb 09 '23 03:02 nanozuki

My hack for rmagatti/auto-session (plugin use x.vim):

save_extra_cmds = {
  -- tabby: tabs name
  function()
    local cmds = {}
    for _, t in pairs(vim.api.nvim_list_tabpages()) do
      local tabname = require("tabby.feature.tab_name").get_raw(t)
      if tabname ~= "" then
        table.insert(cmds, 'require("tabby.feature.tab_name").set(' .. t .. ', "' .. tabname:gsub('"', '\\"') .. '")')
      end
    end

    if #cmds == 0 then
      return ""
    end

    return "lua " .. table.concat(cmds, ";")
  end,
},

qRoC avatar Feb 22 '23 08:02 qRoC

@qRoC It works! But I currently have return "colorscheme " .. vim.g.colors_name inside save_extra_cmds in order to put a persistent colorscheme by session. It is possible to implement those two things (persistent colorscheme and persistent tab names) inside save_extra_cmds? I'm not a master in Lua.

sergiornelas avatar Mar 16 '23 07:03 sergiornelas

@sergiornelas Hi, try:

save_extra_cmds = {
  -- tabby: tabs name
  function()
    local cmds = {}
    for _, t in pairs(vim.api.nvim_list_tabpages()) do
      local tabname = require("tabby.feature.tab_name").get_raw(t)
      if tabname ~= "" then
        table.insert(cmds, 'require("tabby.feature.tab_name").set(' .. t .. ', "' .. tabname:gsub('"', '\\"') .. '")')
      end
    end

    if #cmds == 0 then
      return ""
    end

    return "lua " .. table.concat(cmds, ";")
  end,
  -- colorscheme
  function()
    return "colorscheme " .. vim.g.colors_name
  end,
},

qRoC avatar Mar 16 '23 12:03 qRoC

@qRoC works as expected, but I found new a bug. When you create and rename 3 tabs, if you delete the tab number 1 or 2 and reload the session it breaks.

sergiornelas avatar Mar 16 '23 17:03 sergiornelas

Change

if tabname ~= "" then
  table.insert(cmds, 'require("tabby.feature.tab_name").set(' .. t .. ', "' .. tabname:gsub('"', '\\"') .. '")')
end

to

if tabname ~= "" then
  table.insert(
    cmds,
    'pcall(require("tabby.feature.tab_name").set, ' .. t .. ', "' .. tabname:gsub('"', '\\"') .. '")'
  )
end

:)

qRoC avatar Mar 16 '23 17:03 qRoC

@qRoC It doesn't break, however now after you delete the second tab and reload the session, the name of the second tab (previously was the third), disappears. 😬

sergiornelas avatar Mar 16 '23 18:03 sergiornelas

Sorry for being late! 😭 The ids of tabs are not stable in every neovim startup. If want to persist the names, we should use the numbers of tabs.

nanozuki avatar Mar 17 '23 08:03 nanozuki

The problem can be solved by storing all the tabby titles in an array using tab number(1~n) instead of tab handle, as keeping the ordering has nothing to do with storing tab handles. (as stated by the author)

So no need to change anything in tabby.nvim side. This is not a bug. Tabby.nvim does a great job only taking the tab handle as the argument, which conforms to the APIs Neovim provides.

-- storing.
for tabn, tabh in pairs(vim.api.nvim_list_tabpages()) do
  tabby[tabn] = require('tabby.feature.tab_name').get_raw(tabh)
end

-- restoring.
for tabn, tabh in pairs(vim.api.nvim_list_tabpages()) do
  if tabby[tabn] then
    require('tabby.feature.tab_name').set(tabh, tabby[tabn])
  end
end

nyngwang avatar Mar 17 '23 09:03 nyngwang

Sorry for being late; I'm too busy this year. I use JSON encode/decode to save and restore the tab names to vim global variables. Now, we can restore tab names from the session by addingglobals to sessionoptions!

nanozuki avatar Sep 21 '23 06:09 nanozuki