From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9193 invoked by alias); 22 May 2011 19:23:44 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 16046 Received: (qmail 22192 invoked from network); 22 May 2011 19:23:42 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.212.43 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=uiygLwkVDf89v4xGSFUBF8aK333LwBvXElaxkJLmW8s=; b=ETVGmrK8DaOQuLTjUsNVGJHRoPvrRyuFoXl/IRzqIDccYlS86hqsPliZL2pg8ZbB8E w26/JmBEMa/Rks41APhcpBJXzwlTlwT+rHjuzPJWumDSmWQAe4LZi9w3DckV/PS7rTL3 B3OE9xgX9NqIB2pyjr6EQI7XZbP+CdsHNGuv4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=j0de+CYZ0UQtVTtxIbobzZiMEwGTHgaakp+fnZS3CfQiT0XaNCIaW+2niwemhW6Luw qjb1zb+zT2Hh3pDfFpt2ur5tOxtNX1uLs+7auK6QJtenKZ505eNfz/o4utGkvafuYWLu 9lxTfEBcnl1HPPOn/zmrA4fqZ2Mip8CY5lbGk= MIME-Version: 1.0 Date: Sun, 22 May 2011 21:16:36 +0200 Message-ID: Subject: bracketed paste mode in xterm and urxvt From: Mikael Magnusson To: Zsh Users Content-Type: text/plain; charset=UTF-8 # 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 -- Mikael Magnusson