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

以下信息来自 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. sniffer 简介

    http://www.doc88.com/p-095375416629.html 介绍sniffer的工作原理及简单介绍.

  2. PiggyMetrics windows 部署

    PiggyMetrics 是springcloud的demo,其特性就不细说了,主要描述在win10下部署的坑. 官网是:https://github.com/sqshq/PiggyMetrics 官 ...

  3. 关于二叉查找树的一些事儿(bst详解,平衡树入门)

    最近刚学了平衡树,然后突发奇想写几篇博客纪念一下,可能由于是刚学的缘故,还有点儿生疏,望大家海涵 说到平衡树,就不得不从基础说起,而基础,正是二叉查找树 什么是二叉查找树?? 大家观察一下下面的这棵二 ...

  4. Spring Cloud authentication with JWT service

    @RequestMapping(value = "/authenticate", method = RequestMethod.POST) public ResponseEntit ...

  5. serialPort操作结构体Hashtable的使用

  6. 关于for循环里面异步操作的问题

    首先来看一个比较简单的问题,我们想实现的就是每隔1s输出0-4的值,就是这么简单,看下错误写法: function test() { for (var i = 0; i < 5; ++i) { ...

  7. centos网络配置方法(手动设置,自动获取)

    不知道为什么最近一段时间网络特别的慢,还老是断,断的时候,局域网都连不上,当我手动设置一下ip后就可以了,搞得我很无语.下面是2种设置网络连接的方法,在说怎么设置前,一定要做好备份工作,特别是对于新手 ...

  8. 巩固java(二)----JVM堆内存结构及垃圾回收机制

    前言:        我们在运行程序时,有时会碰到内存溢出(OutOfMemoryError)的问题,为了解决这种问题,我们有必要了解JVM的内存结构和垃圾回收机制. 正文: 1.JVM堆内存结构   ...

  9. 18.app后端如何实现LBS

    移动互联网,除了一直在线这个特点外,还有一个重要特点,能定位到手机的位置.查找附近的人,附近的餐馆等服务,以及大量的o2o应用, 都需要使用LBS(Location Based Services).那 ...

  10. HSLA色相饱和透明度

    H:Hue(色调),取值为:0 - 360.将色相值想成一个圆环中的度数,随着在圆环上移动,得到不同的颜色. S:Saturation(饱和度),取值为:0.0% - 100.0%.数值越低(降低饱和 ...