未加星标

My Favorite Command-Line Shortcuts

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I use a shell every day. Almost always, I want to repeat a previous command, or repeat it after a slight modification. A very convenient way is to use arrow-up to get the most recent command back. Another common trick is to type ctrl-r and incrementally search for a previously used command. However, there are two other tricks for repeating previous commands that I use all the time, which are not as well known.


My Favorite Command-Line Shortcuts
Escape-Dot (or !$)

Often you want to repeat only the last argument of the previous command. For example, suppose you want to run git diff path/to/tests , and then git add path/to/tests . For the second command, you can type git add escape-dot (escape and period pressed at the same time), and it gets expanded to path/to/tests (the last argument of the previous command).

I find that I quite often want to run another command on the same argument, and escape-dot is the most convenient way to do that. It also works to use !$ instead of escape-dot, but that is slightly harder to type, so I don’t use that anymore.

History With !

Sometimes I know I used a command a while back, but I don’t have a good string for searching with in ctrl-r (or maybe I will find too many unrelated hits in the search before I find the case I want). In this case I use history to get a list of the most recently used commands. Suppose I see the command I want to repeat at position 456 in the list. The !456 willrerun the command.

If I want to modify the command before running it, I type !456:p instead. Then I use arrow-up and then modify it before running it.

I like to keep a long history for my shell commands (several thousand entries). To still be able to scroll up in my shell without only seeing history entries, I have created an alias to only show the last 100 items in the list:

alias his=’history | tail -n 100; echo “Only last 100. For full, type: history” ‘

So I usually just type his , and get the last 100 commands listed.

Editing

I often want to edit what I have on the command-line before running the command (especially if I used arrow-up to get the most recent command). Here is what I use most frequently:

ctrl-a Move to the beginning of the line ctrl-e Move to the end of the line ctrl-u Clear the line (before the cursor position) ctrl-w Delete the word before the cursor position Conclusion

Most people I have worked with use both arrow-up and ctrl-r when repeating commands. However, very few are familiar with escape-dot and repeating commands from the history list. Since I use all four ways very frequently, I thought I would write a post to spread the word.

On the subject of command-line shortcuts, I also have to recommend the book Unix Power Tools . It contains over a thousand pages of well-organized, cross-referenced command-line tips. A fantastic resource for anyone who wants to up their command-line game.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

tags: command,use,want,line,ctrl,up,type,commands,before,history
分页:12
转载请注明
本文标题:My Favorite Command-Line Shortcuts
本站链接:https://www.codesec.net/view/586983.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(69)