Merge pull request #9642 from geekpi/master

translated
This commit is contained in:
geekpi 2018-08-01 09:10:51 +08:00 committed by GitHub
commit f5670ae464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 84 deletions

View File

@ -1,84 +0,0 @@
translating---geekpi
Incomplete Path Expansion (Completion) For Bash
======
![](https://4.bp.blogspot.com/-k2pRIKTzcBU/W1BpFtzzWuI/AAAAAAAABOE/pqX4XcOX8T4NWkKOmzD0T0OioqxzCmhLgCLcBGAs/s1600/Gnu-bash-logo.png)
[bash-complete-partial-path][1] enhances the path completion in Bash (on Linux, macOS with gnu-sed, and Windows with MSYS) by adding incomplete path expansion, similar to Zsh. This is useful if you want this time-saving feature in Bash, without having to switch to Zsh.
Here is how this works. When the `Tab` key is pressed, bash-complete-partial-path assumes each component is incomplete and tries to expand it. Let's say you want to navigate to `/usr/share/applications` . You can type `cd /u/s/app` , press `Tab` , and bash-complete-partial-path should expand it into `cd /usr/share/applications` . If there are conflicts, only the path without conflicts is completed upon pressing `Tab` . For instance Ubuntu users should have quite a few folders in `/usr/share` that begin with "app" so in this case, typing `cd /u/s/app` will only expand the `/usr/share/` part.
Here is another example of deeper incomplete file path expansion. On an Ubuntu system type `cd /u/s/f/t/u` , press `Tab` , and it should be automatically expanded to cd `/usr/share/fonts/truetype/ubuntu` .
Features include:
* Escapes special characters
* If the user starts the path with quotes, character escaping is not applied and instead, the quote is closed with a matching character after expending the path
* Properly expands ~ expressions
* If bash-completion package is already in use, this code will safely override its _filedir function. No extra configuration is required, just make sure you source this project after the main bash-completion.
Check out the [project page][2] for more information and a demo screencast.
### Install bash-complete-partial-path
The bash-complete-partial-path installation instructions specify downloading the bash_completion script directly. I prefer to grab the Git repository instead, so I can update it with a simple `git pull` , therefore the instructions below will use this method of installing bash-complete-partial-path. You can use the [official][3] instructions if you prefer them.
1. Install Git (needed to clone the bash-complete-partial-path Git repository).
In Debian, Ubuntu, Linux Mint and so on, use this command to install Git:
```
sudo apt install git
```
2. Clone the bash-complete-partial-path Git repository in `~/.config/`:
```
cd ~/.config && git clone https://github.com/sio/bash-complete-partial-path
```
3. Source `~/.config/bash-complete-partial-path/bash_completion` in your `~/.bashrc` file,
Open ~/.bashrc with a text editor. You can use Gedit for example:
```
gedit ~/.bashrc
```
At the end of the `~/.bashrc` file add the following (as a single line):
```
[ -s "$HOME/.config/bash-complete-partial-path/bash_completion" ] && source "$HOME/.config/bash-complete-partial-path/bash_completion"
```
I mentioned adding it at the end of the file because this needs to be included below (after) the main bash-completion from your `~/.bashrc` file. So make sure you don't add it above the original bash-completion as it will cause issues.
4\. Source `~/.bashrc`:
```
source ~/.bashrc
```
And you're done, bash-complete-partial-path should now be installed and ready to be used.
--------------------------------------------------------------------------------
via: https://www.linuxuprising.com/2018/07/incomplete-path-expansion-completion.html
作者:[Logix][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://plus.google.com/118280394805678839070
[1]:https://github.com/sio/bash-complete-partial-path
[2]:https://github.com/sio/bash-complete-partial-path
[3]:https://github.com/sio/bash-complete-partial-path#installation-and-updating

View File

@ -0,0 +1,82 @@
针对 Bash 的不完整路径展开(补全)
======
![](https://4.bp.blogspot.com/-k2pRIKTzcBU/W1BpFtzzWuI/AAAAAAAABOE/pqX4XcOX8T4NWkKOmzD0T0OioqxzCmhLgCLcBGAs/s1600/Gnu-bash-logo.png)
[bash-complete-partial-path][1] 通过添加不完整的路径展开(类似于 Zsh来增强 Bash它在 Linux 上macOS 使用 gnu-sedWindows 使用 MSYS中的路径补全。如果你想在 Bash 中使用这个省时特性,而不必切换到 Zsh它将非常有用。
这是它如何工作的。当按下 `Tab` 键时bash-complete-partial-path 假定每个部分都不完整并尝试展开它。假设你要进入 `/usr/share/applications` 。你可以输入 `cd /u/s/app`,按下 `Tab`bash-complete-partial-path 应该把它展开成 `cd /usr/share/applications` 。如果存在冲突,那么按 `Tab` 仅补全没有冲突的路径。例如Ubuntu 用户在 `/usr/share` 中应该有很多以 “app” 开头的文件夹,在这种情况下,输入 `cd /u/s/app` 只会展开 `/usr/share/` 部分。
这是更深层不完整文件路径展开的另一个例子。在Ubuntu系统上输入 `cd /u/s/f/t/u`,按下 `Tab`,它应该自动展开为 `cd /usr/share/fonts/truetype/ubuntu`
功能包括:
* 转义特殊字符
* 如果用户路径开头使用引号,则不转义字符转义,而是在展开路径后使用匹配字符结束引号
* 正确展开 ~ 表达式
* 如果 bash-completion 包正在使用,则此代码将安全地覆盖其 _filedir 函数。无需额外配置,只需确保在主 bash-completion 后 source 此项目。
查看[项目页面][2]以获取更多信息和演示截图。
### 安装 bash-complete-partial-path
bash-complete-partial-path 安装说明指定直接下载 bash_completion 脚本。我更喜欢从 Git 仓库获取,这样我可以用一个简单的 `git pull` 来更新它,因此下面的说明将使用这种安装 bash-complete-partial-path。如果你喜欢可以使用[官方][3]说明。
1. 安装 Git需要克隆 bash-complete-partial-path 的 Git 仓库)。
在 Debian、Ubuntu、Linux Mint 等中,使用此命令安装 Git
```
sudo apt install git
```
2. 在 `~/.config/` 中克隆 bash-complete-partial-path 的 Git 仓库:
```
cd ~/.config && git clone https://github.com/sio/bash-complete-partial-path
```
3. 在 `~/.bashrc` 文件中 source `~/.config/bash-complete-partial-path/bash_completion`
用文本编辑器打开 ~/.bashrc。例如你可以使用 Gedit
```
gedit ~/.bashrc
```
`~/.bashrc` 的末尾添加以下内容(在一行中):
```
[ -s "$HOME/.config/bash-complete-partial-path/bash_completion" ] && source "$HOME/.config/bash-complete-partial-path/bash_completion"
```
我提到在文件的末尾添加它,因为这需要包含在你的 `~/.bashrc` 文件的主 bash-completion 下面(之后)。因此,请确保不要将其添加到原始 bash-completion 之上,因为它会导致问题。
4\. Source `~/.bashrc`:
```
source ~/.bashrc
```
这样就好了,现在应该安装完 bash-complete-partial-path 并可以使用了。
--------------------------------------------------------------------------------
via: https://www.linuxuprising.com/2018/07/incomplete-path-expansion-completion.html
作者:[Logix][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://plus.google.com/118280394805678839070
[1]:https://github.com/sio/bash-complete-partial-path
[2]:https://github.com/sio/bash-complete-partial-path
[3]:https://github.com/sio/bash-complete-partial-path#installation-and-updating