Whenever I execute a command using an alias, this command is not stored in the shell's command history.
So if I run history
these commands do not appear in the list.
Nor do they appear when I press CTRL r
for reverse searching the command history.
When I press the keyboard's arrow up for scrolling through the last commands, I will see an aliased command only if it was the last command I ran. Other aliased commands are will not be displayed.
For example:
$ cd my-repo
$ gs # an alias to git status
$ history
Outputs the following:
2374 cd my-repo
(the gs command is not displayed)
A few notes:
gs is only an example. The issue is far more annoying in more complex commands since I have to retype them all over again instead of executing them from history (e.g.
k get pods | grep <pod_name>
, where k=kubectl).gs is defined so:
alias gs=' git status'
.I also have a few functions in ~/.alias, e.g.:
mkcd () { mkdir -pv $1
For some reason,
mkcd
(or any other function in the alias file) is included in the history.I do not mind if it prints out
gs
or expands togit status
, I'll take any of the two...I am using zsh with oh-my-zsh on macOS (Monterey). My shell aliases are defined in ~/.alias which is sourced in ~/.zshrc (source ~/.alias).
This happens both in iTerm2 and in the default Mac terminal.
Thank you for taking the time to help :-)
CodePudding user response:
I will assume that your example alias is exactly what you have in your ~/.alias
file.
So you have aliases like this (notice the space character in front of git
command):
alias gs=' git status'
There is an shell option called HIST_IGNORE_SPACE
which is doing exactly what you are experiencing - in short it will not add command to the history when it starts with space character. Example:
echo 'This command will make it to the history.'
echo 'This poor command will be forgotten.'
You can check your current options using setopt
or specifically:
setopt | grep 'histignorespace'
So there are two ways how you can fix this - either by fixing your aliases to not start with space or, If you really don't want this functionality at all, by unsetting it in your ~/.zshrc
like this:
unsetopt HIST_IGNORE_SPACE