boosting-terminal-productivity-tips

In this blog post, I will share a few tips that will help you boost your productivity while working with a terminal.

macOS users - CTRL is still CTRL

history command

An old friend of mine once told me ...

"History is written by the victors." (Winston Churchill)

Search in history

I used to hit the UP_ARROW to find commands that were previously executed. And then, one of my colleagues told me "Use CTRL+R". This is the most important shortcut, you can stop reading this blog-post, it's totally enough to embrace only this one.

unfor19@~ $ python --version
Python 3.8.5
unfor19@~ $ yarn --version
1.19.1
unfor19@~ $ git --version
git version 2.28.0.windows.1
# Now I hit CTRL+R, and search for `p`
(reverse-i-search)`p': python --version

NOTE: Use history | grep "something" to view multiple lines. It's not as cool as CTRL+R though, because you can't directly execute the command, you gotta' copy-paste it.

Is it secret? Is it safe?

To keep the history clean from private keys that were used, one can simply add whitespace before the command that shouldn't be hidden saved in the history.

# Saved in history - Bad
TOKEN=myAwSom3c00lt0k3n

# Not saved in history - Good
 TOKEN=myAwSom3c00lt0k3n
^ # whitespace

But what if we forget to do that? It's possible to delete a specific line from the history

# Let's see my history ... OMG a token in line #5
unfor19@~ $ history
    1  python --version
    2  yarn --version
    3  git --version
    4  history
    5  TOKEN=myAwSom3c00lt0k3n
    6  history

# Let's delete line #5
unfor19@~ $ history -d 5

# Token is not in the history!
unfor19@~ $ history
    1  python --version
    2  yarn --version
    3  git --version
    4  history
    5  history
    6  history -d 5
    7  history

So it worked, but if I open a new terminal window, I'll still see the token, because -d is relevant for the active shell (terminal) only. To make the changes persistent, you need to use the -w flag, which stands for "write changes" (feels like a database).

# Apply changes to $HISTFILE
history -w

Clean history

To clean up all history, without specifying a line number, you can use the -c flag, combined with the -w flag.

unfor19@~ $ history
    1  python --version
    2  yarn --version
    3  git --version
    4  history
    5  history
    6  history -d 5
    7  history
unfor19@~ $ history -cw
unfor19@~ $ history
    1  history
# All clean!
# You can also split it into two commands
# history -c # clean
# history -w # write (apply) changes to $HISTFILE

Increase history size

I know how to search CTRL+R and clean history -wc or history -d 5, but what If I want to increase the size of my history? I feel that the default 1000 lines is not enough, and I'd like to store more commands in my history.

Add to your ~/.SHELLrc, such as ~/.bashrc, ~/.zshrc, and so on.

HISTSIZE=3000 # per session (terminal window)
HISTFILESIZE=3000 # max file size of $HISTFILE

NOTE: To check the path of your history file echo its location with echo "$HISTFILE"

Jump around

jump-around

Start to end

I remember this one with CTRL+A (A=start of Abc) and CTRL+E (E=end)

# ^cursor - after the pointed character
curl -sL https://catfact.ninja/fact | jq -r .fact
#                                               ^cursor
# Hit CTRL+A
curl -sL https://catfact.ninja/fact | jq -r .fact
# cursor is before the first character

# Hit CTRL+E
curl -sL https://catfact.ninja/fact | jq -r .fact
#                                               ^cursor

Word by word

Moving right and left with RIGHT_ARROW and LEFT_ARROW is very annoying, especially if you want to jump to a specific word.

# ^cursor - after the pointed character
curl -sL https://catfact.ninja/fact | jq -r .fact
#                                               ^cursor
# Hold ALT and hit LEFT_ARROW once
#                                           ^cursor
# Hold ALT and hit LEFT_ARROW multiple times
# The cursor will jump between words to the left
# Same goes to the right by holding ALT and hitting RIGHT_ARROW

Delete the word

Without further ado...

# ^cursor - after the pointed character
curl -sL https://catfact.ninja/fact | jq -r .fact
#                                 ^cursor
# Hold ALT and hit BACKSPACE
curl -sL https://catfact.ninja/ | jq -r .fact
#                             ^cursor

The bird's the word!

bird-is-the-word

Final words

It's not easy to remember multiple shortcuts all at once, and sometimes it's intimidating. Just make sure you memorize CTRL+R(search history), which is definitely a game changer.

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *