如何在Linux中打开默认的文本编辑器? [英] How to open the default text editor in Linux?

查看:832
本文介绍了如何在Linux中打开默认的文本编辑器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在没有文件的情况下在Linux中打开默认的文本编辑器。我知道我可以使用comand xdg-open在默认编辑器中打开文件,但是我需要在没有文件的情况下打开编辑器并让用户创建文件。



编辑:



我用以下脚本解决:

 #!/ bin / sh 
cd / usr / share / applications /
atalho =`grep $ 1 defaults.list |尾巴-1 | sed s:^ $ 1 = ::`
`grep‘^ Exec’$ atalho |尾巴-1 | sed’s / ^ Exec = //’| s’s /%.//’`&

在Ubuntu上工作正常,但我担心此脚本是否可以在其他Linux发行版中使用。

解决方案

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

这篇关于如何在Linux中打开默认的文本编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