Linux 命令行技巧

来源:互联网 发布:mysql增加表空间大小 编辑:程序博客网 时间:2024/06/05 07:23

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

  • 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.txtgedit 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^bartouch bar_1 foo_2[ggli@localhost ~]$ touch bar_1 bar_2[ggli@localhost ~]$ !!:gs/bar/foobartouch foobar_1 foobar_2[ggli@localhost ~]$ 
  • 6.上一条命令

方法:!!

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

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

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

0 0
原创粉丝点击