红联Linux门户
Linux协助

指令行中的 vi 形式

发布时刻:2019-05-23 00:00:00来历:不知道作者:admin

指令行中批改现已输入的指令比较费事,假如你不知道一些快捷键的话,只能运用方向键一个一个字符地移动到方针方位进行批改,关于比较复杂且过长的指令来说,功率不高。

以下信息来自 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 ~/.config/fish/config.fish

# 其他代码
fish_vi_key_bindings

挑选适宜的主题

你或许需求一个能够在指令提示行中显现当时 vi 状况的主题。

引荐 fish 调配 omf 运用 omf 中的主题。

fish 有默许的 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