命令行中的 vi 模式

命令行中修改已经输入的命令比较麻烦,如果你不知道一些快捷键的话,只能使用方向键一个一个字符地移动到目标位置进行修改,对于比较复杂且过长的命令来说,效率不高。

以下信息来自 bash 的 man 页面:

$ man bash  # ...       Commands for 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.    # ...

可看到 bash 本身提供了一些有用的快捷键可在命令中快速导航,

  • control + a 定位到行首(start)
  • control + e 定位到行末(end)
  • control + f 向前移动一个单词(forward)
  • control + b 向后移动一个单词(back)

移动光标,大部分情况下,我觉得记住这四个可以满足日常需求。

除了移动光标,还有一些编辑的快捷键,在 man 页面中 Killing and Yanking 部分,

# ...       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 if 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-)                Delete all spaces and tabs around point.         kill-region                Kill the text in the current region.         copy-region-as-kill                Copy the text in the region to the kill buffer.         copy-backward-word                Copy  the word before point to the kill buffer.  The word boundaries are the                same as backward-word.         copy-forward-word                Copy the word following point to the kill buffer.  The word  boundaries  are                the same as forward-word.         yank (C-y)                Yank the top of the kill ring into the buffer at point.         yank-pop (M-y)                Rotate  the  kill  ring, and yank the new top.  Only works following yank or                yank-pop.    # ...

其中这一个我最常用:

  • control + u 删除当前光标位置到行首的内容,配合着 control + e 把光标定位到行末再使用该命令,可实现清空整行的效果

除了这些快捷键,其实命令行还有个 vi 模式,该模式下的表现和在 vi 编辑器里一样,对于 vi 用户来说,进入这种模式后,编辑和修改命令就显得十分得心应手了。

开启 vi 模式

不同 shell 中开启的命令不一样,我比较偏好 fish,因为它的自动补全真的好用到无以复加。

运行 fish_vi_key_bindings 即可进入 vi 模式。

$ fish_vi_key_bindings

运行 fish_default_key_bindings 回到默认。

$ fish_default_key_bindings

命令行中的 vi 模式

命令行的 vi 模式演示

如果需要一直开始,可以配置文件中添加上述命令。

$ vi ~/.config/fish/config.fish    # 其他代码  fish_vi_key_bindings  

选择合适的主题

你可能需要一个可以在命令提示行中显示当前 vi 状态的主题。

推荐 fish 搭配 omf 使用 omf 中的主题。

fish 有默认的 vi 状态展示,和主题很不搭配。

命令行中的 vi 模式

fish 默认的 vi 状态展示

需要手动去掉,配置文件中添加如下脚本:

function fish_mode_prompt; end

修正 fish 中的自动补全

如果发现 vi 模式下, fish 的自动补全快捷键 control + f 不能用了,可在配置文件中添加如下脚本来修复这个快捷键:

function fish_user_key_bindings      for mode in insert default visual          bind -M $mode cf forward-char      end  end

© 版权声明
THE END
点赞0
抢沙发
头像
提交
头像

昵称

取消
昵称
一言一语