【问题标题】:Emacs shell mode open file in bufferEmacs shell 模式在缓冲区中打开文件
【发布时间】:2014-02-26 16:33:52
【问题描述】:

我的设置:

  • Emacs 终端模式 (emacs -nw)
  • 在其中,使用 shell 模式(使用 M-x ansi-term 调用)
  • 在这个 shell 中,使用 ssh 连接到远程服务器

假设我正在 shell 中浏览远程服务器并找到我想要编辑的文件。是否有命令将其作为并行缓冲区/窗口打开?

我知道从shell打开文件的唯一方法是再次执行emacs -nw,这不太方便,因为a)我不保持shell打开并且b)它确实是一个不同的Emacs会话,所以例如“yank buffer”是不同的。

编辑:如果有一种不同/更好的方式来使用 Emacs 与远程服务器一起工作,我同样感兴趣;这就是我想要做的。

【问题讨论】:

    标签: shell emacs ssh terminal


    【解决方案1】:

    最好用tramp。

    我有这个快捷方式(我用smex 调用它):

    (defun connect-remote ()
      (interactive)
      (dired "/user@domain.com:/"))
    

    这会在遥控器上打开一个 dired 缓冲区。您只需将其用作任何 dired 缓冲区即可。

    我有一段时间可以从 dired 中打开一个术语,但我已经 刚刚添加了一个从tramp dired缓冲区进行ssh的选项:

    (defun dired-open-term ()
      "Open an `ansi-term' that corresponds to current directory."
      (interactive)
      (let ((current-dir (dired-current-directory)))
        (term-send-string
         (terminal)
         (if (file-remote-p current-dir)
             (let ((v (tramp-dissect-file-name current-dir t)))
               (format "ssh %s@%s\n"
                       (aref v 1) (aref v 2)))
           (format "cd '%s'\n" current-dir)))))
    
    (defun terminal ()
      "Switch to terminal. Launch if nonexistent."
      (interactive)
      (if (get-buffer "*terminal*")
          (switch-to-buffer "*terminal*")
        (term "/bin/bash")))
    

    这是我使用的快捷方式:

    (define-key dired-mode-map (kbd "`") 'dired-open-term)
    

    【讨论】:

    • 我无法使用 user@domain 作为参数调用 dired。它说它不存在...
    • 你用“user2460978@domain-or-ip-address:/”替换了吗?
    • 是的:如果我通常使用 $> ssh MyUser@MyServer 连接到服务器然后输入密码,我尝试使用 dired "/MyUser@MyServer:/" 并且回复是: Directory /home /xxx/"MyUser@MyServer:/" 无法访问或不存在
    • 我已经设法修复它。谢谢。我必须补充一点,Dired 是这份工作的天堂。立即感受 Emacs 的力量
    • 我收到以下错误:Setting current directory: No such file or directory
    【解决方案2】:

    您可以在原始 emacs 会话中使用 tramp 通过 ssh 浏览远程服务器,使用 dired。然后,您打开的任何远程文件都会在本地 emacs 会话中打开。

    如果您不想使用 dired 并希望使用 shell 浏览,您可以在文件名前添加远程位置 (/name@host:/path/from/pwd)。您可以使用函数将其自动化。

    【讨论】:

    • 您能详细说明一下这些选项吗?我是 Emacs 的新手。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2017-07-28
    • 2014-10-17
    相关资源
    最近更新 更多