98

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

apt-get in<TAB>

变成

apt-get install

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

4

9 回答 9

96

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

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

于 2011-11-16T13:52:48.953 回答
21

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

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

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

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

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

于 2008-10-21T06:24:00.700 回答
16

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

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

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

Mx 项
它包含在标准 emacs 发行版中(至少在 Ubuntu 和 Debian 上包含在 emacs21-common 或 emacs22-common 中)。

于 2008-10-02T18:09:29.513 回答
9

请考虑另一种模式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 中的任何单词都有 dabbrev ~/.bash_history):

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)

请注意,我使用superkey,因此term-raw-map可能任何其他键映射都不会与我的键绑定冲突。super要从左键制作钥匙,Win我使用.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.

于 2015-02-19T23:06:28.060 回答
6

就像 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'

于 2008-10-03T02:55:07.053 回答
3

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

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

于 2020-01-16T06:59:54.900 回答
1

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

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

于 2013-04-16T02:45:21.393 回答
0

我使用掌舵模式。它具有此功能(按“TAB”后): 在此处输入图像描述

于 2017-02-08T09:04:39.077 回答
-2

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

创建:~/.emacs

添加到它:

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

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

于 2008-10-02T19:16:17.980 回答