DEV Community

Noel Worden
Noel Worden

Posted on • Edited on

Keeping Your Hands on the Keyboard: A Few Bash and Git Shortcuts

Over the last few weeks I've picked up some helpful bash shell shortcuts. I'm no bash power user, but it does hurt my soul a little bit every time I'm in a terminal window and have to reach for the mouse or trackpad. With that said, I'm not great at memorizing long lists of shortcuts. It needs to be something I use a lot, or I trickle them gradually into my existing workflow. The latter was the case with the commands that my colleague introduced me to over the past few weeks.

  • ctrl + e
    • moves the cursor to the end of the line
  • ctrl + a
    • moves the cursor to the beginning of the line
  • ctrl + r
    • starts a reverse i search where you can grep through your terminal history
    • similar to using the up arrow key to cycle through the history, but with the ability to grep
    • press ctrl + r again to cycle through all the grep results
  • ctrl + c
    • clears the current line
  • option + left/right arrow

One more command that I've been using a lot lately is git specific. In this current project we have made it a point to have a clean git history, so when making pull request updates I've been doing a lot of interactive rebasing. What I would find myself doing is getting the log of last commits, then copy/pasting that SHA into my rebase command. The process would look something like this:

Log the last commits:

git log

Result:

Manually copy the appropriate SHA, and paste it into the rebase command:

git rebase -i 2672f7a

I learned that this can also be accomplished by counting back from HEAD and using any one of the following techniques to represent that number. For example, to rebase to that same SHA, two steps back from HEAD, any one of these options would work:

HEAD^^
HEAD~~
HEAD~2

That would make the interactive rebase command look like this:

git rebase -i HEAD^^

The amount of ^ or ~ after HEAD is simply the amount of commits you want to step back; same with HEAD~2, just change the digit to the amount of steps. This technique can be applied to anything in git where you need to step back from HEAD, for example, a git reset.

These are the handful of commands I have integrated into my workflow recently. If these commands are new to you and you'd like to incorporate more into your workflow a quick internet search will bring up more lists than you can shake a stick at.


This post is part of an ongoing This Week I Learned series. I welcome any critique, feedback, or suggestions in the comments.

Top comments (1)

Collapse
 
noelworden profile image
Noel Worden

Ive been using those ^ two commands for the past week or so, definitely making life a little faster. Thanks!