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

以下信息来自 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

命令行中的 vi 模式的更多相关文章

  1. Linux Vi进入编辑模式后使用方向键的时候,并不会使光标移动,而是在命令行中出现A、B、C、D四个字母

    在linux下,初始使用Vi的时候有两个典型的问题: 1.在编辑模式下使用方向键的时候,并不会使光标移动,而是在命令行中出现A.B.C.D四个字母: 2.当编辑出现错误,想要删除时,发现Backspa ...

  2. php在cli模式下取得命令行中的参数的方法-getopt命令行可传递数组-简单自定义方法取命令行参数

    在cli模式下执行PHP时,自动给脚本文件传递了一个变量$argv,其值即是一个命令中所有值组成的数组(以空格区分),在PHP程序中接收参数有3种方法1.直接使用argv变量数组. 2.使用$_SER ...

  3. 在 Linux 命令行中使用和执行 PHP 代码

    PHP是一个开源服务器端脚本语言,最初这三个字母代表的是“Personal Home Page”,而现在则代表的是“PHP:Hypertext Preprocessor”,它是个递归首字母缩写.它是一 ...

  4. 在 Linux/windows下 命令行中使用和执行 PHP 代码[交互式php]

    [注释]在ubuntu下,升级php到7.1版本,虽然提示的是Interactive mode enabled, 但实际上可以直接书写命令,和interactive shell效果一样. 一:wind ...

  5. 在Linux命令行中使用计算器的5个命令

    大家好,我是良许. 在使用 Linux 时,我们有时会需要做一些计算,那么我们就可能需要用到计算器.在 Linux 命令行里,有许多计算器工具,这些命令行计算器可以让我们执行科学计算.财务计算或者一些 ...

  6. 【转】WriteMessage的信息在AutoCAD中命令行中实时显示

    之前程序中有段发送信息到命令行上显示的代码,如下:     ed.WriteMessage("开始标注横断面高程,请稍候!");     但是发现命令行中并不马上显示,代码也明明运 ...

  7. 命令行中mysql乱码问题

    1.现象  在命令行中,执行sql语句如果包含中问题,提示“ Data too long for column '列名' at row 1”   或者在命令行中查询出的结果中,中文乱码   2.分析 ...

  8. Mac 在命令行中获得Root权限

    Mac 在命令行中获得Root权限 作者 firedragonpzy 13 九月, 2012 2条评论 本文为firedragonpzy原创,转载务必在明显处注明:转载自[Softeware MyZo ...

  9. 在命令行中运行eclipse中创建的java项目

    在命令行中运行eclipse中创建的java项目 博客分类: java相关 javaeclipse命令行  由于项目要求,需要对eclipse中的项目进行打包,似的可以在客户机上不装eclipse的情 ...

随机推荐

  1. 寻找DevExpress破解经历之旅

    众所周知DevExpress是收费的,但是破解版的也不少,近期公司需要做发票套打的功能让我找个打印工具,我寻思着DevExpress这个软件好像挺不错的,功能强大,看了下价格方面,好吧!2W多呢,市面 ...

  2. nltk download失败

    之前在台式机win10的系统,python 2.7,用的pycharm执行nltk download(),很顺利.然而到了我的笔记本只是换个一个win8的系统,Python的配置都是一样的,但是这时候 ...

  3. 物流的纯css实现方法

    首先我们来看看UI给出的设计图. 为什么到达是最前面,为什么物流顺序是倒叙的,这是什么用户习惯,这是我拿到设计稿的问题,但是这里不谈设计,因为审美这个东西无法评估.那么这里我就做一个顺序的来对比一下吧 ...

  4. 关于dropout的有趣的进化论解释

    训练神经网络时,使用dropout技术来防止网络的过拟合.我们这里且不谈这个技术的细节,但就这项技术的有趣的生物进化论解释了解下.自然界的高等生物进化出了两性繁殖,其原因可以解释为使得变异的基因能散播 ...

  5. [CVPR2017] Weakly Supervised Cascaded Convolutional Networks论文笔记

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #042eee } p. ...

  6. hexo+github创建属于自己的博客

    配置环境 安装Node(必须) 作用:用来生成静态页面的 到Node.js官网下载相应平台的最新版本,一路安装即可. 安装Git(必须) 作用:把本地的hexo内容提交到github上去. 安装Xco ...

  7. 由于github仓库中提前建立readme文件,导致git push报错error: failed to push some refs to 'git@github.com:

    $ git push -u origin master To git@github.com:xxx/xxx.git ! [rejected] master -> master (fetch fi ...

  8. div布局之面向对象

    栗子之导航条(navbar) http://www.runoob.com/try/try2.php?filename=bootstrap-using-glyphicons-navbar <!DO ...

  9. Intent的跳转和传值

    看了Intent的使用方法,感觉还是要做一些笔记的,首先对于Intent的跳转Activity这个功能的实现就有两种方法,也可以说有三种: 1.                  2.

  10. zookeeper 配置文件conf目录下 zoo文件 配置详解

    clientPort:客户端尝试连接server的端口号 (对外服务端口) 通常为2181 dataDir:存放快照文件snapshot的目录.(储存数据的目录) dataLogDir:事务日志存放位 ...