tmux-resurrect icon indicating copy to clipboard operation
tmux-resurrect copied to clipboard

Neovim sessions lost on restart

Open norogoth opened this issue 3 years ago • 10 comments

I am on Debian 11 using tmux-ressurect with neovim .5. All files are edited through SSHFS. However this happens with files that are only local. If I save the session, close the terminal window, reopen terminal and tmux and resurrect, then all my panes and vim sessions within open up without issue. However, if I restart my computer, then when I open tmux and resurrect, I get all my panes and windows, but I have each pane set to the last dir but no neovim sessions are actually open. This adds a considerable amount of time to my work as I have to hunt down each and every file again versus just hitting the ground running every day.

Thank you very much for your assistance on this important issue and thank you for this very useful plugin.

.tmux.conf - https://pastebin.com/mC9ZSsad last - https://pastebin.com/2DtEQ9MW

norogoth avatar Dec 02 '21 17:12 norogoth

Important! I found that this is happening because tmux relies on files in /tmp that are being deleted on restart.

norogoth avatar Dec 03 '21 15:12 norogoth

I'm having this issue

erikc96 avatar Jun 11 '22 05:06 erikc96

Do not use appimage to fix this problem.

  1. Download nvim-linux64.tar.gz
  2. Extract wherever you want.
  3. chmod +x /home/user/Apps/nvim-linux64/bin/nvim
  4. Create link: sudo ln -s /home/user/Apps/nvim-linux64/bin/nvim /usr/local/bin/nvim -- Be careful to complete the nvim executable file path.

sajadspeed avatar Oct 20 '23 13:10 sajadspeed

I have the following similar error. Opened a nvim session in nvim. All fine. image

I kill tmux sessions with kill-session as you can see (it's the same as restarting the computer): image

I open it with tmux resurrect and I got this: image

So as you can see, it appears that the nvim session got restored, but it's just text. there is a terminal session above and that's all i can do. I can't write in nvim session image

All i can do is clear the terminal.which is the same that no getting nvim session restored. any help?

SamuelCano03 avatar Jan 09 '24 04:01 SamuelCano03

@SamuelCano03 did you find a solution for this?

hlspablo avatar Mar 04 '24 17:03 hlspablo

@SamuelCano03 did you find a solution for this?

I don't remember exactly what I changed, but I ended up with a solution that implies getting nvim sessions restored but without buffers (in other words, you get an empty nvim session) my config related is in https://github.com/SamuelCano03/dotfiles

SamuelCano03 avatar Mar 05 '24 02:03 SamuelCano03

@SamuelCano03 did you find a solution for this?

I don't remember exactly what I changed, but I ended up with a solution that implies getting nvim sessions restored but without buffers (in other words, you get an empty nvim session) my config related is in https://github.com/SamuelCano03/dotfiles

Thanks for sharing your dotfiles

hlspablo avatar Mar 05 '24 13:03 hlspablo

Hi there,

Did anyone end up figuring this one out?

With neovim, the sessions open up blank after PC restart, rather than having the files open.

I have the NVIM stuff setup as per the Resurrect GITHUB:

Vim Tmux Resurrect

set -g @resurrect-strategy-vim 'session' set -g @resurrect-strategy-nvim 'session' set -g @resurrect-capture-pane-contents 'on' set -g @resurrect-dir '~/.tmux/envs' set -g @continuum-restore 'on'

pbower avatar Mar 15 '24 22:03 pbower

Ok, I've realised that the specific issue is that TMUX Resurrect is quite limited for Neovim, in that it only will resurrect the files if one opened the relevant file from terminal like ':nvim my_file' , rather than restoring the actual buffers that were open once within neovim.

TLDR: If one opens Neovim, and then opens a file via Telescope or similar, it only reloads 'Neovim', rather than the file.

pbower avatar Mar 15 '24 22:03 pbower

Ok for anyone who ran into this issue (a lot by a net search?), I developed a custom solution to this which works perfectly. I'm sure something could be adapted for use within the plugin, however at the moment I'm using my Vimscript config and that would need to migrate to the library for appropriate loading. Additionally, I'm not a massive fan of:

  1. The vimscript delay timer, but the syntax highlighting was disappearing on reload.
  2. The ton of Session.vim files. But I don't know if there is another way, and they can be .gitignored. I hid them at least.

How it works:

  • uses the session, window and pane indexes to uniquely identify the sessions.
  • create unique (hidden) Session.vim sessions for each combination.
  • when re-loading a TMUX window, the 'nvim' contents are now restored, regardless of if you 'Telescoped' into your specific file earlier.
  • the only downside is that barbar.nvim (or your favourite tabline plugin), probably clashes with it, as it also tries to load the buffers from the Session file into individual cases. But, given that TMUX is already handling windowing functionality, the way I got around this is by saying:
if empty(getenv('TMUX')) 
     Plug 'romgrk/barbar.nvim'
endif

... on each bit of barbar.nvim config, then just mapping the equivalent commands in Tmux so that there's no context switching needed. That way, when not using TMUX, one gets Barbar, otherwise the full throttle.

SOLUTION:

  1. Put this in ~/.config/nvim/init.vim :
" Nvim Session loader for TMUX.
function! ConstructNvimSessionFileName()
  let session_id_raw = substitute(system('tmux display-message -p "#{session_id}"'), '\n', '', '')
  let session_id = substitute(session_id_raw, '^\$', '', '')
  let window_index = substitute(system('tmux display-message -p "#{window_index}"'), '\n', '', '')
  let pane_index = substitute(system('tmux display-message -p "#{pane_index}"'), '\n', '', '')
  return getcwd() . '/.TmuxNvimSession-' . session_id . '-' . window_index . '-' . pane_index . '.vim'
endfunction

function! StartTmuxPaneNvimSession()
  if exists("$TMUX_PANE")
    let session_file = ConstructNvimSessionFileName()
    "echo 'Attempting to load Tmux Pane Nvim Session from ' . session_file
    if filereadable(session_file)
      execute 'source ' . session_file
      echo "Loading Session from " . session_file
    endif
    execute 'Obsession ' . session_file
    echo "Saving Session to " . session_file
    " Reload buffer to re-enable syntax highlighting and a few issues
    call timer_start(100, {tid -> execute("bufdo if !empty(expand('%')) | e | endif")})
  endif
endfunction

if exists('$TMUX')
  autocmd VimEnter * call StartTmuxPaneNvimSession()
endif

  1. Replace ~/.tmux/plugins/tmux-resurrect/strategies/nvim_session.sh with this :
#!/usr/bin/env bash

# "nvim session strategy"
#
# Same as vim strategy, see file 'vim_session.sh'
#
# Adapted to work with hidden session files named after Tmux pane IDs


ORIGINAL_COMMAND="$1"
DIRECTORY="$2"

# Fetch current Tmux session, window, and pane identifiers
get_tmux_identifiers() {
    local session_id_raw=$(tmux display-message -p "#{session_id}")
    # Remove the dollar sign from the session ID
    local session_id=${session_id_raw#\$}
    local window_index=$(tmux display-message -p "#{window_index}")
    local pane_index=$(tmux display-message -p "#{pane_index}")
    echo "${session_id}-${window_index}-${pane_index}"
}

# Check if a specific Neovim session file exists
nvim_session_file_exists_for_identifiers() {
    local identifiers=$(get_tmux_identifiers)
    [ -e "${DIRECTORY}/.TmuxNvimSession-${identifiers}.vim" ]
}

nvim_session_file_exists() {
	[ -e "${DIRECTORY}/Session.vim" ]
}

original_command_contains_session_flag() {
	[[ "$ORIGINAL_COMMAND" =~ "-S" ]]
}

main() {
	if nvim_session_file_exists_for_identifiers; then
		local identifiers=$(get_tmux_identifiers)
		echo "nvim -S ${DIRECTORY}/.TmuxNvimSession-${identifiers}.vim"
	elif nvim_session_file_exists; then
		echo "nvim -S"
	elif original_command_contains_session_flag; then
		# Session file does not exist, yet the original nvim command contains
		# session flag `-S`. This will cause an error, so we're falling back to
		# starting plain nvim.
		echo "nvim"
	else
		echo "$ORIGINAL_COMMAND"
	fi
}
main

Let me know if this helps anyone!

pbower avatar Mar 16 '24 07:03 pbower