Readline - Arch Linux 中文维基

跳至內容

# Readline

出自 Arch Linux 中文维基

本文或本節需要翻譯。要貢獻翻譯，請訪問簡體中文翻譯團隊。

附註： 翻譯未完成（在 Talk:Readline# 中討論）

Readline 是來自 GNU Project 的庫，Bash 和其他命令行界面的程序使用它在命令行編輯和交互。詳情請參閱 readline(3)。

## 安裝

readline包 包很可能已作為 Bash 的依賴安裝。

## 編輯模式

Readline 默認使用 Emacs 風格的快捷鍵與命令行交互。不過，vi 風格的編輯界面也受支持，但需要將以下內容添加到 `~/.inputrc`：

    ~/.inputrc

    set editing-mode vi

或者，要只為 Bash 設置，你可將以下內容添加到 `~/.bashrc`：

    ~/.bashrc

    set -o vi

### 提示中的模式指示符

Vi 風格的編輯有兩種模式：命令模式和插入模式。你可通過添加以下命令顯示當前的模式：

    ~/.inputrc

    set show-mode-in-prompt on

這將在你的提示中顯示模式（默認是 `(cmd)`/`(ins)`），顯示的內容可通過 `vi-ins-mode-string` 和 `vi-cmd-mode-string` 變量自定義。

### 為每個模式指定不同的光標樣式

你可以使用"\1 .. \2" 轉義符為每個模式指定不同的光標樣式：

    ~/.inputrc

    set vi-ins-mode-string \1\e[6 q\2
    set vi-cmd-mode-string \1\e[2 q\2

這將在命令模式中顯示方塊形狀的光標，在插入模式中顯示豎線形狀的光標。注意，你必須先啟用模式指示符（見#提示中的模式指示符）。

Virtual Console 使用不同的轉義代碼，因此你應先檢查使用的是哪種終端：

    ~/.inputrc

    $if term=linux
    	set vi-ins-mode-string \1\e[?0c\2
    	set vi-cmd-mode-string \1\e[?8c\2
    $else
    	set vi-ins-mode-string \1\e[6 q\2
    	set vi-cmd-mode-string \1\e[2 q\2
    $endif

詳情請參閱 software cursor for VGA。

## Fast word movement

Xterm 默認支持使用 `Ctrl+Left` 和 `Ctrl+Right` 在單詞間移動。要在其他終端模擬器中實現這一效果，先找到正確的終端代碼，然後在 `~/.inputrc` 中綁定到 `backward-word` 和 `forward-word`。

例如，對於 urxvt：

    ~/.inputrc

    "\e[1;5D": backward-word
    "\e[1;5C": forward-word

## 歷史

通常，無論你輸入了什麼，按向上箭頭都會出現上一次輸入的命令。然而，用戶可能會覺得只顯示匹配剛剛輸入的內容的命令更實用。

例如，假設輸入了以下命令：

- `ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig`
- `who`
- `mount`
- `man mount`

此時，當輸入 `ls` 並按向上箭頭，輸入將會被替換為 `man mount`。如果你使用了歷史搜索功能，那麼只有過去以 `ls`（當前的輸入）開頭的命令會被顯示，在這裡就是 `ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig`。

要啟用歷史搜索功能，只需把以下內容加入到 `/etc/inputrc` 或者 `~/.inputrc`：

    "\e[A": history-search-backward
    "\e[B": history-search-forward

如果你使用 vi 模式，添加以下內容到 `~/.inputrc`（來自這個帖子）：

    set editing-mode vi
    $if mode=vi
    set keymap vi-command
    # these are for vi-command mode
    "\e[A": history-search-backward
    "\e[B": history-search-forward
    j: history-search-forward
    k: history-search-backward
    set keymap vi-insert
    # these are for vi-insert mode
    "\e[A": history-search-backward
    "\e[B": history-search-forward
    $endif

如果你選擇將這些內容添加到 `~/.inputrc`，那建議你同時在文件的開始添加以下內容，以避免像這樣的怪事：

    $include /etc/inputrc

Alternatively, one can use reverse-search-history (incremental search) by pressing `Ctrl+R`, which does not search based on previous input but instead jumps backwards in the history buffer as commands are typed in a search term. Pressing `Ctrl+R` again during this mode will display the previous line in the buffer that matches the current search term, while pressing `Ctrl+G` (abort) will cancel the search and restore the current input line. So in order to search through all previous `mount` commands, press `Ctrl+R`, type 'mount' and keep pressing `Ctrl+R` until the desired line is found.

The forward equivalent to this mode is called forward-search-history and is bound to `Ctrl+S` by default. Beware that most terminals override `Ctrl+S` to suspend execution until `Ctrl+Q` is entered. (This is called XON/XOFF flow control). For activating forward-search-history, either disable flow control by issuing:

    $ stty -ixon

或者在 `inputrc` 中使用不同的鍵。例如，要使用默認未綁定的 `Alt+S`：

    "\es": forward-search-history

## Faster completion

When performing tab completion, a single tab attempts to partially complete the current word. If no partial completions are possible, a double tab shows all possible completions.

The double tab can be changed to a single tab by setting:

    ~/.inputrc

    set show-all-if-unmodified on

Or you can set it such that a single tab will perform both steps: partially complete the word and show all possible completions if it is still ambiguous:

    ~/.inputrc

    set show-all-if-ambiguous on

## Colorized completion

You can enable coloring of completion of filenames with the `colored-stats` option. You can also color the identical prefix of completion-lists with `colored-completion-prefix`. For example:

    ~/.inputrc

    # Color files by types
    # Note that this may cause completion text blink in some terminals (e.g. xterm).
    set colored-stats On
    # Append char to indicate type
    set visible-stats On
    # Mark symlinked directories
    set mark-symlinked-directories On
    # Color the common prefix
    set colored-completion-prefix On
    # Color the common prefix in menu-complete
    set menu-complete-display-prefix On

## 宏

Readline also supports binding keys to keyboard macros. For simple example, run this command in Bash:

    bind '"\ew": "\C-e # macro"'

or add the part within single quotes to inputrc:

    "\ew": "\C-e # macro"

Now type a line and press `Alt`+`W`. Readline will act as though `Ctrl+E` (end-of-line) had been pressed, appended with '`# macro`'.

Use any of the existing keybindings within a readline macro, which can be quite useful to automate frequently used idioms. For example, this one makes `Ctrl+Alt+L` append "| less" to the line and run it (`Ctrl+M` is equivalent to `Enter`):

    "\e\C-l": "\C-e | less\C-m"

The next one prefixes the line with 'yes |' when pressing `Ctrl+Alt+Y`, confirming any yes/no question the command might ask:

    "\e\C-y": "\C-ayes | \C-m"

This example wraps the line in `su -c ''`, if `Alt+S` is pressed:

    "\es": "\C-a su -c '\C-e'\C-m"

This example prefixes the line with `sudo` , if `Alt+S` is pressed. It is safer because it will not input the `Enter` key.

    "\es": "\C-asudo \C-e"

As a last example, quickly send a command in the background with `Ctrl+Alt+B`, discarding all of its output:

    "\e\C-b": "\C-e > /dev/null 2>&1 &\C-m"

## 禁用 control 回顯

Readline 使得終端在按下 `Ctrl+C` 後回顯 `^C`。要禁用，只需將以下內容添加到 `~/.inputrc`：

    set echo-control-characters off

## 參閱

- vi readline 編輯速查表
- emacs readline 編輯速查表

分類：​

- 命令行
- GNU

隱藏分類：​

- 使用「模板:Translateme」標記的頁面或章節

Readline
新增話題