ChatGPT解决这个技术问题 Extra ChatGPT

Emacs shell 模式中的 Bash 自动完成

在 GNOME 终端中,Bash 进行智能自动完成。例如

apt-get in<TAB>

变成

apt-get install

在 Emacs shell 模式下,这种自动完成功能不起作用,即使在我显式源 /etc/bash_completion 之后也是如此。上面的示例粘贴为 in 或使用当前目录中的文件名自动完成,而不是有效的 apt-get 命令选项。据推测,这是因为 Emacs 正在拦截 Tab 按键。如何在 shell-mode 中启用智能自动完成?

对于任何刚接触 emacs 的人... emacs 中还有其他 shell 模式。例如具有制表符补全的 eshell-mode。更多信息:masteringemacs.org/articles/2010/11/01/…
eshell 的自动补全功能只适用于本地目录,如果你 ssh 到另一台机器,你会突然失去这个能力。

D
David Christiansen

我知道这个问题已经存在三年了,但我也一直有兴趣解决这个问题。网络搜索将我引向了一段 elisp,它使 Emacs 使用 bash 在 shell 模式下完成。无论如何,它对我有用。

https://github.com/szermatt/emacs-bash-completion 查看。


+1 因为有勇气回答一个三岁但仍然完全相关的问题。谢谢!
这已经在melpa melpa.org/#/bash-completion,使用M-x package-list-packages安装很简单,但最后它对我不起作用,我不知道为什么,可能是我的emacs(24.3.1)或bash(4.3 .30) 版本。文档说“bash-completion.el 对操作系统和 BASH 版本非常敏感......”然后是我的版本不存在的列表。
不幸的是,待处理的 issue #6 对于当前的 CLI 工具来说似乎不是那么有用。
该问题 6 已关闭为<至少大部分已修复/不需要它的示例>(释义)。
S
Steve Lacey

在 emacs shell 中,实际上是 emacs 进行自动完成,而不是 bash。如果 shell 和 emacs 不同步(例如,通过使用 pushd、popd 或一些改变 shell 当前目录的 bash 用户函数),那么自动完成将停止工作。

要解决这个问题,只需在 shell 中输入“dirs”,一切就会恢复同步。

我的 .emacs 中还有以下内容:

(global-set-key "\M-\r" 'shell-resync-dirs)

然后只需按 Esc-return 即可重新同步自动完成。


这是对不同问题的一个很好的回答!
谢谢。我使用自动跳转,这就是目录不同步的问题。
克里斯康威,是的,对于这个:emacs.stackexchange.com/questions/30550/… :-)
m
matli

我不知道这个问题的答案。但它不能按预期工作的原因可能是因为 emacs shell 中的补全是由 emacs 内部处理的(通过 comint-dynamic-complete 函数),并且没有内置那些智能补全函数。

恐怕这不是一件容易解决的事情。

编辑:njsf 关于使用 term-mode 的建议可能和它得到的一样好。开始它

M-x term


使用 M-x term 的制表符补全效果更差。
g
gavenkoa

请考虑另一种模式 M-x term,就像我在 2011 年遇到问题时所做的那样。当时我试图通过 Inet 集中所有努力,以使 shell 与 Bash 完成一起工作,包括这个问题。但是自从在 term-mode 面前发现替代品后,我什至不想尝试 eshell

它是完整的终端模拟器,所以你可以在里面运行交互式程序,比如午夜指挥官。或者切换到 zsh 完成,这样您就不会在 Emacs 配置上浪费时间。

您可以免费在 bash 中完成 TAB。但更重要的是,您可以获得完整的 Readline 功能,例如增量或前缀命令搜索。要使此设置更方便,请检查我的 .inputrc.bashrc.emacs

.inputrc 的基本部分:

# I like this!
set editing-mode emacs

# Don't strip characters to 7 bits when reading.
set input-meta on

# Allow iso-latin1 characters to be inserted rather than converted to
# prefix-meta sequences.
set convert-meta off

# Display characters with the eighth bit set directly rather than as
# meta-prefixed characters.
set output-meta on

# Ignore hidden files.
set match-hidden-files off

# Ignore case (on/off).
set completion-ignore-case on

set completion-query-items 100

# First tab suggests ambiguous variants.
set show-all-if-ambiguous on

# Replace common prefix with ...
set completion-prefix-display-length 1

set skip-completed-text off

# If set to 'on', completed directory names have a slash appended. The default is 'on'.
set mark-directories on
set mark-symlinked-directories on

# If set to 'on', a character denoting a file's type is appended to the
# filename when listing possible completions. The default is 'off'.
set visible-stats on

set horizontal-scroll-mode off

$if Bash
"\C-x\C-e": edit-and-execute-command
$endif

# Define my favorite Emacs key bindings.
"\C-@": set-mark
"\C-w": kill-region
"\M-w": copy-region-as-kill

# Ctrl+Left/Right to move by whole words.
"\e[1;5C": forward-word
"\e[1;5D": backward-word
# Same with Shift pressed.
"\e[1;6C": forward-word
"\e[1;6D": backward-word

# Ctrl+Backspace/Delete to delete whole words.
"\e[3;5~": kill-word
"\C-_": backward-kill-word

# UP/DOWN filter history by typed string as prefix.
"\e[A": history-search-backward
"\C-p": history-search-backward
"\eOA": history-search-backward
"\e[B": history-search-forward
"\C-n": history-search-forward
"\eOB": history-search-forward

# Bind 'Shift+TAB' to complete as in Python TAB was need for another purpose.
"\e[Z": complete
# Cycling possible completion forward and backward in place.
"\e[1;3C": menu-complete                    # M-Right
"\e[1;3D": menu-complete-backward           # M-Left
"\e[1;5I": menu-complete                    # C-TAB

.bashrc(是的!在 Bash 中有来自 ~/.bash_history 中的任何单词的 dabbrev):

set -o emacs

if [[ $- == *i* ]]; then
  bind '"\e/": dabbrev-expand'
  bind '"\ee": edit-and-execute-command'
fi

.emacs 使导航在术语缓冲区中舒适:

(setq term-buffer-maximum-size (lsh 1 14))

