Score:1

Formatting history command using \t instead of space as separator

ca flag

Is it possible to format history command output using tab separator instead of space between fields?

I know the variable $HISTTIMEFORMAT is there another variable to format the output?

For example:

command_number{\t}date_time{\t}command{\n}

It doesn't have to be a specific shell.

cn flag
does not apply for fish: fish history just prints the command. fish does not have history expansion, so `!<number>` does not apply
Marlon Richert avatar
km flag
`$HISTTIMEFORMAT` is unique to Bash. It does not exist in other shells.
HuHa avatar
es flag
Check if `history | column -t` is the output you are looking for (you might have to install package `bsdmainutils`), and if yes, define a shell alias or function that does that.
Artur Meinild avatar
vn flag
@HuHa it's not, since this command replaces the space in each command with a tab also.
cn flag
Why would you need that? A split to separate id from command seems the more obvious method.
Score:0
vn flag

Without knowing your exact purpose, here is a way to remove leading whitespaces, and also replace the default "double space" (separating the columns with ID and Command) with a \t instead:

history | sed 's/^[ \t]*//;s/ \+ /\t/'

The sed expression is 2-part:

  • s/^[ \t]*//: This part removes any leading spaces or tabs (if you for some reason want to start each line with a \t, do this instead: s/^[ \t]*/\t/).
  • s/ \+ /\t/: This part replaces 2 or more spaces with a \t.

The order is important, since you want to first remove any leading whitespaces, and afterwards replace any 2 or more spaces with \t. If you just run the 2nd part, you also risk getting \t at the beginning of lines with smaller ID numbers.

With this, you hopefully have some building blocks to continue customizing your command. If you also want to add timestamp to the history, it gets a little more complicated, since there are additional single spaces separating the columns.

A little inspiration came from this post.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.