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

以下信息来自 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. node八-核心模块、包

    学会查API,远比会几个API更重要. 核心模块意义 -如果只是在服务器运行javascript代码,并没有多大意义,因为无法实现任何功能>读写文件.访问网络 -Node的用处在于它本身还提供可 ...

  2. redis的持久化之RDB的配置和原理

    Redis优秀的性能是由于其将所有的数据都存储在内存中,同样memcached也是这样做的,内存中的数据会在服务器重启后就没有了,也就是不能保证持久化.但是为什么Redis能够脱颖而出呢,很大程度上是 ...

  3. 在Visual Studio中使用Debug Visualizers在C++中实现对原始类的自定义调试信息显示

    在Visual Studio中使用Debug Visualizers在C++中实现对原始类的自定义调试信息显示 当我们在VS的C++中使用vector.list.map等这些STL容器,在开启调试的时 ...

  4. Proxy是在什么时候调用InvocationHandler的invoke方法的

    最近看到spring的动态代理,扒到深处看到时 Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);看到这一句,顿时比较懵逼,还是 ...

  5. PAT1130:Infix Expression

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  6. Oracle 服务启动关闭

    有没有感觉到,自从装上Oracle之后,电脑变卡了?卡是正常的,oracle作为大型数据库软件,运行起来是非常吃内存的. 所以对于自用的电脑来说,不使用Oracle的时候,应该把Oracle的一系列服 ...

  7. java中使用hashSet的特性,判断数组是否有重复值

    public static boolean cheakRepeat(int[] array){ HashSet<Integer> hashSet = new HashSet<Inte ...

  8. Scrapy 1.4 文档 02 安装指南

    安装 Scrapy Scrapy 运行在 Python 2.7 和 Python 3.3 或更高版本上. 如果您使用的是 Anaconda 或 Miniconda,则可以从 conda-forge 通 ...

  9. Markdown 编辑器语法 专题

    基本技巧 代码 如果你只想高亮语句中的某个函数名或关键字,可以使用 `function_name()` 实现 通常编辑器根据代码片段适配合适的高亮方法,但你也可以用 ```(tab键上的符号,要从每行 ...

  10. 带BOM头文件解析

    在java中apache提供了一个工具类BOMStream,在获取文件流时,将获取到的文件流转化成为BOM流: InputStreamReader is = new InputStreamReader ...