How to open the default text editor in Linux?

ⅰ亾dé卋堺 提交于 2020-03-22 07:20:23

问题


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.


回答1:


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).




回答2:


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



来源:https://stackoverflow.com/questions/19252791/how-to-open-the-default-text-editor-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!