Stoof
Stoof

Reputation: 747

Opening default text editor in Bash?

I was writing a shell script and ran into a problem: Is there a way to open a file using the user's specified text editor?

Upvotes: 32

Views: 26493

Answers (3)

JimmyLandStudios
JimmyLandStudios

Reputation: 447

note: xdg-open file.xml will open in a Web-Browser, most likely. So, try;

    # select your default sensible-editor from all installed editors, or not.
    select-editor
    # Open Default Text Editor
    sensible-editor file.xml

Upvotes: 4

Nicholas Wilson
Nicholas Wilson

Reputation: 9685

Ignacio's right (though arguably, the fallback should be ed, which POSIX requires to be present, although it's essentially only useful to old-timers).

If you're thinking about graphical editors, xdg-open file.txt is what you're after.

Upvotes: 10

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

The user's chosen editor should be in $EDITOR, but you must still choose a sane default.

"${EDITOR:-vi}" file.txt

Upvotes: 35

Related Questions