(eval-after-load 'term
  '(progn
    (defun my-term-send-delete-word-forward () (interactive) (term-send-raw-string "\ed"))
    (defun my-term-send-delete-word-backward () (interactive) (term-send-raw-string "\e\C-h"))
    (define-key term-raw-map [C-delete] 'my-term-send-delete-word-forward)
    (define-key term-raw-map [C-backspace] 'my-term-send-delete-word-backward)
    (defun my-term-send-forward-word () (interactive) (term-send-raw-string "\ef"))
    (defun my-term-send-backward-word () (interactive) (term-send-raw-string "\eb"))
    (define-key term-raw-map [C-left] 'my-term-send-backward-word)
    (define-key term-raw-map [C-right] 'my-term-send-forward-word)
    (defun my-term-send-m-right () (interactive) (term-send-raw-string "\e[1;3C"))
    (defun my-term-send-m-left () (interactive) (term-send-raw-string "\e[1;3D"))
    (define-key term-raw-map [M-right] 'my-term-send-m-right)
    (define-key term-raw-map [M-left] 'my-term-send-m-left)
    ))

(defun my-term-mode-hook ()
  (goto-address-mode 1))
(add-hook 'term-mode-hook #'my-term-mode-hook)

由于 C-x o 等任何常用命令在终端仿真模式下都不起作用,因此我扩展了键盘映射:

(unless
    (ignore-errors
      (require 'ido)
      (ido-mode 1)
      (global-set-key [?\s-d] #'ido-dired)
      (global-set-key [?\s-f] #'ido-find-file)
      t)
  (global-set-key [?\s-d] #'dired)
  (global-set-key [?\s-f] #'find-file))

(defun my--kill-this-buffer-maybe-switch-to-next ()
  "Kill current buffer. Switch to next buffer if previous command
was switching to next buffer or this command itself allowing
sequential closing of uninteresting buffers."
  (interactive)
  (let ( (cmd last-command) )
    (kill-buffer (current-buffer))
    (when (memq cmd (list 'next-buffer this-command))
      (next-buffer))))
(global-set-key [s-delete] 'my--kill-this-buffer-maybe-switch-to-next)
(defun my--backward-other-window ()
  (interactive)
  (other-window -1))
(global-set-key [s-up] #'my--backward-other-window)
(global-set-key [s-down] #'other-window)
(global-set-key [s-tab] 'other-window)

请注意,我使用 super 键,因此 term-raw-map 和可能的任何其他键映射都不会与我的键绑定冲突。要从左 Win 键创建 super 键,我使用 .xmodmaprc

! To load this config run:
!   $ xmodmap .xmodmaprc

! Win key.
clear mod3
clear mod4

keycode 133 = Super_L
keycode 134 = Hyper_R
add mod3 = Super_L
add mod4 = Hyper_R

您只需要记住 2 个命令:C-c C-j - 进入正常的 Emacs 编辑模式(用于在缓冲区文本中复制或 grepping),C-c C-k - 返回终端仿真模式。

鼠标选择和 Shift-Insert 的工作方式与 xterm 相同。


谢谢,这是金子!特别是 .inputrc 部分!
n
njsf

就像 Matli 说的那样,这不是一件容易的事,因为 bash 以 --noediting 开头,而 TAB 绑定到 comint-dynamic-complete。

可以使用 local-set-key 将 TAB 重新绑定到 shell-comand-hook 中的 self-insert-command,并使 shell-mode 不以 Mx customize-variable RET explicit-bash-args 的 --noediting 开头,但我怀疑它不适用于所有其他编辑。

您可能想尝试 term-mode,但它还有另一组问题,因为一些其他常规键绑定已被 term-mode 取代。

编辑:通过术语模式取代其他常规键标,我的意思是除了 Cc 之外的所有键,它成为能够切换缓冲区的转义。因此,您必须 Cc Cx k 代替 Cx k 来杀死缓冲区。或者切换到另一个缓冲区 'Cc Cx o' 或 'Cc Cx 2'


self-insert-command 不是:它插入 TAB 字符,而不是将 TAB 按键传递给 Bash。
我没有 term-mode 命令,也不知道从哪里得到它。
P
Prgrm.celeritas

我知道这篇文章现在已经超过 11 年了。但是我创建了一个函数来在 Emacs 中完成本机 shell。它只是向底层进程发送一个制表键并拦截输出,因此它与您在 shell 本身中得到的完全相同。

https://github.com/CeleritasCelery/emacs-native-shell-complete


哇!惊人 ! (基于预览的初步印象。)
我喜欢它。太感谢了。如果因为有多个候选项而无法完成单个命令的 TAB 时,这些候选项会像在 Bash 中一样出现在 Emacs 中,那就更好了。
你的意思是喜欢在终端的大列表中而不是使用 Emacs 完成显示?
d
duma

我使用 Prelude,当我点击 Meta+Tab 时,它就为我完成了。

此外, Ctrl+i 似乎做同样的事情。


A
Alexei

https://i.stack.imgur.com/OeILH.png


S
Scott Alan Miller

我没有声称自己是 emacs 专家,但这应该可以解决您的问题:

创建:~/.emacs

添加到它:

(需要'shell-command) (shell-command-completion-mode)

Emacs 接管了外壳,因此 BASH 设置不会执行。这将为 EMACS 本身设置自动完成。


不,这并不能解决问题。它仅适用于 shell 命令,不适用于 shell 模式。此外,它不会启用问题中请求的智能完成(它只会完成命令和文件名)。
shell-command-completion-mode 执行文件名完成并默认启用。我想要 Bash 的补全功能(可扩展为包括例如 apt-get 和 svn 的子命令)。