DZone
DevOps Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > My 5 Favorite Shell Shortcuts

My 5 Favorite Shell Shortcuts

Aaron Nichols user avatar by
Aaron Nichols
·
Jan. 07, 12 · DevOps Zone · Interview
Like (0)
Save
Tweet
11.57K Views

Join the DZone community and get the full member experience.

Join For Free

I love shortcuts. I do my best to learn keyboard shortcuts, I setup aliases, and I like using anything that makes things faster. Here are a few of my favorites for a shell in Linux or OS X. These aren’t ninja shell moves that folks who’ve been doing this for a while will not know – these are basic shortcuts that everyone should know.

1) !$

I use this shortcut every single day when working in bash or zsh. This takes the last argument of the previous command and uses it wherever this variable is used. Here is an example:

Copy a file & then edit the new file.

$ cp file-a file-b
$ vim !$

If you are a zsh user you can take this one further and reference specific elements of the previous command using !! where is the element you want to reference:

~/> touch this that the other thing
~/> ls !!2
~/> ls that
that

The indexes start with 1.

2) grep -ri

I’m sure a bunch of you are thinking “Seriously?” and I would have too until a week or so ago when I watched someone type this:

  $ grep */*/*/*

I asked what the hell they were doing, they said they were checking the files under each directory (they kept adding asterisks for each level). This person wasn’t Jr, but was used to other operating systems that didn’t have a recurse option. Modern operating systems with any respectable utility have a -r (recurse) option, you should use it.

The -i argument makes it case insensitive.

3) cd -

This will take you back to the directory you were in previously. There are lots of other tricks with ‘cd’ but this is one I use all the time.

Lets say you are deep in some directory and need to hop to another directory quickly:

[/usr/share/fonts/dejavu]$ cd /var/log
[/var/log]$ cd -
/usr/share/fonts/dejavu
[/usr/share/fonts/dejavu]$

4) Reverse i-search

At least in bash & zsh, typing Control-R will allow you to search as you type through your history. This is really handy when you know the start, middle, or end of a command but aren’t sure what the rest is. Try it, type Control-R and start to type a command you’ve typed previously – once you find the one you want, just edit it (or not) and go.

5) alias

If you aren’t building aliases then you are missing out on one of the oldest shortcuts here. An alias allows you to define a command which calls another command. I make very heavy use of these and I see plenty of people who don’t.

alias vipuppet='cd ~/projects/puppet ; mvim .'



Source:  http://www.opsbs.com/index.php/2011/12/my-favorite-shell-shortcuts/


shell

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 9 Strategies to Improve Your Software Development Process
  • The Importance of Semantics for Data Lakehouses
  • Develop With Oracle Transactional Event Queues
  • Machine Learning in Cybersecurity

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo