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

以下信息来自 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. MQTT入手笔记

    MQTT服务官网:http://mosquitto.org/download/ 在unix系统按照以下步骤运行并启动mqtt服务: 1. # 下载源代码包wget http://mosquitto.o ...

  2. 关于Python的super用法研究

    一.问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在python 2.2以前,通常的写法如代码段1: 代码段1: class A:  def __init__(se ...

  3. 4sumii

    problem description: there is four number list named A,B,C,D; now you should out put the num of  tup ...

  4. 【淘宝客】根据淘客联盟精选清单(淘宝天猫内部优惠券)随机显示淘宝天猫优惠券dome

    也许大家在生活中经常淘宝看到[淘宝天猫内部优惠券]的网站,或者在微博中经常有博主发券,让大家生活中购物便宜许多,作为一个站长,我们也希望自己的网站也能有这样的一个功能,现在就分享给大家,还是免后台哦. ...

  5. textarea 里设置 style="resize:none"

    禁止textarea拉伸的方法是::                                    设置这个 style="resize:none" 属性 例子: < ...

  6. SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 简单概述一下本讲 别名,sql片段简单写一下,模糊查询多写一点 一.别名 <typeAliases> ...

  7. 20.如何从app业务逻辑提炼api接口

    在app后端的工作中,设计api是一个很考验设计能力的工作.在项目的初始阶段,只知道具体的业务逻辑,那怎么把业务逻辑抽象和提炼,设计出api呢?通过阅读本文,可解答以上疑惑. 在本文中,是用以前做过的 ...

  8. python爬取所有微信好友的信息

    ''' 爬取所有T信好友的信息 ''' import itchat from pandas import DataFrame itchat.login() friends=itchat.get_fri ...

  9. BDD测试之selenium控制滚动条

    一.对于页面存在滚动条,可以通过插入JS控制滚动条(最常用的方法) (1)将滚动条移动到指定坐标位置处 ((JavascriptExecutor) driver).executeScript(&quo ...

  10. BZOJ_4765_普通计算姬_分块+dfs序+树状数组

    BZOJ_4765_普通计算姬_分块 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能 ...