且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Linux中打开默认的文本编辑器?

更新时间:2023-01-28 14:47:51

Linux上没有完全可靠的默认编辑器概念,更不用说更广泛的类Unix系统了。



传统上,用户会将环境变量 EDITOR 设置为其选择的编辑器的路径。如果设置了此变量,我想您可以有把握地相信他们会知道如何使用它,即使它们以 nano 之类的可怕东西结束。 p>

稍稍较新的约定是将 VISUAL 设置为首选的可视编辑器-我猜术语是来自 vi ed 之类的行编辑器形成对比。

  $ {VISUAL-$ {EDITOR-nano}}路径/至/new/file.txt 

在Debianish系统上,系统默认编辑器可通过替代版本进行配置,并且只需通过命令 editor 即可使用>。



在XDG系统上,当然,您可以简单地

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

不用说,这仅在具有XDG的情况下有效,即在实践中具有活动图形会话的Linux(或现代* BSD)平台(不包括Mac和XDG g)



顺便说一句,如果我能大致猜出您的脚本的作用,它可能可以简化为一个非常简单的 sed 脚本。请记住, sed 可以(几乎)完成 grep tail 的所有操作>可以。也许还会看到组合两个sed命令-这是一种快速而肮脏的重构。

  cd / usr / share / applications 
$(sed -n s:^ Exec = \([^%] * \)\(%。\(。* \)\)*:\1\3:p $(sed -n s:^ $ 1 = :: p默认值。尾巴-1) |尾巴-1)&

但是,通过快速谷歌搜索,它看起来像 / usr / share / applications /defaults.list 特定于OpenDesktop环境;但它是系统范围内的默认默认设置-管理员可能已在其他位置安装了替代项,并且个别用户可能还具有各自的首选项。查找并遍历此层次结构正是 xdg-open 的工作,因此,我不会尝试在我自己的临时脚本中重新实现它



您的问题与图形环境无关,因此尚不清楚您是否正在为那些几乎不知道如何使用的初学者寻找简单的编辑器单击并在图形化环境中流口水(在这种情况下,我想说的是 touch 然后是 xdg-open )或称职的程序员编辑器在窗口中运行的方式(或不运行)(也许尝试 VISUAL 并回退到 EDITOR ,并记录您使用了这种机制)。


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.

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