Shell Keyboard Shortcuts Pt. 1

As developers, we spend a great deal of time in our text editors and terminals. Any boost in efficiency with these tools has a direct impact on our productivity. Small gains with these tools add up over time and get us closer to feeling like our computers can respond immediately to our thoughts.

This series of posts is primarily a way for me to store and reference some of the shell tricks and tips I've come across that I can never remember. I hope that they can be of some use to you as well. 1

I should also note: my understanding is that all of these commands will work with both bash and zsh default shell configurations, but if I'm mistaken in this please reach out to correct me!

Navigating the current line

Moving around the current line in your terminal with the and  keys is slow - it's no way to live your life. The following table shows some shortcuts for navigating the current command quickly.

NavigationShortcut
Go to beginning of lineCtrl + A
Go to end of lineCtrl + E
Go to next wordAlt + F
Go to previous wordAlt + B
Toggle cursor between current position and beginning of lineCtrl + X + X

Editing and deleting text

These commands help quickly edit and delete items in the current command.

EditsShortcut
Delete previous word (via cutting)Ctrl + W
Delete next word (via cutting)Alt + D
Edit current command in default terminal text editorCtrl + X - Ctrl + E
Clear command lineCtrl + U

Cutting and pasting text

The shell has some built-in commands that can help you quickly cut and paste text without using the mouse.

ActionShortcut
Cut previous wordCtrl + W
Cut next wordAlt + D
Cut from cursor to end of lineCtrl + K
Cut from cursor to beginning of lineCtrl + U
Paste the cut contents at current cursor positionCtrl + Y

There is a little bit of nuance here.

The Ctrl + W and Alt + D commands build up the cut buffer sequentially from within the current command. This can be useful if you want to cut the first/last three words in a command, for example, but not the entire line. This behavior, as far as I can tell, does not allow you to bounce around different parts of the command, but only from any given word forward/backward. As soon as you start navigating around other parts of the command and making cuts, it seems the cut buffer is overwritten.

(If my understanding of this is flawed, please let me know.)


Controlling the terminal

ActionShortcutCommand
Clear terminal screenCtrl + Lclear
Close terminal screen / tabCtrl + Dexit
Stop current jobCtrl + Z
Restart stopped job and send to foregroundfg
Restart stopped job and send to backgroundbg
List running and stopped jobsjobs

One nice shortcut for starting a program and immediately sending it to the background is the & operator. For example, the following command would start firefox and send the job to the background of the current terminal instance.

firefox &

This list is just a taste of the things we can do with our terminals. Mastering these shortcuts will greatly increase how quickly and efficiently you can navigate and control your command-line environment.

If you have any other keyboard shortcuts that you feel should be included in this list, please reach out and let me know.


  1. I learned about many of these shortcuts from this blog post

Now What?

If you enjoyed this post, I'd love to continue the conversation on Twitter.

Social media not your thing? Shoot me an email: will@willharris.dev

Designed and developed by Will Harris

Built with

Hosted on

Social

Twitter

GitHub

LinkedIn