Answers:
我知道这个问题已经三岁了,但是我也一直对解决这个问题感兴趣。一次网络搜索将我引向了elisp的片段,该片段使Emacs在外壳模式下使用bash完成操作。无论如何,它对我有用。
M-x package-list-packages
,但是最后它对我不起作用,我不知道为什么,可能是我的emacs(24.3.1)或bash(4.3.30)版本。该文档说“ bash-completion.el对操作系统和BASH版本非常敏感...”,然后列出了我的版本不存在的列表。
在emacs shell中,实际上是emacs执行自动完成,而不是bash。如果shell和emacs不同步(例如,通过使用push,popd或某些bash用户函数来更改shell的当前目录),则自动完成功能将停止工作。
要解决此问题,只需在外壳程序中键入“ dirs”,然后一切恢复同步。
.emacs中也包含以下内容:
(global-set-key "\M-\r" 'shell-resync-dirs)
然后只需按Esc-return即可重新同步自动完成。
就像Matli所说的那样,这不是一件容易的事,因为bash以--noediting开始,而TAB必然是comint-dynamic-complete。
一个人可能可以使用本地设置键将TAB重新绑定到shell-comand-hook的self-insert-command中,并使shell模式不以--noxing开头,由Mx custom-variable RET显式bash-args进行编辑,但我怀疑与所有其他编辑配合使用时效果不佳。
您可能想尝试term-mode,但是它还有另一组问题,因为term-mode已取代了其他一些常规的键绑定。
编辑:通过术语模式超越其他常规的键盘设置,我的意思是除Cc之外的所有密钥都可以转储缓冲区。因此,您不必Cx k来杀死缓冲区,而必须Cc Cx k。或切换到另一个缓冲区“ Cc Cx o”或“ Cc Cx 2”
请考虑另一种模式M-x term
,就像我在2011年遇到问题时所做的那样。当时我试图在Inet上进行所有努力,以使Shell与Bash完成一起工作,包括这个问题。但是因为面对面对发现替代方案,term-mode
我甚至不想尝试eshell
。
它是完整的终端模拟器,因此您可以在内部运行交互式程序,例如Midnight commander。或切换到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_history
)中的任何单词在Bash中都有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
key,因此term-raw-map
其他任何keymap都可能与我的key绑定不冲突。要从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编辑模式(用于复制或grep缓冲文本),C-c C-k
-返回终端仿真模式。
鼠标选择和Shift-Insert
工作方式与相同xterm
。
.inputrc
部分!
我知道这个职位已经超过11年了。但是我创建了一个函数来在Emacs中完成本机外壳程序。它只是将Tab键发送到基础进程并截取输出,因此它与您在Shell本身中获得的完全相同。
https://github.com/CeleritasCelery/emacs-native-shell-complete
我没有声称自己是emacs专家,但这应该可以解决您的问题:
创建:〜/ .emacs
添加到它:
(需要'shell-command)(shell-command-completion-mode)
Emacs接管了外壳程序,因此BASH设置不会保留下来。这将为EMACS本身设置自动完成。
eshell-mode
其中有制表符补全。此处的更多信息:masteringemacs.org/articles/2010/11/01/…–