我在 Windows 7 上使用 Emacs 23.3.1。我知道我可以使用 Mx shell 从 emacs 运行 shell。我想同时拥有多个 shell 窗口,但第二次键入 Mx shell 只会打开同一个 shell 窗口。
有没有办法拥有不同的外壳窗口?
Cu Mx 外壳会做到这一点。
它将提示输入新 shell 的名称,只需按默认值返回即可(类似于 *shell*<2>
.
也适用于 eshell。
另一个技巧,如果你使用 eshell:就像 Mx eshell 带你回到 *eshell*
(而不是开始一个新的 eshell),如果你使用一个数字前缀参数,它会带你到那个 eshell 缓冲区.例如,C-3Mxeshell 会将您带到 *eshell*<3>
。可悲的是,如果您使用 shell(而不是 eshell),这个技巧似乎不起作用(至少在我的 Emacs 24.0.50.1 中。)
您可以使用 Mx rename-buffer 重命名 shell 的缓冲区。然后您将能够启动第二个 shell。
四年多后,我看到有些人有时还在关注这个问题,所以我将发布一个我写的快速函数,用于加载一个 shell 并询问它的名称。这样,如果一个 shell 专门用于对文件进行排序,您可以将其命名为“sort-files”,如果它专门用于运行 hive 查询,则可以将其命名为“hive”。我现在每天都使用它(在 emacs 24 上):
(defun create-shell ()
"creates a shell with a given name"
(interactive);; "Prompt\n shell name:")
(let ((shell-name (read-string "shell name: " nil)))
(shell (concat "*" shell-name "*"))))
这将在您碰巧使用的任何缓冲区中自动生成一个新的 shell 实例;将它绑定到 MS 或类似的东西,然后立即快乐:
(defun new-shell ()
(interactive)
(let (
(currentbuf (get-buffer-window (current-buffer)))
(newbuf (generate-new-buffer-name "*shell*"))
)
(generate-new-buffer newbuf)
(set-window-dedicated-p currentbuf nil)
(set-window-buffer currentbuf newbuf)
(shell newbuf)
)
)
非常感谢 phils 推荐使用 let 重写,即使结果是更糟糕的括号...:\
let
-绑定变量。现在,您现在拥有 currentbuf
和 newbuf
的全局值。
这将在您每次调用该函数时打开一个新的 shell,并在需要时自动重命名它。添加的优点是,如果您正在远程编辑文件(dired/tramp...),这将在远程主机上打开一个 shell 并使用远程主机名自动重命名:
(defun ggshell (&optional buffer)
(interactive)
(let* (
(tramp-path (when (tramp-tramp-file-p default-directory)
(tramp-dissect-file-name default-directory)))
(host (tramp-file-name-real-host tramp-path))
(user (if (tramp-file-name-user tramp-path)
(format "%s@" (tramp-file-name-user tramp-path)) ""))
(new-buffer-nameA (format "*shell:%s*" host))
(new-buffer-nameB (generate-new-buffer-name new-buffer-nameA))
(currentbuf (get-buffer-window (current-buffer)))
)
(generate-new-buffer new-buffer-nameB)
(set-window-dedicated-p currentbuf nil)
(set-window-buffer currentbuf new-buffer-nameB)
(shell new-buffer-nameB)
))
C-u
运行命令universal-argument
。这是一种将参数注入下一个命令的方法。您可以使用C-h k C-u
了解更多信息(C-h k
运行describe-key
,非常方便!)*shell*<2> 3038 Shell (shell<1> run) ~/
并且当您尝试退出时,emacs 将 shell 称为 shell<1>即使名称是 shell<2>。当然,我可以编辑建议的默认名称参数,但我不这样做。