命令行中的 vi 模式

<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

相关文章
相关标签/搜索