Browse Source

zsh_paste

raylu 9 years ago
parent
commit
17ca3440da
2 changed files with 59 additions and 3 deletions
  1. 54 0
      zsh_paste
  2. 5 3
      zshrc

+ 54 - 0
zsh_paste

@@ -0,0 +1,54 @@
+# Code from Mikael Magnusson: http://www.zsh.org/mla/users/2011/msg00367.html
+#
+# Requires xterm, urxvt, iTerm2 or any other terminal that supports bracketed
+# paste mode as documented: http://www.xfree86.org/current/ctlseqs.html
+
+# 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 zle-line-init _zle_line_init
+zle -N zle-line-finish _zle_line_finish
+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 || $TERM = xterm-256color || $TERM = screen || $TERM = screen-256color ]] && 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 || $TERM = xterm-256color || $TERM = screen || $TERM = screen-256color ]] && printf '\e[?2004l'
+}
+

+ 5 - 3
zshrc

@@ -11,7 +11,7 @@ zstyle ':completion:*' cache-path ~/.zshcache
 __git_files () {
 	_wanted files expl 'local files' _files
 }
-zstyle :compinstall filename '/home/raylu/.zshrc'
+zstyle :compinstall filename "$HOME/.zshrc"
 
 HISTFILE=~/.histfile
 HISTSIZE=10000
@@ -66,7 +66,9 @@ RPROMPT+="%{$reset_color%}"
 autoload -U url-quote-magic
 zle -N self-insert url-quote-magic
 
-export EDITOR=vim
+source $HOME/.zsh_paste
+
+export EDITOR=vi
 export MANPAGER="/bin/sh -c \"col -bx | vim -c 'set ft=man' -\""
 export GREP_COLORS='ms=01;36:mc=01;31:sl=:cx=:fn=35:ln=33:bn=33:se=01;34'
 
@@ -95,7 +97,7 @@ alias tree='tree -AvL 10'
 alias vi='vi -p'
 alias vil='vi *(.om[1])'
 alias dt='gnome-terminal --hide-menubar'
-alias sudovi='sudo vi -N -u /home/raylu/.vimrc'
+alias sudovi="sudo vi -N -u $HOME/.vimrc"
 function cl() {
 	if [ $# = 0 ]; then
 		cd && ls