<table class="d-block"> <tbody class="d-block"> <tr class="d-block"> <td class="d-block comment-body markdown-body js-comment-body">html
<p>命令行中修改已經輸入的命令比較麻煩,若是你不知道一些快捷鍵的話,只能使用方向鍵一個一個字符地移動到目標位置進行修改,對於比較複雜且過長的命令來講,效率不高。</p> <p>如下信息來自 bash 的 man 頁面:</p> <div class="highlight highlight-source-shell"><pre>$ man bash <span class="pl-c"><span class="pl-c">#</span> ...</span>git
Commands <span class="pl-k">for</span> Moving beginning-of-line (C-a) Move to the start of the current line. end-of-line (C-e) Move to the end of the line. forward-char (C-f) Move forward a character. backward-char (C-b) Move back a character. forward-word (M-f) Move forward to the end of the next word. Words are composed of alphanu- meric characters (letters and digits). backward-word (M-b) Move back to the start of the current or previous word. Words are composed of alphanumeric characters (letters and digits). clear-screen (C-l) Clear the screen leaving the current line at the top of the screen. With an argument, refresh the current line without clearing the screen. redraw-current-line Refresh the current line.github
<span class="pl-c"><span class="pl-c">#</span> ...</span></pre></div>shell
<p>可看到 bash 自己提供了一些有用的快捷鍵可在命令中快速導航,</p> <ul> <li><kbd>control</kbd> + <kbd>a</kbd> 定位到行首(st<strong>a</strong>rt)</li> <li><kbd>control</kbd> + <kbd>e</kbd> 定位到行末(<strong>e</strong>nd)</li> <li><kbd>control</kbd> + <kbd>f</kbd> 向前移動一個單詞(<strong>f</strong>orward)</li> <li><kbd>control</kbd> + <kbd>b</kbd> 向後移動一個單詞(<strong>b</strong>ack)</li> </ul> <p>移動光標,大部分狀況下,我以爲記住這四個能夠知足平常需求。</p> <p>除了移動光標,還有一些編輯的快捷鍵,在 man 頁面中 Killing and Yanking 部分,</p> <div class="highlight highlight-source-shell"><pre><span class="pl-c"><span class="pl-c">#</span> ...</span>bash
Killing and Yanking kill-line (C-k) Kill the text from point to the end of the line. backward-kill-line (C-x Rubout) Kill backward to the beginning of the line. unix-line-discard (C-u) Kill backward from point to the beginning of the line. The killed text is saved on the kill-ring. kill-whole-line Kill all characters on the current line, no matter where point is. kill-word (M-d) Kill from point to the end of the current word, or <span class="pl-k">if</span> between words, to the end of the next word. Word boundaries are the same as those used by for- ward-word. backward-kill-word (M-Rubout) Kill the word behind point. Word boundaries are the same as those used by backward-word. unix-word-rubout (C-w) Kill the word behind point, using white space as a word boundary. The killed text is saved on the kill-ring. unix-filename-rubout Kill the word behind point, using white space and the slash character as the word boundaries. The killed text is saved on the kill-ring. delete-horizontal-space (M-<span class="pl-cce">)</span> Delete all spaces and tabs around point. kill-region Kill the text <span class="pl-k">in</span> the current region. copy-region-as-kill Copy the text <span class="pl-k">in</span> the region to the <span class="pl-c1">kill</span> buffer. copy-backward-word Copy the word before point to the <span class="pl-c1">kill</span> buffer. The word boundaries are the same as backward-word. copy-forward-word Copy the word following point to the <span class="pl-c1">kill</span> buffer. The word boundaries are the same as forward-word. yank (C-y) Yank the top of the <span class="pl-c1">kill</span> ring into the buffer at point. yank-pop (M-y) Rotate the <span class="pl-c1">kill</span> ring, and yank the new top. Only works following yank or yank-pop.markdown
<span class="pl-c"><span class="pl-c">#</span> ...</span></pre></div>編輯器
<p>其中這一個我最經常使用:</p> <ul> <li><kbd>control</kbd> + <kbd>u</kbd> 刪除當前光標位置到行首的內容,配合着 <kbd>control</kbd> + <kbd>e</kbd> 把光標定位到行末再使用該命令,可實現清空整行的效果</li> </ul> <p>除了這些快捷鍵,其實命令行還有個 vi 模式,該模式下的表現和在 vi 編輯器裏同樣,對於 vi 用戶來講,進入這種模式後,編輯和修改命令就顯得十分駕輕就熟了。</p> <h2>開啓 vi 模式</h2> <p>不一樣 shell 中開啓的命令不同,我比較偏好 <a href="https://fishshell.com" rel="nofollow">fish</a>,由於它的自動補全真的好用到無以復加。</p> <p>運行 <code>fish_vi_key_bindings</code> 便可進入 vi 模式。</p> <div class="highlight highlight-source-shell"><pre>$ fish_vi_key_bindings</pre></div> <p>運行 <code>fish_default_key_bindings</code> 回到默認。</p> <div class="highlight highlight-source-shell"><pre>$ fish_default_key_bindings</pre></div> <p><a target="_blank" rel="noopener noreferrer" href="https://user-images.githubusercontent.com/3783096/56299503-bf90b100-6166-11e9-8c1f-d103a7be9af2.gif"><img src="https://user-images.githubusercontent.com/3783096/56299503-bf90b100-6166-11e9-8c1f-d103a7be9af2.gif" alt="命令行的 vi 模式演示" style="max-width:100%;"></a></p> <p align="center">命令行的 vi 模式演示</p> <p>若是須要一直開始,能夠配置文件中添加上述命令。</p> <div class="highlight highlight-source-shell"><pre>$ vi <span class="pl-k">~</span>/.config/fish/config.fishoop
<span class="pl-c"><span class="pl-c">#</span> 其餘代碼</span> fish_vi_key_bindings </pre></div>spa
<h2>選擇合適的主題</h2> <p>你可能須要一個能夠在命令提示行中顯示當前 vi 狀態的主題。</p> <p>推薦 fish 搭配 omf 使用 omf 中的<a href="https://github.com/oh-my-fish/oh-my-fish/blob/master/docs/Themes.md">主題</a>。</p> <p>fish 有默認的 vi 狀態展現,和主題很不搭配。</p> <p><a target="_blank" rel="noopener noreferrer" href="https://user-images.githubusercontent.com/3783096/56299552-dafbbc00-6166-11e9-8120-6d67820d85d8.png"><img src="https://user-images.githubusercontent.com/3783096/56299552-dafbbc00-6166-11e9-8120-6d67820d85d8.png" alt="fish 默認的 vi 狀態展現" style="max-width:100%;"></a></p> <p align="center">fish 默認的 vi 狀態展現</p> <p>須要手動去掉,配置文件中添加以下腳本:</p> <div class="highlight highlight-source-shell"><pre><span class="pl-k">function</span> <span class="pl-en">fish_mode_prompt;</span> end</pre></div> <h2>修正 fish 中的自動補全</h2> <p>若是發現 vi 模式下, fish 的自動補全快捷鍵 <kbd>control</kbd> + <kbd>f</kbd> 不能用了,可在配置文件中添加以下腳原本修復這個快捷鍵:</p> <div class="highlight highlight-source-shell"><pre><span class="pl-k">function</span> <span class="pl-en">fish_user_key_bindings</span> <span class="pl-k">for</span> <span class="pl-smi">mode</span> <span class="pl-k">in</span> insert default visual <span class="pl-c1">bind</span> -M <span class="pl-smi">$mode</span> <span class="pl-cce">\c</span>f forward-char end end</pre></div> </td> </tr> </tbody> </table>命令行
原文出處:https://www.cnblogs.com/Wayou/p/cli_vi_mode.html