惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Martin Heinz's Blog

A Guide to Python's Weak References Using weakref Module Recent Docker BuildKit Features You're Missing Out On Modern Git Commands and Features You Should Be Using Everything You Can Do with Python's textwrap Module Monitoring Indoor Air Quality with Prometheus, Grafana and a CO2 Sensor Everything You Can Do with Python's bisect Module You Don't Need a Dedicated Cache Service - PostgreSQL as a Cache A Collection of Docker Images To Solve All Your Debugging Needs Weird Python "Features" That Might Catch You By Surprise Lessons Learned From Writing 100 Articles Debugging Crashes and Deadlocks in Python using PyStack Goodbye etcd, Hello PostgreSQL: Running Kubernetes with an SQL Database Remote Interactive Debugging of Python Applications Running in Kubernetes The Right Way to Run Shell Commands From Python Real Multithreading is Coming to Python - Learn How You Can Use It Now Python's Missing Batteries: Essential Libraries You're Missing Out On Kubernetes-Native Synthetic Monitoring with Kuberhealthy Make Your CLI Demos a Breeze with Zero Stress and Zero Mistakes Reduce - The Power of a Single Python Function Why I Will Never Use Alpine Linux Ever Again Cgroups - Deep Dive into Resource Management in Kubernetes Dictionary Dispatch Pattern in Python Boost Your Python Application Performance using Continuous Profiling Lazy Evaluation Using Recursive Python Generators Python Magic Methods You Haven't Heard About Getting Started with Mastodon API in Python Backup-and-Restore of Containers with Kubernetes Checkpointing API Getting Started with Google APIs in Python Python CLI Tricks That Don't Require Any Code Whatsoever All The Ways To Introspect Python Objects at Runtime What is Python's "self" Argument, Anyway? Python List Comprehensions Are More Powerful Than You Might Think You Should Be Using Python's Walrus Operator - Here's Why Recipes and Tricks for Effective Structural Pattern Matching in Python It's Time to Say Goodbye to These Obsolete Python Libraries Advanced Features of Kubernetes' Horizontal Pod Autoscaler Data and System Visualization Tools That Will Boost Your Productivity Stop Messing with Kubernetes Finalizers Automate All the Boring Kubernetes Operations with Python End-to-End Monitoring with Grafana Cloud with Minimal Effort Bitly | bit.ly/3JLmSgA Bitly | bit.ly/3uETfbi Ultimate CI Pipeline for All of Your Python Projects Bitly | bit.ly/3M30D82 Bitly | bit.ly/3oMJ6qR Bitly | bit.ly/3IRD7IK Bitly | bit.ly/3A3B69t Profiling and Analyzing Performance of Python Programs Bitly | bit.ly/30uviIM Bitly | bit.ly/3E1X2mw Bitly | bit.ly/3Dv7JxP Bitly | bit.ly/3GG1BEz Bitly | bit.ly/3lLavs4 Bitly | bit.ly/39TqP3m Bitly | bit.ly/3A5Mpx8 Bitly | bit.ly/3kGwPl4 Bitly | bit.ly/3iHtulU Bitly | bit.ly/3xGjtKS Bitly | bit.ly/3h8DZg0 Bitly | bit.ly/2RQn1dG Bitly | bit.ly/3p2B5wW Bitly | bit.ly/3tULpb0 Bitly | bit.ly/2PHVudx Bitly | bit.ly/3uPtnb0 Bitly | bit.ly/3dg3QR9 Bitly | bit.ly/3qHtSkZ Bitly | bit.ly/3kIkTPr Bitly | bit.ly/3qlRAUN Bitly | bit.ly/3pCUJ26 Hardening Docker and Kubernetes with seccomp Bitly | bit.ly/34ZhIMt Bitly | bit.ly/3qSO7h0 Bitly | bit.ly/3muGLOk Bitly | bit.ly/35xN79v Bitly | bit.ly/3mLGshK Bitly | bit.ly/2IvkGQl Bitly | bit.ly/2Sk1KFK Bitly | bit.ly/3iCNIL6 Saving Your Linux Machine from Certain Death New Features in Python 3.9 You Should Know About Deploy Any Python Project to Kubernetes Analyzing Docker Image Security Recursive SQL Queries with PostgreSQL Automating Every Aspect of Your Python Project Tour of Python Itertools Implementing 2D Physics in Javascript Ultimate Setup for Your Next Python Project Making Python Programs Blazingly Fast Security and Cryptography Mistakes You Are Probably Doing All The Time Going Serverless with OpenFaaS and Golang - Building Optimized Templates Going Serverless with OpenFaaS and Golang - The Ultimate Setup and Workflow Setting Up Swagger Docs for Golang API Building RESTful APIs in Golang Pytest Features, That You Need in Your (Testing) Life Setting up GitHub Package Registry with Docker and Golang Ultimate Setup for Your Next Golang Project Python Tips and Trick, You Haven't Already Seen, Part 2. Tricks for Postgres and Docker that will make your life easier Getting The Most Out of Reading Books - Reading The "Professional Way" Python Tips and Trick, You Haven't Already Seen
Shell History Is Your Best Productivity Tool
Martin · 2024-04-08 · via Martin Heinz's Blog

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.

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


# ~/.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 infinite amount of command history. It also makes it 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 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 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 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 number of lines for both history and fc command. Additionally, the -E and -i will give different date formats.

fc - without arguments - can be 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):

ZSH FZF Demo

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 commandline:


bindkey "^[[A" up-line-or-beginning-search    # Arrow up
bindkey "^[[B" down-line-or-beginning-search  # Arrow down

ZSH bck-i-search Demo

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 a 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 following gist.