zsh-users
 help / color / mirror / code / Atom feed
From: "Jérémie Roquet" <jroquet@arkanosis.net>
To: Mikael Magnusson <mikachu@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: bracketed paste mode in xterm and urxvt
Date: Thu, 28 May 2015 13:42:05 +0200	[thread overview]
Message-ID: <CAFOazAOfk=Sq-smkMGzJKO4b7jMb_1_m4vXXn8twoVA2wV55YA@mail.gmail.com> (raw)
In-Reply-To: <BANLkTikh_-+L2W5=Yfu7h7iAe5CcpP6fxw@mail.gmail.com>

Hi everyone,

I've kept this mail in my inbox for almost exactly four years (!), as
a cool thing to look at when I'd have time. Obvious, I haven't had
that much time since then, and I've only tested it today… What a
mistake! This is with no doubt one of the most useful additions I've
done to my config in years.

Thanks Mikael!

PS: below is the full quote of the original mail, which could be
useful for people that weren't on this list four years ago. Or for
people like me who didn't give it the attention it deserves.
PS2: I've added screen-256color to my list of supported terminals,
though technically I'm not sure it supports bracketed paste (and I'm
running tmux anyway).

---------- Forwarded message ----------
From: Mikael Magnusson <mikachu@gmail.com>
Date: 2011-05-22 21:16 GMT+02:00
Subject: bracketed paste mode in xterm and urxvt
To: Zsh Users <zsh-users@zsh.org>

# create a new keymap to use while pasting
bindkey -N paste
# make everything in this keymap call our custom widget
bindkey -R -M paste "^@"-"\M-^?" paste-insert
# these are the codes sent around the pasted text in bracketed
# paste mode.
# do the first one with both -M viins and -M vicmd in vi mode
bindkey '^[[200~' _start_paste
bindkey -M paste '^[[201~' _end_paste
# insert newlines rather than carriage returns when pasting newlines
bindkey -M paste -s '^M' '^J'

zle -N _start_paste
zle -N _end_paste
zle -N paste-insert _paste_insert

# switch the active keymap to paste mode
function _start_paste() {
  bindkey -A paste main
}

# go back to our normal keymap, and insert all the pasted text in the
# command line. this has the nice effect of making the whole paste be
# a single undo/redo event.
function _end_paste() {
#use bindkey -v here with vi mode probably. maybe you want to track
#if you were in ins or cmd mode and restore the right one.
  bindkey -e
  LBUFFER+=$_paste_content
  unset _paste_content
}

function _paste_insert() {
  _paste_content+=$KEYS
}

function _zle_line_init() {
  # Tell terminal to send escape codes around pastes.
  [[ $TERM == rxvt-unicode || $TERM == xterm ]] && printf '\e[?2004h'
}

function _zle_line_finish() {
  # Tell it to stop when we leave zle, so pasting in other programs
  # doesn't get the ^[[200~ codes around the pasted text.
  [[ $TERM == rxvt-unicode || $TERM == xterm ]] && printf '\e[?2004l'
}

Alternatively, you can also do stuff to the text before inserting it,
I have this additional stuff which lets me toggle a mode where all the
pasted text is automatically quoted and a space is appended, which is
useful when pasting (some) urls with ? and & and what have you.

function _end_paste() {
  bindkey -e
  if [[ $_SPACE_AFTER_PASTE_QUOTE = 1 ]]; then
    LBUFFER+=${(q)_paste_content}' '
  else
    LBUFFER+=$_paste_content
  fi
  unset _paste_content
}

function _spaceafterpastequote() {
  if [[ $_SPACE_AFTER_PASTE_QUOTE = 1 ]]; then
    _SPACE_AFTER_PASTE_QUOTE=0
    zle -M "Not inserting a space after pastes, not quoting"
  else
    _SPACE_AFTER_PASTE_QUOTE=1
    zle -M "Inserting a space after pastes and quoting"
  fi
}
zle -N _spaceafterpastequote
# this is a custom wrapper that uses zkbd stuff, just use regular bindkey.
zbindkey Control-Insert _spaceafterpastequote


  reply	other threads:[~2015-05-28 11:42 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-22 19:16 Mikael Magnusson
2015-05-28 11:42 ` Jérémie Roquet [this message]
2015-05-28 19:16   ` Yuri D'Elia
2015-05-28 20:30   ` Daniel Hahler
2015-05-28 22:30     ` Bart Schaefer
2015-05-29 10:00     ` Yuri D'Elia
2015-06-03  8:35       ` Jesper Nygårds
2015-06-03  8:48         ` toki clover
2015-06-03 10:04         ` Yuri D'Elia
2015-06-03 10:14           ` Jesper Nygårds
2015-06-03 12:06           ` Mikael Magnusson
2015-06-03 14:01             ` Yuri D'Elia
2015-06-06  8:51               ` toki clover

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFOazAOfk=Sq-smkMGzJKO4b7jMb_1_m4vXXn8twoVA2wV55YA@mail.gmail.com' \
    --to=jroquet@arkanosis.net \
    --cc=mikachu@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).