Linux 命令行技巧

减少键盘输入,可以大大提高程序员的工作效率,快捷键的使用就是一个很好的例子。程序员经常使用终端,而且程序员的工作往往是前后相关连的。所以,如果能利用上一条命令的信息,就能大幅提高工作效率。本文将演示这些技巧。

  • 1.使用上一条命令的所有参数

方法:!*
例子:如果我对 hello.txt 和 bye.txt 进行了编辑,然后希望使用 git add 添加这两个文件。就可以使用:git add !*

[ggli@localhost ~]$ gedit hello.txt bye.txt
[ggli@localhost ~]$ git add !*
git add hello.txt bye.txt
[ggli@localhost ~]$
  • 2.使用上一条命令的最后一个参数

方法:!$
ALT + .
ESC + .
其中后面两种方法,terminal 中会自动补全

[ggli@localhost ~]$ gedit hello.txt bye.txt
[ggli@localhost ~]$ git add !$
git add bye.txt
[ggli@localhost ~]$
  • 3.使用上一条命令中除了最后一个参数的部分

方法:!:-

[ggli@localhost ~]$ gedit hello.txt bye.txt
[ggli@localhost ~]$ !:- hi.txt
gedit hello.txt hi.txt
[ggli@localhost ~]$
  • 4.使用上一条命令中任意一个部份

方法:ALT + <num> + .
其中 num 表示的上一条命令中的第几部分,从0开始,对于 ls -shld hello.txt,ALT +0+. 就是 ls,1就是-shld

  • 5.替换上一条命令中的一个部份

方法:将foo替换为bar
^foo^bar 仅替换地一个
!!:gs/foo/bar 替换所有

[ggli@localhost ~]$ touch foo_1 foo_2
[ggli@localhost ~]$ ^foo^bar
touch bar_1 foo_2
[ggli@localhost ~]$ touch bar_1 bar_2
[ggli@localhost ~]$ !!:gs/bar/foobar
touch foobar_1 foobar_2
[ggli@localhost ~]$ 
  • 6.上一条命令

方法:!!

[ggli@localhost ~]$ gedit kiwi_home.txt kiwi_school.txt
[ggli@localhost ~]$ !!:gs/kiwi/lucy
gedit lucy_home.txt lucy_school.txt
[ggli@localhost ~]$

最后:推荐一个网站,可以这样来学习,如果遇到一条需要输入的命令,让你觉得需要键盘输入过多,或者说你猜想可能有更好的、更快捷的方式时,不妨到这个网站上来搜一下,这样慢慢积累,加以时日,应该还是有一定的收获:commandlinefu.com

更多:我们经常会运行很多很长的命令,这个时候,可以通过 history 查看命令,然后用 !(history中命令编号)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值