Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open the default text editor in Linux?

I need to open the default text editor in Linux without having a file. I know that I could use the comand xdg-open to open a file in the default editor but I need to open the editor without having a file and let the user create the file.

Edit:

I solve with this script:

#!/bin/sh
cd /usr/share/applications/
atalho=`grep $1 defaults.list | tail -1 | sed "s:^$1=::" `
`grep '^Exec' $atalho | tail -1 | sed 's/^Exec=//' | sed 's/%.//'` &

Works fine on Ubuntu but I'm worried if this script will work in other Linux distribuitions.

like image 799
Rodrigo Bulgarelli Avatar asked Oct 02 '22 18:10

Rodrigo Bulgarelli


2 Answers

There is no completely reliable concept of "default editor" on Linux, let alone more broadly Unix-like systems.

Traditionally, users would set the environment variable EDITOR to the path of their editor of choice. If this variable is set, I'm thinking you can be reasonably confident that they will know how to use it, even if they end up in something horrible like nano.

A slightly newer convention is to set VISUAL to the preferred "visual editor" - I guess the terminology comes from vi to contrast against line editors like ed.

${VISUAL-${EDITOR-nano}} path/to/new/file.txt

On Debianish systems, the system default editor is configurable via alternatives and available simply with the command editor.

On XDG systems, of course, you could simply

touch path/to/new/file.txt
xdg-open path/to/new/file.txt

Needless to say, this only works if you have XDG, i.e. In practice a Linux (or maybe modern *BSD) platform with an active graphical session (excludes Mac and pre-XDG graphical systems as well as of course any server environment where there is no GUI).

As an aside, if I can guess even roughly what your script does, it could probably be pared down to a fairly simple sed script. Remember, sed can do (almost) everything grep and tail can. Maybe see also Combining two sed commands - here is a quick and dirty refactoring.

cd /usr/share/applications
$(sed -n "s:^Exec=\([^%]*\)\(%.\(.*\)\)*:\1\3:p" "$(sed -n "s:^$1=::p" defaults.list | tail -1)" | tail -1) &

However, from quick googling, it looks like /usr/share/applications/defaults.list is specific to OpenDesktop environments; but it's the system-wide default default - the admin could have installed an override in a different location, and individual users probably have individual preferences on top of that. Finding and traversing this hierarchy is precisely what xdg-open does, so I'm not going to try to reimplement it in an ad-hoc script of my own, and suggest you shouldn't, either.

There is nothing about graphical environments in your question, so it's unclear whether you are actually looking for a simple editor for beginners who barely know how to click and drool in a graphical environment (in which case I'd say go with touch followed by xdg-open) or a competent programmers' editor which way or may not run in a window (maybe try VISUAL with fallback to EDITOR, and document that you use this mechanism).

like image 83
tripleee Avatar answered Oct 05 '22 22:10

tripleee


Is vi your default editor? i would do something like "vi mynewfile.txt"

like image 31
Israelm Avatar answered Oct 06 '22 00:10

Israelm