How Electerm Gets the Current Working Directory (pwd) in Terminal When "SFTP Follow Terminal Path" Is Enabled
ZHAO Xudong edited this page 2026-01-21 18:44:29 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

How Electerm Gets the Current Working Directory (pwd) in Terminal When "SFTP Follow Terminal Path" Is Enabled / Electerm 如何在启用 "SFTP 跟随终端路径" 时获取终端中的当前工作目录 (pwd)

Language / 语言: English | 中文


English

Overview

When you enable the "SFTP follow terminal path" feature in Electerm, the application synchronizes your SFTP panel with your terminal's current working directory (CWD). This ensures that the SFTP file browser always shows the files in the directory you're working in.

To achieve this synchronization safely and reliably, Electerm uses modern Shell Integration via OSC 633 escape sequences. This is a standard protocol (originally from VS Code) that allows terminals to communicate shell state information without interfering with user commands or terminal history.

How Shell Integration Works

Shell Integration uses special escape sequences that the shell emits automatically when certain events occur:

  • OSC 633 ; A - Prompt started
  • OSC 633 ; B - Command input started
  • OSC 633 ; C - Command execution started
  • OSC 633 ; D ; - Command finished
  • OSC 633 ; E ; - The command line being executed
  • OSC 633 ; P ; Cwd= - Current working directory

When "SFTP follow terminal path" is enabled, Electerm listens for the OSC 633 ; P ; Cwd= sequence. This tells Electerm exactly what the current working directory is, without needing to inject any commands.

Why This Method Is Safe

Shell Integration provides several advantages:

  1. No Command Injection: It doesn't add any commands to your terminal history or output.
  2. No Interference: It works invisibly in the background without affecting your typing or command execution.
  3. Reliable: It provides accurate CWD information regardless of shell type, prompt customization, or complex directory changes.
  4. Standard Protocol: OSC 633 is a widely supported standard used by modern terminals and editors.

Requirements

Shell Integration requires:

  • A compatible shell (bash, zsh, fish, etc.) with shell integration enabled
  • A terminal emulator that supports OSC 633 sequences
  • The shell integration scripts loaded (usually via ~/.bashrc, ~/.zshrc, etc.)

Electerm automatically detects and uses shell integration when available. If shell integration is not available, the feature may fall back to other methods or be disabled.

Summary

Electerm uses OSC 633 Shell Integration to safely track your terminal's current working directory for SFTP synchronization. This modern approach ensures:

  • Clean terminal history
  • Reliable directory tracking
  • No interference with user commands
  • Compatibility with advanced shell features

中文

概述

当您在 Electerm 中启用 "SFTP 跟随终端路径" 功能时,应用程序会将您的 SFTP 面板与终端的当前工作目录 (CWD) 同步。这确保 SFTP 文件浏览器始终显示您正在工作的目录中的文件。

为了安全可靠地实现这种同步Electerm 使用现代 Shell Integration 通过 OSC 633 转义序列。这是一个标准协议(最初来自 VS Code允许终端在不干扰用户命令或终端历史的情况下传达 shell 状态信息。

Shell Integration 如何工作

Shell Integration 使用 shell 在某些事件发生时自动发出的特殊转义序列:

  • OSC 633 ; A - 提示符开始
  • OSC 633 ; B - 命令输入开始
  • OSC 633 ; C - 命令执行开始
  • OSC 633 ; D ; - 命令完成
  • OSC 633 ; E ; - 正在执行的命令行
  • OSC 633 ; P ; Cwd= - 当前工作目录

当启用 "SFTP 跟随终端路径" 时Electerm 监听 OSC 633 ; P ; Cwd= 序列。这告诉 Electerm 当前工作目录是什么,而无需注入任何命令。

为什么这种方法是安全的

Shell Integration 提供了几个优势:

  1. 无命令注入:它不会向您的终端历史记录或输出添加任何命令。
  2. 无干扰:它在后台工作,不会影响您的输入或命令执行。
  3. 可靠:无论 shell 类型、提示符自定义还是复杂的目录更改,它都能提供准确的 CWD 信息。
  4. 标准协议OSC 633 是现代终端和编辑器广泛支持的标准。

要求

Shell Integration 需要:

  • 兼容的 shellbash、zsh、fish 等)并启用 shell integration
  • 支持 OSC 633 序列的终端仿真器
  • 加载 shell integration 脚本(通常通过 /.bashrc、/.zshrc 等)

Electerm 会在可用时自动检测并使用 shell integration。如果 shell integration 不可用,该功能可能会回退到其他方法或被禁用。

总结

Electerm 使用 OSC 633 Shell Integration 来安全地跟踪终端的当前工作目录以进行 SFTP 同步。这种现代方法确保:

  • 清洁的终端历史记录
  • 可靠的目录跟踪
  • 不干扰用户命令
  • 与高级 shell 功能的兼容性