11

I'm a bash user, starting a new job at a place where people use fish shell.

I'm looking at the history command which I often use in bash. When I use it in fish I get a long list of my history which I can scroll up and down on with the arrow keys.

There are no numbers like in bash and pressing enter is the same as the down key.

How can I run a past command with fish shell's history?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Philip Kirkbride
  • 9,816
  • 25
  • 95
  • 167

2 Answers2

10

The history command in the fish shell isn't bash-compatible, it's just displaying it in a pager (e.g. less).

To select an old command, you'll probably want to enter the part you remember right into the commandline, press up-arrow until you have found what you want and then press enter to execute.

E.g. on my system I enter mes, press up and rm -I meson.build appears (with the "mes" part highlighted). I then press enter and it executes.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
faho
  • 1,136
  • 1
  • 7
  • 12
  • But what if you don't remember the command, but read your history and find it out? E.g. “whichever command I ran immediately after that one” or “that command contained the name of a Chinese emperor but I don't remember which one”. – Gilles 'SO- stop being evil' Apr 18 '17 at 23:25
  • Thanks faho and yes @gilles another big downside is that you can't really use a double grep. – Philip Kirkbride Apr 18 '17 at 23:41
  • 1
    You can search the history with the usual pager command (usually '/'), or pipe it through `grep` or other commands. Like git, or systemctl, the pager is only used if the output is directly to a TTY. – Zanchey Apr 19 '17 at 00:30
0

It seems like this is by design, but you can mimic other shell's behavior with a pipe into the nl command (or setup an alias like alias history="history | nl"

However, if you were looking for numbers in order to do history expansion (ex. !123 to re-run command 123 in list), it seems that fish does not support this, but you may find some workarounds here: https://superuser.com/questions/719531/what-is-the-equivalent-of-bashs-and-in-the-fish-shell

crobicha
  • 133
  • 4