Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maximizing Productivity with Shell History: Tips and Configurations #809

Open
1 task
ShellLM opened this issue Apr 14, 2024 · 1 comment
Open
1 task
Labels
CLI-UX Command Line Interface user experience and best practices linux Linux notes tools links shell-script shell scripting in Bash, ZSH, POSIX etc software-engineering Best practice for software engineering

Comments

@ShellLM
Copy link
Collaborator

ShellLM commented Apr 14, 2024

# Maximizing Productivity with Shell History: Tips and Configurations

**Shell History Is Your Best Productivity Tool**

*MARTIN* | Apr 8, 2024 | DevOps

If you work in shell/terminal often enough, then over time the history will become your personal knowledge vault, documentation, and command reference. Being able to use this personal documentation efficiently can hugely boost your productivity. So, here are a couple of tips on how to optimize your shell history configuration and usage to get the most out of it.

## Configuration

I use ZSH and Oh My Zsh, so tips in this article will focus on ZSH, but similar results can be achieved for Bash or other shells.

If you're using Bash and would like to switch to ZSH, or try out the examples shown below, then check out [this article](https://martinheinz.dev/blog/110).

With that out of the way, let's start with configuration options that will help us get the most out of the saved history:

```zsh
# ~/.zshrc
# ...

HISTFILE="$HOME/.zsh_history"
HISTSIZE=10000000
SAVEHIST=10000000

HISTORY_IGNORE="(ls|cd|pwd|exit|cd)*"

We can't search and use history efficiently, unless we actually save/keep the data. The above config makes it so that we store pretty much an infinite amount of command history. It also uses HISTORY_IGNORE to list commands like ls, cd etc. that won't be stored in history, you can adjust this ignore list to your liking.

To further optimize the configuration, we can add the following ZSH options:

# ~/.zshrc
# https://zsh.sourceforge.io/Doc/Release/Options.html (16.2.4 History)

setopt EXTENDED_HISTORY      # Write the history file in the ':start:elapsed;command' format.
setopt INC_APPEND_HISTORY    # Write to the history file immediately, not when the shell exits.
setopt SHARE_HISTORY         # Share history between all sessions.
setopt HIST_IGNORE_DUPS      # Do not record an event that was just recorded again.
setopt HIST_IGNORE_ALL_DUPS  # Delete an old recorded event if a new event is a duplicate.
setopt HIST_IGNORE_SPACE     # Do not record an event starting with a space.
setopt HIST_SAVE_NO_DUPS     # Do not write a duplicate event to the history file.
setopt HIST_VERIFY           # Do not execute immediately upon history expansion.
setopt APPEND_HISTORY        # append to history file (Default)
setopt HIST_NO_STORE         # Don't store history commands
setopt HIST_REDUCE_BLANKS    # Remove superfluous blanks from each command line being added to the history.

The above mainly configures ZSH to ignore certain commands to avoid cluttering history, e.g., duplicates or history commands. It also adds timestamps to each command which will be handy when searching (see next section).

One particular option I would highlight here is HIST_IGNORE_SPACE which can be useful if you want to avoid storing secrets in history - simply prefix any command that includes a secret/password with space, e.g., export AWS_ACCESS_KEY_ID=... (notice the space before export) and it won't appear in history.

The previous options configured how the history is stored, now let's talk about configuring how the history is searched and presented.

By default, you can only search by exact match, but ZSH includes a plugin, that makes it possible to search using fuzzy-search. To enable it, we just need to add the plugin:

# ~/.zshrc
# ...

plugins=(git fzf)

With that, when you search history, ZSH will automatically use FZF for fuzzy search. This, however, might use find command in the background which isn't very fast. So, to improve performance (for very large history files) I recommend using ag:

sudo apt-get install silversearcher-ag

# add to # ~/.zshrc
export FZF_DEFAULT_COMMAND='ag --hidden -g ""'

And the last configuration option I want to mention is history timestamp formatting - by default when you view history, you will get output like:

10206  echo hello
10207  head some-file.txt
10208  curl google.com
10209  cat some-file.txt

That is - line number from history file and the command itself. However, by adding HIST_STAMPS="yyyy-mm-dd" to ~/.zshrc, we can get the following:

10206  2024-03-30 12:29  echo hello
10207  2024-03-30 12:51  head some-file.txt
10208  2024-03-30 13:21  curl google.com
10209  2024-03-30 14:15  cat some-file.txt

Searching

Now that we've configured everything, let's see how we can efficiently search all the history.

The simplest way - without keybindings - is to use the history command. However, just using history will dump the whole history file which isn't practical, instead we can use:

\history -E -10

10206  30.3.2024 12:29  echo hello
10207  30.3.2024 12:51  head some-file.txt
10208  30.3.2024 13:21  curl google.com
10209  30.3.2024 14:15  cat some-file.txt
...

\history -i

10206  2024-03-30 12:29  echo hello
10207  2024-03-30 12:51  head some-file.txt
10208  2024-03-30 13:21  curl google.com
10209  2024-03-30 14:15  cat some-file.txt
...

fc -E -l -10

10206  30.3.2024 12:29  echo hello
10207  30.3.2024 12:51  head some-file.txt
10208  30.3.2024 13:21  curl google.com
10209  30.3.2024 14:15  cat some-file.txt
...

We can use the -<N> flag to specify the number of lines for both history and fc command. Additionally, the -E and -i will give different date formats.

fc - without arguments - can also be used if you want to edit and re-execute the last command - it opens the default editor, where you can edit the command, and when you close it, the command gets executed.

Another useful command is r, which re-executes the last command without editing (like !!), and r <WORD>, which executes the last command that contains the string WORD.

Most of the time, though, we want to search history with keybind - in ZSH, the default CTRL+R will give you the context-based history scrolling (the history widget):

As you can see that also includes the FZF fuzzy search.

If - for whatever reason - this doesn't work on your machine check the keybindings:

bindkey | grep fzf-history-widget

"^R" fzf-history-widget  # ^R is CTRL+R

If you prefer the classic reverse search, instead of context-based history scrolling, then you can bind the history-incremental-search-backward and history-incremental-search-forward events:

# Add to ~/.zshrc
bindkey "^E" history-incremental-search-backward  # CTRL+E
bindkey "^S" history-incremental-search-forward   # CTRL+S

You can also simply start typing a command, and press up/down arrow to search through history, taking into account what's typed on the command line:

bindkey "^[OA" up-line-or-beginning-search    # Arrow up
bindkey "^[OB" down-line-or-beginning-search  # Arrow down

There are also more variations in docs, e.g.:

  • history-beginning-search-backward
  • history-beginning-search-backward-end
  • history-search-backward
  • ...

Be aware, that the keycodes may differ based on your OS, e.g., up arrow can be \e[A, ^[OA or ^[[A on different systems.

As you probably noticed from the above GIF, you can have syntax highlighting for this classic reverse-search, just install zsh-syntax-highlighting with:

sudo apt-get install zsh-syntax-highlighting
echo "source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc

Synchronization

To take it to the next level, and to have your shell history always with you on different workstations, you can additionally use ZSH history-sync plugin, which uses Git to sync history across workstations - see plugin docs for explanation of how it works and how to set it up.

A more modern alternative to the above sync functionality is atuin.sh, which uses SQLite as a storage and supports ZSH - see the website for features and details.

Conclusion

At this point, I'm pretty sure, that if I lost my .zsh_history I probably wouldn't be able to do my job anymore. Most of the commands I run are really just CTRL+R plus minor edits.

So, with that said, hopefully, with the above tips and configs, your shell history can become your personal knowledge vault and can help you be a bit more efficient in shell.

Note: My complete ~/.zshrc (stripped of comments) can be found in the following gist.

Copyright © 2023 Martin Heinz


#### Suggested labels
#### None
@ShellLM ShellLM added CLI-UX Command Line Interface user experience and best practices shell-script shell scripting in Bash, ZSH, POSIX etc software-engineering Best practice for software engineering labels Apr 14, 2024
@ShellLM
Copy link
Collaborator Author

ShellLM commented Apr 14, 2024

Related content

#701 similarity score: 0.9
#740 similarity score: 0.86
#16 similarity score: 0.85
#743 similarity score: 0.84
#6 similarity score: 0.84
#114 similarity score: 0.83

@irthomasthomas irthomasthomas added the linux Linux notes tools links label Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI-UX Command Line Interface user experience and best practices linux Linux notes tools links shell-script shell scripting in Bash, ZSH, POSIX etc software-engineering Best practice for software engineering
Projects
None yet
Development

No branches or pull requests

2 participants