DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

My Favorite Command-Line Shortcuts

If you use a UNIX-based shell, then these shortcuts could help you save time when using or editing repeated commands while programming.

Henrik Warne user avatar by
Henrik Warne
·
Aug. 15, 18 · Tutorial
Like (9)
Save
Tweet
Share
8.35K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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 followed by a period), 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. !456 will rerun 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.

Command (computing)

Published at DZone with permission of Henrik Warne, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Agile Architecture Spikes Are Used in Shift-Left BDD
  • 7 Ways for Better Collaboration Among Your Testers and Developers
  • Utilizing Database Hooks Like a Pro in Node.js
  • Browser Engines: The Crux of Cross-Browser Compatibility

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: