Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Bracketed paste for zsh 4.3.x up to 5.0.8



A few ways to do this have been posted, starting back at workers/29898.
The most straightforward is probably this (condensed from Oliver and
Mikael's solutions in 29898 and 29899):

  typeset -g main_keymap=emacs # or viins as you prefer
  # Create keymap where all key strokes are self-insert-unmeta
  bindkey -N paste
  bindkey -R -M paste "^@"-"\M-^?" .self-insert-unmeta
  # Swap keymaps during paste
  paste-begin() { bindkey -A paste main }
  zle -N paste-begin
  paste-end() { bindkey -A ${main_keymap} main }
  zle -N paste-end
  bindkey -M paste '\e[201~' paste-end
  bindkey '\e[200~' paste-begin
  bindkey -a '\e[200~' paste-begin
  # Turn on paste mode of terminal while editing
  PROMPT+=$'%{\e[?2004h%}'
  POSTEDIT=$'\e[?2004l'"$POSTEDIT"

However, if anything goes wrong with that you're dead in the water with
a keymap that won't let you execute any commands.  So an alternate is
the following, which doesn't need to change keymaps.  I've expanded it
to support stuffing the pasted text into a parameter if the name is
passed as an argument, and to replace the region if there is one,
both of which are features of the new builtin bracketed-paste widget
coming in the next release.

    paste-end() { :; }
    zle -N paste-end
    paste-begin() {
	local bp_PASTED
	while zle .read-command; do
	    case "$REPLY" in
		(paste-end) break;;
		(*) bp_PASTED="$bp_PASTED$KEYS";;
	    esac
	done
	# This may not be necessary everywhere (fix newlines)
	eval bp_PASTED=\$\{bp_PASTED:gs/$'\r'/$'\n'\}
	if (( ARGC )); then
	    builtin typeset -g "$1"="$bp_PASTED"
	else
	    if (( REGION_ACTIVE )); then
		zle .kill-region
	    fi
	    LBUFFER="$LBUFFER$bp_PASTED"
	fi
    }
    zle -N paste-begin
    # Haven't really tested with vicmd,
    # but should work with "bindkey -a"
    bindkey '\e[200~' paste-begin
    bindkey '\e[201~' paste-end
    # Still need this too in older shells
    PROMPT+=$'%{\e[?2004h%}'
    POSTEDIT=$'\e[?2004l'"$POSTEDIT"

Hope someone finds this useful.



Messages sorted by: Reverse Date, Date, Thread, Author