tmux-plugins / tmux-resurrect

Persists tmux environment across system restarts.
MIT License
10.99k stars 412 forks source link

Neovim sessions lost on restart #421

Open norogoth opened 2 years ago

norogoth commented 2 years ago

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 commented 2 years ago

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

erikc96 commented 2 years ago

I'm having this issue

sajadspeed commented 9 months ago

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.
SamuelCano03 commented 6 months ago

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?

hlspablo commented 4 months ago

@SamuelCano03 did you find a solution for this?

SamuelCano03 commented 4 months ago

@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

hlspablo commented 4 months ago

@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

pbower commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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:

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!