Skip to content

Instantly share code, notes, and snippets.

@kelvinauta
Created July 21, 2025 22:46
Show Gist options
  • Save kelvinauta/ea6bcad65fc9cbf16513c98f90530058 to your computer and use it in GitHub Desktop.
Save kelvinauta/ea6bcad65fc9cbf16513c98f90530058 to your computer and use it in GitHub Desktop.
Adding this configuration to your zsh allows you to edit your commands with vim easily in your zsh.
# Note: you must change local "local editor=" with the actual path of your editor.sh scriptset -o vi
edit-with-vim(){
local fifo=$(mktemp -u /tmp/vimfifo.XXXX)
mkfifo "$fifo"
local editor="$HOME/.config/scripts/editor.sh $fifo"
EDITOR="$editor" zle edit-command-line
local vimfifo=$(cat "$fifo")
[[ $vimfifo -eq 0 ]] && zle accept-line
[[ $vimfifo -eq 1 ]] && zle send-break
}
zle -N edit-with-vim
bindkey -M viins '^E' edit-with-vim
# Explanation:
# We simply change the EDITOR environment variable to inject a script
# so the zsh edit-command-line widget (which already exists) will use your script instead of the one you actually have in $EDITOR
# the fifo is to communicate whether you have canceled the script or accepted it (accept/cancel)
#!/usr/bin/env bash
fifo_path="$1"
shift
# Notes:
# Use the keymaps you prefer, I used Enter to accept, Ctrl+c to cancel
# If you don't use the vim plugin Goyo it's better to remove it
# You can also use nvim and put any script you prefer
vim -c 'nnoremap <CR> :wa <Bar> qa!<CR>' -c "nnoremap <C-c> :cq!<CR>" -c ":silent! Goyo 100%x100%" "$@"
status="$?"
[ -p "$fifo_path" ] && { echo "$status" > "$fifo_path" ;} &
exit "$status"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment