简体   繁体   English

不要在shell中为vim自动完成文件名中的某些扩展

[英]Do not autocomplete certain extensions from file names in shell for vim

I've noticed a really cool feature of bash-completion in bash. 我注意到bash中bash完成的一个非常酷的功能。 Let's say I have a directory with files 假设我有一个包含文件的目录

a.java
a.class
b.java
b.class

if I start typing vim a Tab , bash will autocomplete a.java . 如果我开始键入vim a Tab ,bash将自动完成a.java It knows I don't want to edit a.class 它知道我不想编辑a.class

I was wondering how it achieves this behavior. 我想知道它是如何实现这种行为的。 In my bash_completion.d directory there are a bunch of completion files, but vim does not have one. 在我的bash_completion.d目录中有一堆completion文件,但vim没有。 How does vim achieve this behavior, and how do I modify it to include other file extensions to ignore? vim如何实现此行为,如何修改它以包含其他要忽略的文件扩展名?

On Ubuntu, if I install the package bash-completion , I can see its contents with: 在Ubuntu上,如果我安装包bash-completion ,我可以看到它的内容:

$ dpkg -L bash-completion

In the output, /etc/bash_completion is listed. 在输出中,列出了/etc/bash_completion
Inside, the following command is written: 在里面,写入以下命令:

. /usr/share/bash-completion/bash_completion

It sources the file /usr/share/bash-completion/bash_completion . 它来源文件/usr/share/bash-completion/bash_completion

The location of these files vary from an OS to another: 这些文件的位置操作系统而异

Note: I use a Mac, and on macOS the bash-completion file is stored (by default) in /usr/local/etc/bash_completion , and the bash_completion.d directory is in /usr/local/etc/bash_completion.d 注意:我使用Mac,在macOS上,bash-completion文件(默认情况下)存储在/usr/local/etc/bash_completionbash_completion.d目录位于/usr/local/etc/bash_completion.d

On my system, /usr/share/bash-completion/bash_completion contains this line: 在我的系统上, /usr/share/bash-completion/bash_completion包含以下行:

_install_xspec '*.@(o|so|so.!(conf|*/*)|a|[rs]pm|gif|jp?(e)g|mp3|mp?(e)g|avi|asf|ogg|class)' vi vim gvim rvim view rview rgvim rgview gview emacs xemacs sxemacs kate kwrite

I think this line is responsible for the behavior you're observing. 我认为这一行是你所观察到的行为的原因。

If you want to tweak it to make bash exclude the foo and bar extensions when completing a filename after the $ vim command, you could try the following procedure . 如果你想调整它以使bash在$ vim命令之后完成文件名时排除foobar扩展名,你可以尝试以下过程

  1. Create the file ~/.bash_completion 创建文件~/.bash_completion

Inside the latter, write: 在后者内部,写道:

for bcfile in ~/.bash_completion.d/* ; do
  [ -f "$bcfile" ] && . $bcfile
done
  1. Create the ~/.bash_completion.d/ directory. 创建~/.bash_completion.d/目录。

  2. Inside this directory, create a vim file. 在此目录中,创建一个vim文件。

Inside the latter, write: 在后者内部,写道:

complete -f -X '*.@(o|so|so.!(conf|*/*)|a|[rs]pm|gif|jp?(e)g|mp3|mp?(e)g|avi|asf|ogg|class|foo|bar)' vi vim gvim rvim view rview rgvim rgview gview

complete is a bash builtin command which allows you to specify how arguments to a command name should be completed. complete是一个bash builtin命令,它允许您指定如何完成命令名称的参数。
-f is a shorthand for -A file , which specifies to bash that you want to see only filenames in your suggestions. -f-A file的简写,它指定要阻止您只想在建议中查看文件名。
-X excludes anything which matches the following pattern. -X排除任何符合以下模式的内容。

Note that I have merely copied the pattern used in /usr/share/bash-completion/bash_completion , and added the foo and bar extensions: 请注意,我只是复制了/usr/share/bash-completion/bash_completion ,并添加了foobar扩展:

*.@(o|so|so.!(conf|*/*)|a|[rs]pm|gif|jp?(e)g|mp3|mp?(e)g|avi|asf|ogg|class|foo|bar)
                                                                           ^^^^^^^

It's up to you to modify the pattern to exclude the real extensions you want to avoid. 您可以修改模式以排除要避免的实际扩展。

The names after the pattern tell bash for which commands should it exclude these extensions. 模式后面的名称告诉bash哪些命令应该排除这些扩展。
In the previous command, they are: 在上一个命令中,它们是:

vi vim gvim rvim view rview rgvim rgview gview

All of them invoke a version of Vi or Vim. 他们都调用了Vi或Vim的版本。 You could add other editor names at the end. 您可以在最后添加其他编辑器名称。

For more information see: 有关更多信息,请参阅

$ man bash

Look for the READLINE section and the Programmable Completion subsection, as well as the description of the complete builtin in the SHELL BUILTIN COMMANDS section. 查找READLINE部分和Programmable Completion子部分,以及SHELL BUILTIN COMMANDS部分中complete内置的描述。

See also An introduction to bash completion part 1 and An introduction to bash completion part 2 . 另见bash完成第1部分 介绍和bash完成第2部分介绍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM