简体   繁体   English

Emacs shell模式在缓冲区中打开文件

[英]Emacs shell mode open file in buffer

My setup: 我的设置:

  • Emacs terminal mode ( emacs -nw ) Emacs终端模式( emacs -nw
  • inside it, use the shell mode (invoked with Mx ansi-term ) 在它里面,使用shell模式(用Mx ansi-term调用)
  • inside this shell, connect to a remote server with ssh 在这个shell中,用ssh连接到远程服务器

Suppose I'm browsing the remote server inside the shell and find a file I want to edit. 假设我正在浏览shell中的远程服务器并找到我想要编辑的文件。 Is there a command to open it as a parallel buffer/window? 是否有命令将其作为并行缓冲区/窗口打开?

The only method I know to open a file from the shell is to do emacs -nw again, which isn't quite convenient because a) I don't keep the shell open and b) it's really a different Emacs session, so for instance the "yank buffer" is different. 我知道从shell打开文件的唯一方法是再次执行emacs -nw ,这不太方便,因为a)我不保持shell打开,b)它实际上是一个不同的Emacs会话,例如“yank缓冲区”是不同的。

Edit: if there is a different/better way to work with a remote server with Emacs I'm just as interested; 编辑:如果有一个不同的/更好的方式来使用Emacs的远程服务器,我只是感兴趣; it's what I'm trying to do. 这就是我想要做的。

It's best to use tramp. 最好使用tramp。

I have this shortcut (I call it with smex ): 我有这个快捷方式(我用smex称它):

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

This opens a dired buffer on the remote. 这将在遥控器上打开一个直接缓冲区。 You just use it as any dired buffer. 你只需将它用作任何直接缓冲区。

I've had a function to open a term from dired for a while, but I've added an option to ssh from a tramp dired buffer just now: 我有一个函数从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")))

And this is the shortcut that I use: 这是我使用的快捷方式:

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

You could use tramp from within your original emacs session to browse the remote server via ssh, using dired. 您可以在原始emacs会话中使用tramp来使用dired通过ssh浏览远程服务器。 Then, any remote file you open is opened in your local emacs session. 然后,您打开的任何远程文件都会在本地emacs会话中打开。

If you prefer to avoid dired and want to browse with a shell, you can prepend the remote location (/name@host:/path/from/pwd) to the file name. 如果您希望避免使用dired并希望使用shell进行浏览,则可以将远程位置(/ name @ host:/ path / from / pwd)添加到文件名中。 You can automate that with a function. 您可以使用功能自动执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM