在Emacs shell模式下自动完成

在GNOMEterminal中,Bash做了智能的自动完成。 例如

apt-get in<TAB> 

 apt-get install 

在Emacs shell模式下,即使在显式地input/etc/bash_completion之后,这个自动完成也/etc/bash_completion 。 上面的例子在当前目录中粘贴或自动完成,而不是有效的apt-get命令选项。 据推测,这是因为Emacs拦截了Tab键。 如何在shell-mode启用智能自动完成shell-mode

我知道这个问题已经三年了,但这也是我一直想解决的问题。 networkingsearch引导了我一个elisp,使Emacs使用bash在shell模式下完成。 无论如何,它对我都有效。

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

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

要解决这个问题,只需在shell中input“dirs”,然后就可以同步了。

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

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

然后只是按Esc返回resyncs自动完成。

我不知道这个答案。 但是它不能正常工作的原因可能是因为emacs shell中的完成是由emacs在内部处理的(由comint-dynamic-complete函数完成),而且没有内置的智能完成函数。

恐怕修复不是一件容易的事情。

编辑:njsf的使用术语模式的build议可能是一样好。 从开始

  Mx期限 

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

就像Matli说的那样,这不是一件容易的事情,因为bash是以–noediting开始的,而TAB必然是comint-dynamic-complete。

一个可能会重新绑定TAB自我插入命令在shell-comand-hook与local-set-key,并使shell模式不启动 – 通过Mx自定义variablesRET显式-bash-args编号,但我怀疑在所有其他编辑中都不会很好。

你可能想尝试术语模式,但它有另外一些问题,因为一些其他的常规键绑定被术语模式取代。

编辑:由其他常规关键字被术语模式超越,我的意思是除了抄送成为逃生能够切换缓冲区。 所以,而不是Cx k杀死缓冲区,你必须Cc Cx k。 或者切换到另一个缓冲区“Cc Cx o”或“Cc Cx 2”

请考虑另一个模式Mx term ,就像我在2011年遇到问题时那样做了。我试图把当时Inet的所有努力都用Bash完成,包括这个问题。 但是,由于在term-mode发现替代scheme,我不想尝试eshell

它是完整的terminal模拟器,所以你可以运行里面的交互式程序,如午夜指挥官。 或者切换到zsh完成,这样你就不会浪费在Emacsconfiguration上的时间 – 一次使用所有的生命。

有了这个特点 – TAB在bash中完成,你可以免费获得。 但更重要的是,您可以获得完整的Readlinefunction,如增量或前缀命令search 。 为了使这个设置更方便使用检查我的.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 "\Cx\Ce": edit-and-execute-command $endif # Define my favorite Emacs key bindings. "\C-@": set-mark "\Cw": kill-region "\Mw": 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 "\Cp": history-search-backward "\eOA": history-search-backward "\e[B": history-search-forward "\Cn": 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 (YEA!在~/.bash_history有从bash中的~/.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\Ch")) (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) 

因为任何常见的命令如Cx o不能在terminal仿真模式下工作我扩展键盘映射:

 (unless (ignore-errors (require 'ido) (ido-mode 1) (global-set-key [?\sd] #'ido-dired) (global-set-key [?\sf] #'ido-find-file) t) (global-set-key [?\sd] #'dired) (global-set-key [?\sf] #'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 Key中使用.xmodmaprc来创buildsuper密钥:

 ! 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个命令: Cc Cj – 在缓冲区中进入正常的Emacs编辑模式 – 我用它来复制或者擦除, Cc Ck – 回到terminal仿真模式。

鼠标select和Shift-Insert工作在xterm

我使用前奏,当我打中元+ Tab它完成了我。

另外,Ctrl +我似乎做同样的事情。

我使用舵模式。 它有这个function(按“TAB”之后): 在这里输入图像说明

我没有声称是一个emacs专家,但这应该解决您的问题:

创build:〜/ .emacs

添加到:

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

Emacs接pipe了这个shell,所以BASH的设置没有通过。 这将为EMACS本身设置自动完成。