Diff
1diff --git a/plugins/safe-paste/safe-paste.plugin.zsh b/plugins/safe-paste/safe-paste.plugin.zsh
2new file mode 100644
3index 0000000000000000000000000000000000000000..0aa97965fc65a15a7871273d45d10b4dacff4429
4--- /dev/null
5+++ b/plugins/safe-paste/safe-paste.plugin.zsh
6@@ -0,0 +1,54 @@
7+# Code from Mikael Magnusson: http://www.zsh.org/mla/users/2011/msg00367.html
8+#
9+# Requires xterm, urxvt, iTerm2 or any other terminal that supports bracketed
10+# paste mode as documented: http://www.xfree86.org/current/ctlseqs.html
11+
12+# create a new keymap to use while pasting
13+bindkey -N paste
14+# make everything in this keymap call our custom widget
15+bindkey -R -M paste "^@"-"\M-^?" paste-insert
16+# these are the codes sent around the pasted text in bracketed
17+# paste mode.
18+# do the first one with both -M viins and -M vicmd in vi mode
19+bindkey '^[[200~' _start_paste
20+bindkey -M paste '^[[201~' _end_paste
21+# insert newlines rather than carriage returns when pasting newlines
22+bindkey -M paste -s '^M' '^J'
23+
24+zle -N _start_paste
25+zle -N _end_paste
26+zle -N zle-line-init _zle_line_init
27+zle -N zle-line-finish _zle_line_finish
28+zle -N paste-insert _paste_insert
29+
30+# switch the active keymap to paste mode
31+function _start_paste() {
32+ bindkey -A paste main
33+}
34+
35+# go back to our normal keymap, and insert all the pasted text in the
36+# command line. this has the nice effect of making the whole paste be
37+# a single undo/redo event.
38+function _end_paste() {
39+#use bindkey -v here with vi mode probably. maybe you want to track
40+#if you were in ins or cmd mode and restore the right one.
41+ bindkey -e
42+ LBUFFER+=$_paste_content
43+ unset _paste_content
44+}
45+
46+function _paste_insert() {
47+ _paste_content+=$KEYS
48+}
49+
50+function _zle_line_init() {
51+ # Tell terminal to send escape codes around pastes.
52+ [[ $TERM == rxvt-unicode || $TERM == xterm || $TERM = xterm-256color ]] && printf '\e[?2004h'
53+}
54+
55+function _zle_line_finish() {
56+ # Tell it to stop when we leave zle, so pasting in other programs
57+ # doesn't get the ^[[200~ codes around the pasted text.
58+ [[ $TERM == rxvt-unicode || $TERM == xterm || $TERM = xterm-256color ]] && printf '\e[?2004l'
59+}
60+