featured_image

Brian Kabiro

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.

ctrl-a

Do you want to move the cursor to the end of the line?

ctrl-e

A good way of remembering these two commands is that a is the start of the alphabet and e stands for end.

Cutting text

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 ctrl-y.

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.

Searching commands

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 masteresc ,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.

Conclusion

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. 🙂

featured_image
About the Author

Brian Kabiro

Software Engineer at Andela

Thanks for subscribing!

 

May 21, 2019

A Few Unix Shell Shortcuts To Make You More Productive

Brian Kabiro

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.

ctrl-a

Do you want to move the cursor to the end of the line?

ctrl-e

A good way of remembering these two commands is that a is the start of the alphabet and e stands for end.

Cutting text

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 ctrl-y.

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.

Searching commands

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 masteresc ,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.

Conclusion

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. 🙂

featured_image
About the Author

Brian Kabiro

Software Engineer at Andela

Thanks for subscribing!

 

More Blog Posts

Insights

The Efficacy of Lean UX

The concept behind Lean UX is that the first design iteration isn’t conclusive, so don’t spend too much time on it. Build a minimum working product based on assumpt

16_October_2018
Insights

Why Coders Should Care About Ethics

The average coder’s work isn’t partic

6_September_2018
Insights

Andela Program Manager, Chimdindu’s Journey to Google I/O 2017

This is a guest post by Andela Developer,

16_May_2017

Partners

Tap into a global talent pool and hire the “right” developers in days, not months.

Developers

Accelerate your career by working with high-performing engineering teams around the world.

BECOME A DEVELOPER

Hire Developers

We take great pride in matching our developers with the best partners. Tell us about your team below!

preloader_image

Thank you for your interest

A member of our team will reach out to you soon.