如何实现Vim命令:退出后在新tmux会话打开当前文件
Hey there! Let's tackle this problem step by step. You want a custom Vim command that quits Vim, then automatically launches a new tmux session, switches to Vim's original working directory, and opens the current file in a fresh Vim instance. Here are two solid approaches tailored to your needs:
Method 1: Direct Vimrc Command (No Shell Config Changes)
This approach creates a self-contained Vim command that sets up the tmux session in the background before quitting Vim.
Add this line to your ~/.vimrc:
command! TmuxOpen execute "silent !tmux new-session -d -s vim_session 'cd " . shellescape(getcwd()) . " && vim " . shellescape(expand('%:p')) . "'" | quit
Let's break down what each part does:
command! TmuxOpen: Creates a custom command namedTmuxOpen(the!overwrites any existing command with the same name).shellescape(getcwd()): Safely escapes your current working directory—critical for handling spaces or special characters in paths.shellescape(expand('%:p')): Grabs the absolute path of your open file and escapes it to avoid shell errors.tmux new-session -d -s vim_session: Spins up a detached tmux session namedvim_session(the-dflag keeps it running in the background so Vim doesn't hang).silent !: Runs the shell command quietly, skipping the annoying "Press ENTER to continue" prompt.| quit: Exits Vim immediately after triggering the tmux session.
How to use it:
- Open
test.cin Vim - Run
:TmuxOpen - Vim will quit—then just run this command to jump into your new tmux session:
tmux attach -t vim_session
Method 2: Vim + Shell Function (Auto-Attach After Quitting)
If you want a completely seamless experience (no extra tmux attach command), this method uses environment variables and a shell function to auto-launch the tmux session right after Vim exits.
Step 1: Add to your ~/.vimrc
command! TmuxOpen let $TMUX_TARGET_FILE = expand('%:p') | let $TMUX_TARGET_CWD = getcwd() | quit
This stores your file path and working directory in temporary environment variables, then quits Vim.
Step 2: Add a shell function to ~/.bashrc (or ~/.zshrc for Zsh)
vim() { # Run the regular Vim command command vim "$@" # Check if Vim set our target variables before exiting if [[ -n "$TMUX_TARGET_FILE" && -n "$TMUX_TARGET_CWD" ]]; then # Create a new tmux session, switch to the saved directory, and open the file tmux new-session -c "$TMUX_TARGET_CWD" -s vim_session "vim $TMUX_TARGET_FILE" # Clear the variables so they don't trigger accidentally later unset TMUX_TARGET_FILE TMUX_TARGET_CWD fi }
How to use it:
- Restart your shell (or run
source ~/.bashrc) to load the function - Open
test.cwithvim test.c - Run
:TmuxOpeninside Vim - Vim will quit, and a new tmux session will automatically open with
test.cloaded in Vim!
Quick Notes:
- If you already have a tmux session named
vim_session, the command will fail. To fix this, use a unique session name (e.g.,vim_session_$(date +%s)for a timestamped name). - Always use
shellescapefor paths—it prevents crashes from spaces, parentheses, or special characters in file/directory names.
内容的提问来源于stack exchange,提问作者Rokner

