Or how to minimize your mouse usage.
“Code is read much more often than it is written.” — Guido van Rossum
I think the same can be said for terminal commands except that we edit them more than we write new ones.
Whether it’s scrolling up the terminal or using the mouse to copy a command, it’s possible to use your keyboard to do all of these things. Additionally, not having to shift from the keyboard to the mouse will ease the strain on fingers.
For some of the commands, we’ll be using the Control key on Mac which is interchangeable with the Ctrl key on Windows.
These shortcuts are meant to run on Unix shells and so will work on bash, zsh and fish e.t.c.
Let’s get started!
Moving the cursor
Let’s say you wanted to check out the master branch using git. However because you’ve perhaps been so into Game of Thrones lately, you type it in as:
The cursor is already at the end of the line. You want to move it to the start of the line and change got to git? Hold the control button and press a.
Do you want to move the cursor to the end of the line?
A good way of remembering these two commands is that a is the start of the alphabet and e stands for end.
Have you ever typed something only to realize that you need to delete the whole line? Pressing backspace is inefficient especially when deleting long commands.
If the cursor is at the end of the line, use
ctrl-u to cut all the text until the start of the line.
If you are at the start of the line, you can use
ctrl-k to cut all the text until the end.
Since both ctrl-k and ctrl-u actually cut the text, it’s possible to paste this text back to the terminal using
Sometimes you only need to delete a piece of text and not the entire line. For this, you can use
ctrl-w to delete a word.
You have run a command before but you can’t remember it word for word. Not to worry, ctrl-r is here to help.
ctrl-r is like Google/DuckDuckGo for your terminal.
It opens up a “search bar” where you can type in anything and it will try and look for a previous command that’s closest to what you’ve typed.
After getting a match, you can just use
ctrl-r to cycle through the results to find the one that you are looking for.
The last command
Typing in `!!` references the last command that you ran. At first, this might seem trivial because you could just press the up button on your keyboard to go to the previous command.
However, its power comes in when you want to append it to a command.
Let’s say you’ve run a command and find out that it requires sudo. You can do
sudo !! and this will append the last command to sudo. The example below is not the best but I hope it helps in understanding when it’s best to use.
esc ,: holding the escape button and then pressing the comma key returns the last parameter of the last command that was run.
If the last command was git checkout master,
esc ,will return master and
!!will return git checkout master.
It is best used when you want to use a parameter in the last command without having to type it all over again.
It’s not possible to learn all of these shortcuts at once. A good way to do this is to come back and refer to them when you find that there’s something that you are doing that isn’t efficient.
I hope that this saves you some developer hours and shows you how easy it is to change got to git. 🙂