diff options
author | Josh Rahm <rahm@google.com> | 2023-01-25 20:30:33 +0000 |
---|---|---|
committer | Josh Rahm <rahm@google.com> | 2023-01-25 20:30:33 +0000 |
commit | f381d24007454b22a1c7d4ca5eb5b9f2ce04d50d (patch) | |
tree | 584cdeedd59ea254f8d90950dc8deb477931a291 | |
parent | f7817f2c8de57df46f60220490a42219548c98be (diff) | |
download | zshrcd-f381d24007454b22a1c7d4ca5eb5b9f2ce04d50d.tar.gz zshrcd-f381d24007454b22a1c7d4ca5eb5b9f2ce04d50d.tar.bz2 zshrcd-f381d24007454b22a1c7d4ca5eb5b9f2ce04d50d.zip |
Add more bindkeys for better integration with neovim
-rw-r--r-- | 98-bindkeys.zsh | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/98-bindkeys.zsh b/98-bindkeys.zsh index 066a70b..0d63dce 100644 --- a/98-bindkeys.zsh +++ b/98-bindkeys.zsh @@ -15,3 +15,70 @@ bindkey -v '' backward-delete-char autoload edit-command-line zle -N edit-command-line bindkey '' edit-command-line + +nvim-feed-keys() { + keys=${1/\'/\\\'} + keys=${keys/\\/\\\\} + python3 - <<END +import neovim, os +try: + nvim_socket = os.environ["NVIM"] + nvim = neovim.attach('socket', path=nvim_socket) + nvim.feedkeys('$keys') +except KeyError: + pass; +END +} + +# This allows a ctrl-w motion with neovim, so one does not have do a terminal +# escape to switch panes. +nvim-move-pane() { + read -sk key + nvim-feed-keys "$key" +} + +# Does anyone actually use the execute prompt in zsh? Rebind to go directly to +# the neovim command prompt. +nvim-cmd-mode() { + if [[ -z "$NVIM" ]] ; then + # fallback to original behavior. + zle execute-named-cmd + else + nvim-feed-keys ":" + fi +} + +quote-escape() { + sed "s/'/'\"'\"'/g" <<< "$1" +} + +expand-last-file() { + local before_cursor=$LBUFFER + local after_cursor=$RBUFFER + + local after_cursor_first_word=${after_cursor/ *} + local after_cursor_remaining=${after_cursor[${#after_cursor_first_word}+1,-1]} + + local before_cursor_last_word=${before_cursor//* } + local before_cursor_remaining=${before_cursor[0, -${#before_cursor_last_word}-1]} + + local file="${before_cursor_last_word}${after_cursor_first_word}" + + if [ -f "${file}" ] ; then + local catted_file="$(cat ${file})" + catted_file="'$(quote-escape "${catted_file}")'" + + local new_l_buffer="${before_cursor_remaining}${catted_file}" + + BUFFER="${new_l_buffer}${after_cursor_remaining}" + CURSOR=$#new_l_buffer + fi +} + +zle -N nvim-move-pane +zle -N nvim-cmd-mode +zle -N expand-last-file + +bindkey -M vicmd '' nvim-move-pane +bindkey -M vicmd ':' nvim-cmd-mode +bindkey -M viins '' expand-last-file |