0

In fish, when I type history | less I see the following for example:

history | less
export HISTTIMEFORMAT="%h/%d - %H:%M:%S "
bash

in bash I see this:

  491  18/04/16 14:31:02 cd
  492  18/04/16 14:31:02 ls -l
  493  18/04/16 14:31:02 less .bashrc

so I can execute the command again with !491 for example and I also can do an audit on my server, but fish doesn't have a way to display this. I also tried to add the time and date with export HISTTIMEFORMAT="%h/%d - %H:%M:%S " but nothing, any ideas on how to add that like in bash?

2

fish tracks but does not yet surface the history timestamps. See issue #677 - there's some contributed scripts in there to parse the history file.

fish does not support exclamation mark history expansion, because the syntax is hard to remember and it's often invoked by mistake. You can follow the discussion in #288. What you normally do is to start typing the command, and accept the autosuggestion when it appears (right-arrow or ctrl-F). Another option is to type part of the command and use the up arrow to cycle through matching history items.

  • Taking advantage that you are here I have to ask you a question, Where can I write my functions and make them permanent? I went throught the fish manual and created my function in the terminal but then it gets forgotten when I close the window. – VaTo May 14 '16 at 1:00
  • 1
    You can use funcsave foo to save a function named foo. You can also edit the file ~/.config/fish/functions/foo.fish (which funcsave will create for you). See the docs regarding autoloading functions. – ridiculous_fish May 14 '16 at 4:25
1

Fish now supports timestamps in the builtin history command if you execute it with the --show-time parameter.

(You must be using at least fish version 2.4 which was released Oct 2016)

As per your original example, you can specify the format as so:

$> history --show-time='%h/%d - %H:%M:%S '

or save that into a function (not using an alias here because 1. recursive and 2. long):

# ~/.config/fish/functions/history.fish

function history
    builtin history --show-time='%h/%d - %H:%M:%S ' | tail -r
end

I add the additional pipe to tail-reverse so that it reads top-down like bash history does.

Add that to your fish functions and it should be enough for you.

___

There does not appear to be a way to print the "history index" (which "line number" a given command corresponds to). For comparison, here's my preferred bash history format:

  228  01/05/17 14:47:53 $> history
  229  01/05/17 14:48:11 $> vim .bash_profile
  230  01/05/17 14:48:42 $> source .bash_profile
  231  01/05/17 14:48:45 $> history
  232  01/05/17 15:15:29 $> ls
  233  01/05/17 15:15:30 $> clear
  234  01/05/17 15:15:32 $> history

and the corresponding fish history for the time I was writing this post:

  01/05/17 15:05:53 $> vim history.fish
  01/05/17 15:10:01 $> man history
  01/05/17 15:14:50 $> history | tail -r | less
  01/05/17 15:15:01 $> history | tail -r
  01/05/17 15:15:11 $> ls
  01/05/17 15:15:14 $> vim history.fish
  01/05/17 15:15:41 $> history

Frankly, it's close enough to be honest.

___

UPDATE: after some thought, you can keep piping the result in order to get those line numbers. It won't be perfect (multi-line commands will be counted as a new "line" for each line entered) but it gives you pretty much everything you would want out of a bash-like history for fish. Here's my final history.fish file:

function history
    builtin history --show-time='%m/%d/% %T $> ' | tail -r | less -N +G
end

less -N gives line numbers, and less +G starts the file at the bottom (so you are automatically looking at most recent entries).

tail -r is only compatible on FreeBSD-based systems (which includes Mac OS X) but you can use similar alternatives like tac (and gtac if you're using GNU coreutils).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.