realiaser icon indicating copy to clipboard operation
realiaser copied to clipboard

Prompt function does not allow for history format

Open jalanb opened this issue 12 years ago • 5 comments

On my machine the given method to extract the last command does not work:

$ echo $0
-bash
$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.
$ history -1 | cut -d ' ' -f 3-20 | realiaser
-bash: history: -1: invalid option
history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]
1

Even if I were to work around that, the cut command is too simplistic, because it does not consider $HISTTIMEFORMAT:

$ echo $HISTTIMEFORMAT
%h/%d - %H:%M:%S
$ history | tail -n 2 | head -n 1 | cut -d ' ' -f 3-20
 Dec/03 - 23:47:13echo $HISTTIMEFORMAT

OK, let's turn that off

$ unset HISTTIMEFORMAT

Now I wonder about the cut command starting at field 3:

$ history | tail -n 2 | head -n 1 | cut -d ' ' -f 3-20
   981  unset HISTTIMEFORMAT

Those extra spaces before the command number (981) do seem to make the third field less than helpful. I need the 5th instead:

$ cd ..
$ history | tail -n 2 | head -n 1 | cut -d ' ' -f 5-20
cd ..

And that leaves the whole function as

function last_command() {
  local old_format=$HISTTIMEFORMAT
  unset HISTTIMEFORMAT
  echo $(history | head -n 2 | tail -n 1 | cut -d ' ' -f 5-20 | realiaser)
  export HISTTIMEFORMAT=$old_format
}

Which works for me, but YMMV as you may be on Linux or Solaris or ... some other variety of Unix

jalanb avatar Dec 04 '12 00:12 jalanb

Ah yes. I'm using ZSH and the method of getting history is different. I should note that in the Readme.

Nice work.

paulmars avatar Dec 04 '12 09:12 paulmars

So I'm running ZSH in iTerm2, and by default last_command() wasn't working as expected. I used the follwing:

function last_command() {
  echo $(history | tail -n 1 | cut -d ' ' -f 2-20 | realiaser)
}

kid-icarus avatar Dec 17 '12 05:12 kid-icarus

Here is the expected output.

~ ls -l 1440 [ABRIDGED] ~ history | tail -n 1 ll (1390) 37247 ls -l ~

paulmars avatar Dec 17 '12 07:12 paulmars

~ history | tail -n 1 | cut -d ' ' -f 2-20 ll (1342) ls -l

paulmars avatar Dec 17 '12 07:12 paulmars

I've found that part of the configuration to be very system dependent. I'm using ZSH / OSX. Newest version afaik.

paulmars avatar Dec 17 '12 07:12 paulmars