1

My current workflow is often to do things in a terminal and open/change relevant files in a text editor. Transitioning from VSCode to Emacs I am looking for a similar workflow that I got used to when using VSCode. One of these things was navigating my directories in a VSCode terminal to search for the relevant file and simply opening it in the editor, i.e. code file.txt, and proceed to make changes.

I experienced a similar workflow using Emacs term, where I could simply navigate my folders and use C-x C-f (or rather C-c C-f when in the term-buffer) to open file as it defaulted to the current directory of term. However, when trying out vterm as I needed a better terminal emulato, I found that C-x C-f does not default to the current directory in vterm. This means that I have to manually navigate to the file I want to open in Emacs, which can be quite cumbersome.

Is there a way to configure vterm to export the current directory to the find-file functionality of Emacs? Or should I use term for simple directory navigation and vterm for more complex terminal emulation?

3
  • This sounds like you might want to enable dirtrack-mode. Alternatively, you can enable the emacs server, and then use emacsclient <file> from within vterm to edit files.
    – rpluim
    Commented Sep 22, 2022 at 11:02
  • dirtrack-mode does not seem to work out of the box, but emacsclient <file> does appear to work! Commented Sep 22, 2022 at 11:37
  • Yes, dirtrack-mode only works with modes derived from comint, which vterm isn't. But I found a better option, which I'll add as an answer
    – rpluim
    Commented Sep 22, 2022 at 11:48

1 Answer 1

1

The README shipped with vterm describes how to do this in detail by sending special escape sequences in your prompt.

Cribbed from there:

vterm_printf(){
    if [ -n "$TMUX" ] && ([ "${TERM%%-*}" = "tmux" ] || [ "${TERM%%-*}" = "screen" ] ); then
        # Tell tmux to pass the escape sequences through
        printf "\ePtmux;\e\e]%s\007\e\\" "$1"
    elif [ "${TERM%%-*}" = "screen" ]; then
        # GNU screen (screen, screen-256color, screen-256color-bce)
        printf "\eP\e]%s\007\e\\" "$1"
    else
        printf "\e]%s\e\\" "$1"
    fi
}

vterm_prompt_end(){
    vterm_printf "51;A$(whoami)@$(hostname):$(pwd)"
}
PS1=$PS1'\[$(vterm_prompt_end)\]'
3
  • 1
    This works great! It had never occurred to me that packages installed with package-install contain README files in the ~/.emacs.d/elpa/<package> directory. This will undoubtedly be useful in the future. Commented Sep 22, 2022 at 12:47
  • Is there a way to do the vice versa? As in, I have an already running vterm process and want to open a vterm starting in same directory as my current buffer's? Commented Mar 9 at 6:57
  • @user129393192 I'm not sure I understand: M-x vterm switches to an existing vterm, C-u M-x vterm creates a new session in the same directory as the current buffer.
    – rpluim
    Commented Mar 11 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.