comparison .shell.d/99.safe-paste.zsh @ 351:bef29d49d19f

safe-paste: adds support for bracketed paste mode to zsh Since I don't use oh-my-zsh, just import their code directly with a reference. Based on the site where I learned about this [0], it's originally from [1], and I downloaded it from [2]. 0: https://cirw.in/blog/bracketed-paste 1: http://www.zsh.org/mla/users/2011/msg00367.html 2: https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/plugins/safe-paste/safe-paste.plugin.zsh
author Augie Fackler <raf@durin42.com>
date Mon, 10 Nov 2014 09:26:04 -0500
parents
children
comparison
equal deleted inserted replaced
350:b55a7fe4dfab 351:bef29d49d19f
1 # taken from https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/plugins/safe-paste/safe-paste.plugin.zsh
2 # Code from Mikael Magnusson: http://www.zsh.org/mla/users/2011/msg00367.html
3 #
4 # Requires xterm, urxvt, iTerm2 or any other terminal that supports bracketed
5 # paste mode as documented: http://www.xfree86.org/current/ctlseqs.html
6
7 # create a new keymap to use while pasting
8 bindkey -N paste
9 # make everything in this keymap call our custom widget
10 bindkey -R -M paste "^@"-"\M-^?" paste-insert
11 # these are the codes sent around the pasted text in bracketed
12 # paste mode.
13 # do the first one with both -M viins and -M vicmd in vi mode
14 bindkey '^[[200~' _start_paste
15 bindkey -M paste '^[[201~' _end_paste
16 # insert newlines rather than carriage returns when pasting newlines
17 bindkey -M paste -s '^M' '^J'
18
19 zle -N _start_paste
20 zle -N _end_paste
21 zle -N zle-line-init _zle_line_init
22 zle -N zle-line-finish _zle_line_finish
23 zle -N paste-insert _paste_insert
24
25 # switch the active keymap to paste mode
26 function _start_paste() {
27 bindkey -A paste main
28 }
29
30 # go back to our normal keymap, and insert all the pasted text in the
31 # command line. this has the nice effect of making the whole paste be
32 # a single undo/redo event.
33 function _end_paste() {
34 #use bindkey -v here with vi mode probably. maybe you want to track
35 #if you were in ins or cmd mode and restore the right one.
36 bindkey -e
37 LBUFFER+=$_paste_content
38 unset _paste_content
39 }
40
41 function _paste_insert() {
42 _paste_content+=$KEYS
43 }
44
45 function _zle_line_init() {
46 # Tell terminal to send escape codes around pastes.
47 [[ $TERM == rxvt-unicode || $TERM == xterm || $TERM = xterm-256color || $TERM = screen || $TERM = screen-256color ]] && printf '\e[?2004h'
48 }
49
50 function _zle_line_finish() {
51 # Tell it to stop when we leave zle, so pasting in other programs
52 # doesn't get the ^[[200~ codes around the pasted text.
53 [[ $TERM == rxvt-unicode || $TERM == xterm || $TERM = xterm-256color || $TERM = screen || $TERM = screen-256color ]] && printf '\e[?2004l'
54 }
55