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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Spring in Action

Spring in Action

Craig Walls / Manning Publications / 2011-6-29 / USD 49.99

Spring in Action, Third Edition has been completely revised to reflect the latest features, tools, practices Spring offers to java developers. It begins by introducing the core concepts of Spring and......一起来看看 《Spring in Action》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

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

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具