DEV Community

Discussion on: Bash: Edit command in your editor and execute directly

Collapse
 
taikedz profile image
Tai Kedzierski

Note that for this to work, there must be a default editor set. Normally this is pre-setup by most distros and systems - but! This generally defaults to Nano.

You can override this by setting the EDITOR environment variable (and for good measure, the VISUAL variable too.

This will do the trick (and not repeat itself if it's already done !)

grep -P '^(VISUAL|EDITOR)=' "$HOME/.basrhc" &>/dev/null && cat EOF >> "$HOME/.bashrc"
export EDITOR="$(which vim)"
export VISUAL="$EDITOR"
EOF
Enter fullscreen mode Exit fullscreen mode

It checks whether VISUAL or EDITOR are set, and if neither is set, adds the vim editor as default editor. You can replace this with any other command line text editor you care for - normally one of vim emacs or nano. If you know, you know ;-)