将默认的 xdg-open 应用程序设置为终端程序

Eva*_*van 14 arch-linux terminal xdg-open

是否可以在 xdg-open 中使用终端 vim?

我没有 GUI 文本编辑器,因为我只通过终端使用 vim。(我也不太关心 gvim。)是否可以告诉 xdg-open 打开一个终端,然后用选定的文件打开 vim?

谢谢。

thi*_*wfx 10

在您的 .bashrc 或 .zshrc 中,根据您分别使用 bash 还是 zsh,导出这两个环境变量:

export EDITOR=vim
export VISUAL=vim
Run Code Online (Sandbox Code Playgroud)

另外,您可能希望将 vim 与文本文件的 mimetype 相关联:

xdg-mime default vim.desktop text/plain
Run Code Online (Sandbox Code Playgroud)

现在你必须在 中创建一个 vim.desktop 文件/usr/share/applications,它应该执行你想要的终端模拟器,打开 vim。


Ale*_* DC 7

我必须提出一个新答案,即使我的评论只完成了thiagowfx的答案,因为在评论中你不能缩进代码。

vim.desktop 的内容可以是这样的:

[Desktop Entry]
Name=Vim Text Editor
Comment=Edit text files
Exec=vim 
Terminal=true
Type=Application
Icon=terminal
Categories=Utility;TextEditor;
StartupNotify=true
MimeType=text/plain;
Run Code Online (Sandbox Code Playgroud)

我更喜欢把它放在~/.local/share/applications.


Jul*_*fee 5

长话短说

$ xdg-mime default vim.desktop <MIMETYPE>
Run Code Online (Sandbox Code Playgroud)

或者编辑~/.config/mimeapps.list.
MIMETYPE 的输出是$ xdg-mime query filetype <interested-file>


如果您使用某些桌面环境(KDE、GNOME、LXQT 等),则应参阅 DE 文档。但是XDG 标准用于为特定 mime 类型设置默认应用程序。Mime 类型是一种区分一种文件类型和另一种文件类型的方法,请参阅 Wikipedia 或archwiki。例如,有用于文件text/html的mime-type *.html,以及text/plain用于*.txt文件的mime-type。您可以使用 确定 mime-type $xdg-mime query filetype <file_you_interested>

Mime 类型用于将应用程序与这些应用程序应打开的文件链接起来。
打开~/.config/mimeapps.list它在我的机器上看起来像这样:

[Default Applications]
x-scheme-handler/http=firefox.desktop
...
inode/directory=org.gnome.Nautilus.desktop

[Added Associations]
application/x-shellscript=nvim-qt.desktop;
...
application/pdf=firefox.desktop;
Run Code Online (Sandbox Code Playgroud)

[Default Applications]将这些行添加到部分

text/plain=vim.desktop
text/markdown=vim.desktop
text/html=vim.desktop
Run Code Online (Sandbox Code Playgroud)

(如果需要,添加其他哑剧类型)

vim.desktop我的机器上的文件包含/usr/share/applications以下内容:

[Desktop Entry]
Name=Vim
TryExec=vim
Exec=vim %F
Terminal=true
Type=Application
Keywords=Text;editor;
Icon=gvim
Categories=Utility;TextEditor;
StartupNotify=false
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Run Code Online (Sandbox Code Playgroud)

(确实,名字和评论有很多翻译,但在这里并不重要)

看线Terminal=true

根据 XDG 桌面入口规范中的规则,此行表示启动器应打开终端模拟器,然后应在此终端窗口中打开您的应用程序(Exec行)。很好,是吗?


然而,xdg-open和 xdg-open 忽略键中存在一个五年前的错误Terminal,因为没有默认终端模拟器的规范。

他们尝试过,但 2020 年还没有规范。)

因此,如果您不使用 DE,xdg-open 不会为您遵守桌面输入规范。填充错误的人创建了一个补丁(遗憾的是,该补丁被忽略了),该补丁查看 $TERMINAL 变量并打开终端模拟器,因此您可以修补 /usr/bin/xdg-open (或$ which xdg-open)。

如果您不想xdg-open自己更改脚本,可以使用一些解决方法:

  1. 您可以使用gvim.desktop(如果gvim您的系统中有)或nvim-qt.desktop(qt front-end for neovim)。

  2. 您可以创建一个具有某种名称的文件,例如my-vim.desktop包含以下内容:

[Desktop Entry]
Type=Application
Name=MyVim
Exec=<COMMAND TO RUN YOUR TERMINAL> vim %F
Terminal=false
Icon=gvim
Categories=Utility;TextEditor
Run Code Online (Sandbox Code Playgroud)

并将其放入~/.local/share/applications.
您可以从首选终端的手册页中获取运行终端的命令(例如,gnome-terminal -e)。
更多详细信息,请参见桌面入口规范。

  1. 你可以以某种方式阅读xdg-open源代码并调整你的系统,它xdg-open会认为你正在使用 DE 并调用 DE 特定的工具,但我认为这很奇怪。

有用的链接:
XDG-mime 手册页 - https://linux.die.net/man/1/xdg-mime
关于 XDG mime 类型的 Arch wiki - https://wiki.archlinux.org/index.php/XDG_MIME_Applications
Arch wiki关于 XDG Desktop 条目 - https://wiki.archlinux.org/index.php/Desktop_entries#Application_entry
xdg-open 问题 - https://gitlab.freedesktop.org/xdg/xdg-utils/-/issues/84