Bash 和 Zsh 開啓 vi-mode

Bash 和 Zsh 開啓 vi-mode

bash 有兩種操做模式,分別是 emacs 和 vi 。數組

在 bash 中

set -o vi # 臨時開啓 vi 模式
vi ~/.bashrc # 在其中寫入 `set -o vi`
source ~/.bashrc # 刷新配置

在 zsh 中

vi ~/.zshr # 在 plugins 數組中添加 vi-mode
source ~/.bashrc # 刷新配置

vi 編輯模式快捷鍵

http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.txt
http://mywiki.wooledge.org/set-o-vi-emacsbash

進入命令行模式:
    ESC          | 進入命令行模式

  進入輸入模式:
    i            | 在光標前插入
    a            | 光標後插入
    I            | 在行的開頭插入
    A            | 在行的結尾插入
    c<mov. comm> | Change text of a movement command <mov. comm> (見下文).
    C            | 剪切到行尾 (同 c$)
    cc 或 S      | 剪切整行 (同 0c$)
    s            | 刪除光標處文本,並進入插入模式。 (同 c[SPACE])
    r            | 修改光標處文本 (沒有離開命令樣式)
    R            | 進入替換模式
    v            | 先執行命令,再使用編輯編輯命令。使用 $VISUAL 或 $EDITOR 變量定義編輯器, 編輯器默認 vi 。

  移動 (命令模式下):
    h            | 按字左移
    l            | 按字右移
    w            | 按詞右移
    b            | 按詞左移
    W            | 以空白字符按詞右移(好比 ab a-b , W 不會移動到 - 上,而 w 會)
    B            | 以空白字符按詞左移
    e            | 移動光標到詞尾部
    E            | 以空白符移動光標到詞尾
    0            | 移動光標到行首
    ^            | 移動光標到行首不是空白符
    $            | 移動光標到行尾
    %            | 移動到左括號或右括號

  字符查找 (也是移動命令):
    fc           | 右移到字符 c .
    Fc           | 左移到字符 c .
    tc           | 右移到字符 c 的左邊
    Tc           | 左移到字符 c 的右邊
    ;            | 重作查找
    ,            | 反方向重作查找
    |            | 移到第 n 列 (如 3| 移到第 3 列)

  刪除命令:
    x            | 刪除當前光標所在字符.
    X            | 刪除光標前的一個字符.
    d<mov. comm> | Delete text of a movement command <mov. comm> (see above).
    D            | 刪除到行尾 (同 d$).
    dd           | 刪除行 (同 0d$).
    CTRL-w       | 向左刪除單詞 (編輯模式下)
    CTRL-u       | 刪除到進入編輯模式時光標位置

  撤銷、重作、複製、粘貼:
    u            | 單步撤銷
    U            | 撤銷全部
    .            | 重作
    y<mov. comm> | Yank a movement into buffer (copy).
    yy           | Yank the whole line.
    p            | 在光標處粘貼
    P            | 在光標前粘貼

  歷史記錄:
    k            | 上一條命令
    j            | 下一條命令
    G            | 回來當前命令
    /string 或 CTRL-r  | 搜索歷史命令(/string 用於命令模式下, ctrl-r 用於輸入模式下)
    ?string 或 CTRL-s  | 搜索歷史命令(Note that on most machines Ctrl-s STOPS the terminal | output, change it with `stty' (Ctrl-q to resume)).
    n            | 下一條歷史匹配
    N            | 上一條歷史匹配

  自動完成:
    TAB 或 = 或  | 列出全部可能(TAB 用於輸入模式)
    CTRL-i       |
    *            | Insert all possible completions.

  其餘:
    ~            | 切換當前光標處文本的大小寫,並右移光標
    #            | 註釋當前命令並把其放入歷史
    _            | Inserts the n-th word of the previous command in the current line.
    0, 1, 2, ... | Sets the numeric argument.
    CTRL-v       | Insert a character literally (quoted insert).
    CTRL-r       | Transpose (exchange) two characters.

快捷鍵2

http://mywiki.wooledge.org/set-o-vi-emacs?action=raw
```markdown

== Emacs and Vi modes in Bash ==app

Two options that can be set using the set command that will be of some interest to the common user are 「-o vi」 and 「-o emacs」. As with all of the environment modifying commands these can be typed at the command prompt or inserted into the appropriate file mentioned above.編輯器

=== Emacs Mode ===post

  • $ set -o emacsui

  • This is usually the default editing mode when in the bash environment and means that you are able to use commands like those in Emacs (defined in the Readline library) to move the cursor, cut and paste text, or undo editing.this

  • Commands to take advantage of bash’s Emacs Mode:
    . o ctrl-a Move cursor to beginning of line
    . o ctrl-e Move cursor to end of line
    . o meta-b Move cursor back one word
    . o meta-f Move cursor forward one word
    . o ctrl-w Cut the last word
    . o ctrl-u Cut everything before the cursor
    . o ctrl-k Cut everything after the cursor
    . o ctrl-y Paste the last thing to be cut
    . o ctrl-_ Undo
    . o NOTE: ctrl- = hold control, meta- = hold meta (where meta is usually the alt or escape key).
    . o A combination of ctrl-u to cut the line combined with ctrl-y can be very helpful. If you are in middle of typing a command and need to return to the prompt to retrieve more information you can use ctrl-u to save what you have typed in and after you retrieve the needed information ctrl-y will recover what was cut.url

    === Vi Mode ===spa

  • $ set -o vi

  • Vi mode allows for the use of vi like commands when at the bash prompt. When set to this mode initially you will be in insert mode (be able to type at the prompt unlike when you enter vi). Hitting the escape key takes you into command mode.

  • Commands to take advantage of bash’s Vi Mode:
    . o h Move cursor left
    . o l Move cursor right
    . o A Move cursor to end of line and put in insert mode
    . o 0 (zero) Move cursor to beginning of line (doesn’t put in insert mode)
    . o i Put into insert mode at current position
    . o a Put into insert mode after current position
    . o dd Delete line (saved for pasting)
    . o D Delete text after current cursor position (saved for pasting)
    . o p Paste text that was deleted
    . o j Move up through history commands
    . o k Move down through history commands
    . o u Undo


CategoryShell

相關文章
相關標籤/搜索