Skip to content

Instantly share code, notes, and snippets.

@tweekmonster
Created April 22, 2016 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tweekmonster/f4d52ab2069288cd115e0dcea0e47715 to your computer and use it in GitHub Desktop.
Save tweekmonster/f4d52ab2069288cd115e0dcea0e47715 to your computer and use it in GitHub Desktop.
zsh edit-command-line zle widget that sets the cursor position in Vim
function edit-command-line() {
tmpfile=$(mktemp -t zsheditXXXXXXXX.sh)
print -R - "$PREBUFFER$BUFFER" > $tmpfile
editor=${VISUAL:-${EDITOR:-vi}}
args=()
if [[ "$editor" =~ vim ]]; then
pb=${#PREBUFFER}
(( b=pb+CURSOR ))
args+=("-c" ":call cursor(byte2line($b), ($b - $pb) + 1)")
fi
args+=($tmpfile)
exec </dev/tty
$editor ${args[@]}
print -z - "$(<$tmpfile)"
command rm -f $tmpfile
zle send-break
}
bindkey '^F' edit-command-line
zle -N edit-command-line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment