28

Shell Productivity Tips and Tricks

 4 years ago
source link: https://blog.balthazar-rouberol.com/shell-productivity-tips-and-tricks.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
2URz6nR.jpg!web

This article is part of a self-published book project by Balthazar Rouberol and Etienne Brodu , ex-roommates, friends and colleagues, aiming at empowering the up and coming generation of developers. We currently are hard at work on it!

If you are interested in the project, we invite you to join the mailing list !

Table of Contents

Shell productivity tips

I estimate that I spend around 50% of my day working in my text editor and my terminal. Any way I can get more productive in these environments has a direct and measurable impact on my daily productivity as a whole.

If you spend a good chunk of your day repeatedly hitting the left and right arrow keys to navigate in long commands or correct typos, or hitting the up or down arrow keys to navigate your command history, this chapter should help you get more done quicker. We will cover some shell features you can leverage to make your shell do more of the work for you.

On a personal level, I probably use some of these up to 30 times a day, sometimes even without thinking about it, and it gives me a real sense of ownership of my tool.

In the immortal words of Kimberly “Sweet Brown” Wilkins:

Ain't nobody got time for that.

Tab completion

When you are typing in your shell, I suggest you treat the Tab key as a superpower. Indeed, the same way your phone keyboard can autocomplete words for you, so can your shell. It can suggest completions of command names and even command arguments or options! This works by pressing Tab (twice for bash and once for zsh ).

One of the reasons zsh might be favored over bash is its more powerful auto-completion system, giving more results out-of-the-box and allowing you to navigate through the auto-completion options.

Here is an example of bash auto-completing a command name:

$ mkd<Tab>
mkdep  mkdir

Here is an example of bash auto-completing a command argument:

$ man mkd<Tab>
mkdir         mkdirat       mkdtemp       mkdtempat_np

And finally, an example of bash auto-completing a command option:

$ python -<Tab>
-    -3   -B   -E   -O   -OO  -Q   -R   -S   -V   -W
-b   -c   -d   -h   -i   -m   -s   -t   -u   -v   -x

I suggest you get used to using auto-completion as much as possible. It can save you keystrokes, as well as make you discover command options you didn't know about.

Pro-tip: if you are using bash, you can get install the bash-completion package (using your system package-manager) in order to enable auto-completion for a wide variety of commands that do not support it out-of-the-box.

Keyboard shortcuts

The shell uses a library called readline to provide you with many keyboard shortcuts to navigate, edit, cut, paste, search, etc, in the command line. Mastering these will help to dramatically increase your efficiency, instead of copying and pasting with your mouse, and navigating the command with the ↑ and ↓ arrow keys.

The default shortcuts are inspired by the emacs terminal-based text editor. If you are already familiar with it, a lot of the default readline shortcuts might feel familiar. emacs isn't the only famous text editor in the history of computers though: another one, dating back from 1976, is vi . vi and emacs are designed in two very different ways, and have two very different logics. It is possible that one might “click” more than the other for you. If you happen to be familiar with the vi editor and are accustomed to its navigation system, you can replicate it in your shell as well by adding set -o vi in your shell configuration file. If you are using zsh with the Oh My Zsh framework that we introduced in the previous chapter, you can also use the vi-mode plugin to do this.

The advantage of using the same navigation logic and shortcuts in your text editor and your terminal is that is blurs the line between both, and brings consistency to your terminal environment. If you have no clue how emacs or vi work though, I would probably suggest you don't worry about all this for now and experiment with the default terminal shortcuts.

Navigating the current line

The following navigation shortcuts allow you to move quickly your cursor in the current command saving you from relying solely on the → and ← arrows.

Navigation Shortcut Go to beginning of line Ctrl - A Go to end of line Ctrl - E Go to next word Alt - F Go to previous word Alt - B Toggle your cursor between its current position and the beginning of line Ctrl - X - X

If you however prefer using the vi navigation system, you will first need to type Esc to switch from the Insertion mode to an emulation of vi 's normal mode, in which you can navigate in your text using the following shortcuts:

Navigation Shortcut Go to beginning of line ^ Go to end of line $ Go to next word w Go to previous word b Move to the end of the previous word e

You can go back to editing your command line by hitting the i key.

Deleting and editing text

These shortcuts allow you to quickly edit the current command more efficiently than by just using the Delete key.

Edition Shortcut Delete current character Ctrl - D Delete previous word Ctrl - W Delete next word Alt - D Edit the current command in your text editor Ctrl - X Ctrl - E Undo previous action(s) Ctrl - -

The equivalent vi -style shortcuts are:

Edition Shortcut Replace current character by another (ex: e ) r - e Delete current character x Delete previous word d - b Delete next word d - w Edit the current command in your text editor v Undo previous action(s) u

Cutting and pasting

The shell provides you with shortcuts to cut and paste commands quickly without using your mouse.

Action Shortcut Cut current word before the cursor Ctrl - W Cut from cursor to end of line Ctrl - K Cut from cursor to start of line Ctrl - U Paste the cut buffer at current position Ctrl - Y

The equivalent vi -style shortcuts are:

Action Shortcut Cut current word before the cursor d - w Cut from cursor to end of line d - $ Cut from cursor to start of line d - ^ Paste the cut buffer at current position p

Controlling the terminal

Finally, these shortcuts will let you interact with the terminal itself.

Action Shortcut Equivalent command Clear the terminal screen Ctrl - L clear Close the terminal screen Ctrl - D exit Send current command to the background. Ctrl - Z

Even mastering some of these shortcuts should make you immensely more productive at typing commands and navigating command-line interfaces. I suggest you take time to experiment until you feel more accustomed with them. I can guarantee that you will feel the productivity boost!

A u


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK