Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Emacs shell模式下的Bash自动完成_Bash_Shell_Emacs_Autocomplete - Fatal编程技术网

Emacs shell模式下的Bash自动完成

Emacs shell模式下的Bash自动完成,bash,shell,emacs,autocomplete,Bash,Shell,Emacs,Autocomplete,在GNOME终端中,Bash执行智能自动完成。比如说 apt-get in<TAB> 在Emacs shell模式下,即使在我显式地使用source/etc/bash\u completion之后,这种自动完成也不起作用。上述示例在中保持为,或在当前目录中使用文件名自动完成,而不是有效的apt get命令选项。这大概是因为Emacs正在拦截Tab键的按下。如何在shell模式下启用智能自动完成功能?我不知道这个问题的答案。但它不能像您期望的那样工作的原因可能是因为emacs she

在GNOME终端中,Bash执行智能自动完成。比如说

apt-get in<TAB>

在Emacs shell模式下,即使在我显式地使用source
/etc/bash\u completion
之后,这种自动完成也不起作用。上述示例在中保持为
,或在当前目录中使用文件名自动完成,而不是有效的
apt get
命令选项。这大概是因为Emacs正在拦截Tab键的按下。如何在
shell模式下启用智能自动完成功能?

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

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

编辑:njsf关于使用术语模式的建议可能是最好的。从M-x项开始 M-x term
它包含在标准的emacs发行版中(至少在Ubuntu和Debian上包含在emacs21 common或emacs22 common中)。

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

创建:~/.emacs

加上:

(需要“shell”命令) (shell命令完成模式)


Emacs接管了shell,因此BASH设置无法执行。这将为EMACS本身设置自动完成。

正如Matli所说,这不是一项容易的任务,因为bash是从--noediting开始的,TAB绑定到comint dynamic complete

可以将TAB重新绑定到shell中的self-insert命令,并使用本地set键钩住 并使shell模式不以--noediting开始,由M-x定制变量RET explicit bash args执行,但我怀疑它不会适合所有其他编辑

您可能想尝试术语模式,但它有另一组问题,因为术语模式取代了其他一些常规键绑定


编辑:术语模式取代了其他常规按键,我指的是除了C-C之外的所有键,C-C成为能够切换缓冲区的转义。因此,您必须使用C-C-x k而不是C-x k来终止缓冲区。或者切换到emacs shell中的另一个缓冲区“C-C-x o”或“C-C-x 2”

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

要解决这个问题,只需在shell中键入'dirs',事情就会恢复同步

我的.emacs中还包含以下内容:

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

然后点击Esc返回重新同步自动完成。

我知道这个问题已经三年了,但我也一直对解决这个问题感兴趣。一次Web搜索让我找到了一段elisp,它让Emacs在shell模式下使用bash来完成。不管怎么说,这对我来说很有效


查看。

我使用Prelude,当我点击Meta+选项卡时,它为我完成


同样,CTRL+I似乎也做同样的事情。请考虑另一个模式<代码> M X项< /C>,就像我在2011中的HIT问题那样做的那样。当时,我试图通过Inet收集所有的努力,使shell能够使用Bash完成,包括这个问题。但是自从在

术语模式
面前发现替代方案以来,我甚至不想尝试
eshell

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

您可以在bash中免费完成制表符。但更重要的是,您可以获得完整的读线功能,如增量或带前缀的命令搜索。要使此设置更方便,请检查我的

.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
! 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
.bashrc
(是的!Bash中的dabbrev来自
~/.Bash_history
中的任何单词):

.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
在终端仿真模式下不工作,I扩展键映射具有:

(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
键,因此
术语原始映射
和任何其他键映射都不会与我的键绑定冲突。要使
super
键从左起
Win
键,我使用
.xmodmaprc

# 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
! 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
您只需记住两个命令:
C-C-j
-进入正常的Emacs编辑模式(用于复制或在缓冲区文本中进行grepping),
C-C-k
-返回终端仿真模式


鼠标选择和Shift Insert的工作方式与我使用helm模式时的工作方式相同。它具有以下功能(按“TAB”键后):
我知道这篇文章已经有11年的历史了。但是我已经创建了一个函数来在Emacs中完成本机shell。它只向底层进程发送一个tab键并截取输出,因此它与shell本身中的输出完全相同


不,这并不能解决问题。它只在shell命令下工作,不在shell模式下工作。此外,它不会启用问题中请求的智能完成(它只会完成命令和文件名)。shell命令完成模式执行文件名完成,默认情况下启用。我想要Bash的完成功能(可以扩展,包括apt get和svn的子命令)。self insert命令不是它:它插入制表符,而不是将制表键传递给Bash。我没有术语模式命令,也不知道从哪里获取它。这是对不同问题的一个很好的回答!谢谢我使用自动跳转,这就是为什么DIR不同步的问题。Chris Conway,是的,对这一个::-)+1,因为有勇气回答一个三岁但仍然完全相关的问题。谢谢这已经在melpa中,使用
M-x软件包列表软件包安装很简单,但最终它对我不起作用,我不知道为什么