You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现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 named TmuxOpen (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 named vim_session (the -d flag 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:

  1. Open test.c in Vim
  2. Run :TmuxOpen
  3. 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:

  1. Restart your shell (or run source ~/.bashrc) to load the function
  2. Open test.c with vim test.c
  3. Run :TmuxOpen inside Vim
  4. Vim will quit, and a new tmux session will automatically open with test.c loaded 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 shellescape for paths—it prevents crashes from spaces, parentheses, or special characters in file/directory names.

内容的提问来源于stack exchange,提问作者Rokner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:32:12