Shell Productivity Tips and Tricks

栏目: IT技术 · 发布时间: 4年前

内容简介: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 rep
Shell Productivity Tips and Tricks

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


以上所述就是小编给大家介绍的《Shell Productivity Tips and Tricks》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网冲击

互联网冲击

杰伦•拉尼尔 (Jaron Lanier) / 李龙泉、祝朝伟 / 中信出版社 / 2014-5-1 / CNY 65.00

在《互联网冲击》一书中,关于网络技术对经济造成的影响,作者进行了卓有远见的预测。拉尼尔断言,数字网络的崛起会造成我们经济的衰退,并且摧毁中产阶级。如今,科技已经征服了一个又一个行业——从媒体到医药业,再到制造业。我们的就业形势和个人财富都将面临更加严峻的挑战。  但还有另外一种方法,能够让科技掌握我们的未来。在本书中,作者不仅展现了他的雄心壮志,而且也处处体现着他的人文关怀。拉尼尔指明了一条新信息......一起来看看 《互联网冲击》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具