Last active
December 27, 2017 15:44
-
-
Save jamietre/e031894cd689342d8ee47be132b77528 to your computer and use it in GitHub Desktop.
zsh config for windows-like inline text editing in iterm2 console in MacOS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add to .zshrc | |
r-delregion() { | |
if ((REGION_ACTIVE)) then | |
zle kill-region | |
else | |
zle $1 | |
fi | |
} | |
r-deselect() { | |
((REGION_ACTIVE = 0)) | |
zle $1 | |
} | |
r-select() { | |
((REGION_ACTIVE)) || zle set-mark-command | |
zle $1 | |
} | |
# adjust as needed - this depends on my karabener elements mapping: | |
# | |
# https://gist.github.com/jamietre/6457204045dce85476a693d21142edc3 | |
# | |
# which also maps | |
# | |
# home -> ctrl+A | |
# end -> ctrl+E | |
# cmd+right -> option+right | |
# cmd+left -> option+left | |
# | |
# you may also have to remove default keymappings in iterm2 e.g. ctrl+left, ctrl+right which usually changes tabs, | |
# we want to move cursor by word. | |
# | |
# - 'key' just must be unique | |
# - 'seq' is the key escape seqeuence | |
# To obtain 'seq' type CTRL+V and then a key combination. \e is escape, enter the remaining from console output | |
# the last column is the widgets invoked by zhs when that keycode is pressed | |
# - 'mode' constrols the action of the selected region: activates it, deactivates it, or deletes it. These are | |
# all function defined above | |
# - 'widget' is the zsh widget to use: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Movement | |
for key seq mode widget ( | |
sleft $'\e[1;2D' select backward-char | |
sright $'\e[1;2C' select forward-char | |
sup $'\e[1;2A' select up-line-or-history | |
sdown $'\e[1;2B' select down-line-or-history | |
cleft $'\e[1;9D' deselect backward-word | |
cright $'\e[1;9C' deselect forward-word | |
csleft $'\e[1;10D' select backward-word | |
csright $'\e[1;10C' select forward-word | |
end $'\EOF' deselect end-of-line | |
end2 $'^E' deselect end-of-line | |
send $'\E[1;2F' select end-of-line | |
home $'\EOH' deselect beginning-of-line | |
home2 $'^A' deselect beginning-of-line | |
shome $'\E[1;2H' select beginning-of-line | |
left $'\EOD' deselect backward-char | |
right $'\EOC' deselect forward-char | |
del $'^D' delregion delete-char | |
backsp $'^?' delregion backward-delete-char | |
) { | |
eval "key-$key() r-$mode $widget" | |
zle -N key-$key | |
bindkey "$seq" key-$key | |
} | |
# This section makes cut/copy/paste behave sanely. Because there's no direct comprehension in the terminal of | |
# the "command" key, which is my preferred usage for copy/cut/paste (Cmd+C/Cmd+X/Cmd+V) - you will need to add | |
# key bindings in iTerm2 to send escape sequences as defined below, e.g. Command+C -> esc seq "[1;cp". | |
# But this can easily be substituted with any key captures. | |
x-copy-region () { | |
zle copy-region-as-kill | |
print -rn $CUTBUFFER | pbcopy | |
} | |
zle -N x-copy-region | |
x-kill-region () { | |
zle kill-region | |
((REGION_ACTIVE = 0)) | |
print -rn $CUTBUFFER | pbcopy | |
} | |
zle -N x-kill-region | |
x-paste () { | |
if ((REGION_ACTIVE)) then | |
zle kill-region | |
((REGION_ACTIVE = 0)) | |
fi | |
CUTBUFFER=$(pbpaste </dev/null) | |
zle yank | |
} | |
zle -N x-paste | |
# requires iterm mapping in keys Command+C -> escape sequence | |
bindkey -e $'\e[1;cp' x-copy-region | |
# requires iterm mapping in keys Command+X -> escape sequence | |
bindkey -e $'\e[1;ct' x-kill-region | |
# requires iterm mapping in keys Command+V -> escape sequence | |
bindkey -e $'\e[1;ps' x-paste |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment