Confused about the "history" command

I start a terminal (in xfce), type history, and get a list of previous commands numbered from 462 to 481, so 20 previous commands.
But when i hit cursor up i get many many more.
History does not show these and neither can i use “history | grep” to get them.

The history manpage is not vibing with me either :-/.

What am i doing wrong?

Thanks!

Can’t you just open .bash_history or .zhistory,or similar in your home directory(depending on your terminal) with any text editor to view?

history
will list the whole history, what you get to see are the last lines, the most recent lines
as many as will fit in your terminal window
The rest of it will have scrolled by already …

Can’t confirm that one. :man_shrugging:

If you want to search the history, or if you want to scroll through all of it, you can either use
history | grep some_search_term
or
history | less
and then, through the functionality of less,
scroll through it or
search all of it (hit / to activate the search and type what you are looking for)

Yes .bash_history seesm to have everything. Thanks!

That’s why i was asking, history does not show everything.
There is nothing to scroll and much more room available.
It shows only the last 20 commands.

history is a shell builtin command. In zsh the default is the most recent history entries; you can use history 1 to set the start point to the earliest available, and you then get them all.

In bash the builtin command defaults to showing more.

2 Likes

The history command in zsh may work differently - I don’t know.
In bash it works as I described.

1 Like

Oh ok it’s the switch to zsh. Thank you!

This one seems to work fine.

alias history-grep=‘history 1| grep’

Add it to your bash or .zshrc,

and history-grep <YourSearchTerm> In the terminal should do it.

An alternative way to search the shell history (and my personal favourite) is to add
set -o vi
in ~/.zshrc or another shell’s profile file.
Then you can navigate through your history just like navigating through vim. So you hit <esc> just like you would to switch to command mode in vim, and you can then use k/j for up/down or hit / the type what you want to search for. Then if necessary you can edit a previous command just like you’d edit a line in vim, including using h/l for left/right.
Very intuitive if you’re already used to vim.

And completely unintuitive if you’re not. :smile:

4 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.