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

推荐订阅源

博客园 - 聂微东
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
博客园 - 司徒正美
博客园 - Franky
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
量子位
博客园 - 叶小钗
博客园_首页
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Better Zsh history
2021-03-24 · via jdhao's digital space

My notes on setting up history-related features for zsh.

History settings#

Some env variable controlling the behaviour of history feature:

# the detailed meaning of the below three variable can be found in `man zshparam`.
export HISTFILE=~/.histfile
export HISTSIZE=1000000   # the number of items for the internal history list
export SAVEHIST=1000000   # maximum number of items for the history file

# The meaning of these options can be found in man page of `zshoptions`.
setopt HIST_IGNORE_ALL_DUPS  # do not put duplicated command into history list
setopt HIST_SAVE_NO_DUPS  # do not save duplicated command
setopt HIST_REDUCE_BLANKS  # remove unnecessary blanks
setopt INC_APPEND_HISTORY_TIME  # append command to history file immediately after execution
setopt EXTENDED_HISTORY  # record command start time

In the above config, option EXTENDED_HISTORY makes sure that zsh record the time that a certain command is executed. By default, command typed in a shell session are saved in the internal command history list. When the shell exits, commands in the internal history list will be written to HISTFILE. Option INC_APPEND_HISTORY_TIME changes this behavior: right after you have executed some command, it will be written to the history file, along with the time spent to execute it. Refer to man zshoptions for more details.

Ref:

history command shows only 16 items?#

In zsh, if I use history command, only 16 items are shown, which is far less than the actual command history stored in HISTFILE. Why? According to the doc (man zshbuiltins), history command is just an alias to fc -l. The doc for fc command says:

fc -l [ -LI ] [ -nrdfEiD ] [ -t timefmt ] [ -m match ] [ old=new … ] [ first [ last ] ]

If first is not specified, it will be set to -1 (the most recent event), or to -16 if the -l flag is given. If last is not specified, it will be set to first, or to -1 if the -l flag is given.

So when we use command history, what we actually use is fc -l -16 -1, so the most recent 16 items in history are shown.

We can create an alias to change this:

# show all the history stored.
alias history="fc -l 1"

Ref:

Search history command more effectively?#

As programmers, we sometimes use a command with long options and parameters. Later on, when we want to run that command again or modify it a bit, it would be time-consuming to type it all over. I know that we can use Ctrl-R to search the command history. However, its features are limited.

I find fzf works better for this task, which supports fuzzy-search your command history. To install fzf, run the following command:

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
# say yes to its install prompts
~/.fzf/install

Restart the shell, you should get a far better experience searching your history commands (the shortcut is still Ctrl-R). To navigate the search results, use Ctrl-N and Ctrl-P.

Replace part of last command and re-execute#

In Bash, we can use fc -s old=new to replace string old in the last command executed to new and execute it again. Bash built-in fc is different from zsh fc.

For fc in zsh, there is no -s option. The zsh equivalent is fc -e -. -e options can be found in man zshbuiltins.

Zsh also provides r command which is same as fc -e -. You do not need to define an alias for that any more.

Ref: