jog icon indicating copy to clipboard operation
jog copied to clipboard

Multi-line command support

Open DmitrySandalov opened this issue 4 years ago • 2 comments
trafficstars

$ cd
$ echo '123
456'

$ cat ~/.zsh_history_ext
$ echo '123
456'|/home/user   

$ jog
456'

jog shows just the last line of a multi-line command

DmitrySandalov avatar Nov 09 '21 13:11 DmitrySandalov

FWIW, here is my version of this trick:

function zshaddhistory() {
  if [[ $1 == $'jog\n' ]]; then return; fi
  printf '%s\t%q\n' "$PWD" "${1%%$'\n'}" >> ~/.cache/jog-history
}
function jog() {
  grep -a --color=never "^${PWD}"$'\t' ~/.cache/jog-history |
    tail | sed 's/^[^\t]+\t//' | xargs -n1 echo
}

With this version, your example produces:

❯ echo '123
… quote ❯ 456'
123
456

❯ cat ~/.cache/jog-history
/Users/jason	echo\ \'123$'\n'456\'

❯ jog
echo '123$\n456'

This makes it work almost like the built-in zsh history command (which would store it as two lines, but display it as echo '123\n456').

Other things I changed included:

  • Skip adding calls to jog to the history, rather than skip reading them from the history
  • Store the history file in ~/.cache (mostly because I keep my dotfiles in git, so putting things like this into ~/.cache means I don't need to add additional top-level gitignore entries
  • Store the directory first, followed by a tab so it works with commands that include pipes
  • Do the directory removal after the tail, rather than stripping the directory from possibly hundreds or thousands of lines and then discarding most of them.

jasonk avatar Nov 13 '21 00:11 jasonk

@jasonk I don't know if I misused something but my zsh print out the directory + \n and the command. e.g:

❯ jog
/home/me
cat .cache/jog-history

I'm using zsh with Ubuntu on wsl 2

manaclan avatar Nov 18 '21 04:11 manaclan