diff --git a/functions/_fzf_search_history.fish b/functions/_fzf_search_history.fish index 8b9adfd1..5adfa947 100644 --- a/functions/_fzf_search_history.fish +++ b/functions/_fzf_search_history.fish @@ -10,6 +10,10 @@ function _fzf_search_history --description "Search command history. Replace the set -f fzf_history_time_format "%m-%d %H:%M:%S" end + # Delinate time from command in history entries using the vertical box drawing char (U+2502). + # Then, to get raw command from history entries, delete everything up to it. The ? on regex is + # necessary to make regex non-greedy so it won't match into commands containing the char. + set -f time_prefix_regex '^.*? │ ' # Delinate commands throughout pipeline using null rather than newlines because commands can be multi-line set -f commands_selected ( builtin history --null --show-time="$fzf_history_time_format │ " | @@ -19,12 +23,12 @@ function _fzf_search_history --description "Search command history. Replace the --scheme=history \ --prompt="Search History> " \ --query=(commandline) \ - --preview="echo -- {} | string replace --regex '^.*? │ ' '' | fish_indent --ansi" \ + --preview="string replace --regex $time_prefix_regex '' {} | fish_indent --ansi" \ --preview-window="bottom:3:wrap" \ $fzf_history_opts | string split0 | # remove timestamps from commands selected - string replace --regex '^.*? │ ' '' + string replace --regex $time_prefix_regex '' ) if test $status -eq 0 diff --git a/tests/search_history/cmd_with_box_char.fish b/tests/search_history/cmd_with_box_char.fish new file mode 100644 index 00000000..9986ee78 --- /dev/null +++ b/tests/search_history/cmd_with_box_char.fish @@ -0,0 +1,14 @@ +set fish_history test +set history_file_path ~/.local/share/fish/test_history +printf "%s" "- cmd: echo 'lots of │ chars + when: 1612201487" >$history_file_path + +mock commandline "--replace --" "echo \$argv" +mock commandline \* "" +set --export --append FZF_DEFAULT_OPTS --select-1 + +set actual (_fzf_search_history) +set expected "echo 'lots of │ chars" +@test "doesn't erase commands containing box drawing char delineator" "$actual" = "$expected" + +rm $history_file_path diff --git a/tests/search_history/multi_line_cmd.fish b/tests/search_history/multi_line_cmd.fish index 0f898d35..97d3cdf4 100644 --- a/tests/search_history/multi_line_cmd.fish +++ b/tests/search_history/multi_line_cmd.fish @@ -1,4 +1,5 @@ # force history to read from a file with pre-populated history +# https://fishshell.com/docs/current/cmds/history.html#customizing-the-name-of-the-history-file set fish_history test set history_file_path ~/.local/share/fish/test_history printf "%s" "- cmd: z fzf