Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: multi-line support #25

Open
lmccartneystar opened this issue Feb 1, 2023 · 0 comments
Open

Feature: multi-line support #25

lmccartneystar opened this issue Feb 1, 2023 · 0 comments

Comments

@lmccartneystar
Copy link

lmccartneystar commented Feb 1, 2023

I managed to add support for multi-line blocks, and I want to share it.

What I did was to retrieve the number event of the selected command in fzf, and then use history expansion zle expand-or-complete with that number so zsh gets the correct block of code.

function fzf_history_search() {
  # get event number
  e_num=$(fc -l 0 | fzf --tac | awk '{print $1}')
  
  # HISTORY EXPANSION
  BUFFER="!$e_num"
  zle vi-fetch-history -n $BUFFER
  zle expand-or-complete
}

I modified the script to my needs, didn't include some stuff, but maybe you like some of what i changed, here I leave it:

# do nothing if fzf is not installed
(( ! $+commands[fzf] )) && return

# Bind for fzf history search
(( ! ${+ZSH_FZF_HISTORY_SEARCH_BIND} )) &&
typeset -g ZSH_FZF_HISTORY_SEARCH_BIND='^r'

# Cursor to end-of-line
(( ! ${+ZSH_FZF_HISTORY_SEARCH_END_OF_LINE} )) &&
typeset -g ZSH_FZF_HISTORY_SEARCH_END_OF_LINE=''

# Include event numbers
(( ! ${+ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS} )) &&
typeset -g ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS=1

# Include full date timestamps in ISO8601 `yyyy-mm-dd hh:mm' format
(( ! ${+ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH} )) &&
typeset -g ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH=1

# Remove duplicate entries in history
(( ! ${+ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES} )) &&
typeset -g ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES=''

function fzf_history_search() {
	local cmd_col=1
	local fc_args=

	# SETUP DATES
	if [ -n "${ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH}" ]; then
		cmd_col=3
		fc_args+=' -i'
	fi

	local his_out=$(fc ${=fc_args} -l 0)

	# REMOVE DUPLICATES
	if [ -n "${ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES}" ]; then
		his_out=$(printf '%s\n' "$his_out" | sort -k $((cmd_col+1)) | tac | uniq -f $((cmd_col)) | sort)
	fi

	# REMOVE NUMBER COLUMNS
	if ! [ -n "${ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS}" ]; then
		his_out=$(printf '%s\n' "$his_out" | sed 's/^[[:blank:]]*[[:digit:]]*//')
	fi

	local e_num=$(printf '%s\n' "$his_out" | fzf --tac -q "$BUFFER" | awk '{print $1}')
	if [ -n "$e_num" ]; then
		BUFFER="!$e_num"
		zle vi-fetch-history -n $BUFFER
		zle expand-or-complete
		if [ -n "$ZSH_FZF_HISTORY_SEARCH_END_OF_LINE" ]; then
			zle end-of-buffer-or-history
		fi
	fi
	#zle reset-prompt
}
autoload fzf_history_search
zle -N fzf_history_search
bindkey $ZSH_FZF_HISTORY_SEARCH_BIND fzf_history_search
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant