Skip to content

Instantly share code, notes, and snippets.

@lo48576
Created April 25, 2026 02:28
Show Gist options
  • Select an option

  • Save lo48576/ce6f643b79e56e4938fdc3ea6b54b036 to your computer and use it in GitHub Desktop.

Select an option

Save lo48576/ce6f643b79e56e4938fdc3ea6b54b036 to your computer and use it in GitHub Desktop.
zshaddhistory (2026-04-25)
# zshrc より抜粋。
zshaddhistory() {
# Truncate trailing newline.
local line=${1%%$'\n'}
# Split command line string into words.
# `(z)`: Split string into words using shell parsing.
typeset -a quoted_cmds=( ${(z)line} )
# `(Q)`: Unquote.
typeset -a cmds=( ${(Q)${quoted_cmds}} )
# If pipe (`|`) is used, add to history.
if [[ ${cmds[(ie)|]} -le ${#cmds} ]] ; then
# Found pipe.
return 0
fi
# Ignore commands with no arguments.
if [[ ${#cmds} -eq 1 ]] ; then
case ${quoted_cmds[1][1,1]} in
'$' | '`')
# Not simple command, but maybe variable or command expansion.
return 0
;;
*)
# Simple command with no arguments. Ignore.
return 1
;;
esac
fi
# Ignore some commands.
typeset -a ignore_cmds=( cd echo ls )
if [[ ${ignore_cmds[(ie)${cmds[1]}]} -le ${#ignore_cmds} ]] ; then
# Ignore.
return 1
fi
# Ignore simple subcommands.
#
# `g`: alias to `git`.
# `cr`: alias to `cargo`.
# `cn`: alias to `cargo +nightly`.
typeset -a ignore_single_subcommand=( git g cargo cr cn )
if [[ ( ${ignore_single_subcommand[(ie)${cmds[1]}]} -le ${#ignore_single_subcommand} ) && ( ${#cmds} -le 2 ) ]] ; then
# Ignore listed commands with at most one argument.
return 1
fi
# Ignore some editor commands.
typeset -a ignore_editor_cmds=( vi vim nvim nano )
if [[ ${ignore_editor_cmds[(ie)${cmds[1]}]} -le ${#ignore_editor_cmds} ]] ; then
case ${quoted_cmds[2][1,1]} in
'~' | '`' | '=' | '$' | '/' | '+')
# Maybe editing special path (or even not a path).
#
# * `~`: absolute: path from the home directory.
# * '`': dynamic: editing command output.
# * `=`: absolute: path to the command (zsh syntax).
# * `$`: dynamic: path from environment variable.
# * `/`: absolute: absolute path.
# * `+`: special: Neovim command.
:
;;
*)
# Maybe editing normal (relative) path. Just ignore.
return 1
;;
esac
fi
return 0
}
# vim: set ft=zsh :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment