centos7 應用筆記:修改文件默認關聯的應用程序

centos7 應用筆記:修改文件默認關聯的應用程序

plain text 格式 和 markdown 格式,默認是用 gedit 打開。
本文檔會修改它們都使用 sublime 打開。

在修改 默認關聯的應用程序之前,注意:

  1. 先安裝好 sublime。
  2. 再安裝好 alacarte。
  3. 使用 alacarte 編輯 Applications 菜單。添加一個 sublime 的菜單項。

mime類型文件

mime類型是由一個 xml 格式的文件來描述的。
mime類型文件存在於以下的兩個路徑:

  1. /usr/share/mime
  2. ~/.local/share/mime

mime類型文件例子:

# plain text
/usr/share/mime/text/plain.xml

# markdown
/usr/share/mime/text/markdown.xml

查看文件的mime類型

# plain text
xdg-mime query filetype file.txt
>	text/plain

# markdown
xdg-mime query filetype file.md
>	text/markdown

應用程序desktop文件

desktop文件 用於描述一個應用程序

desktop文件 存在於以下的兩個路徑:

  1. /usr/share/applications
  2. ~/.local/share/applications

desktop文件文件例子:

# gedit
#   我的機器上使用了 wine,
#   所以 gedit 被 wine-extension-txt.desktop 接管
cat ~/.local/share/applications/wine-extension-txt.desktop
>	[Desktop Entry]
>	Type=Application
>	Name=notepad
>	MimeType=text/plain;
>	Exec=env WINEPREFIX="/home/andrew/.wine" wine start /ProgIDOpen txtfile %f
>	NoDisplay=true
>	StartupNotify=true
>	Icon=1E64_notepad.0

# sublime_text
#   這個一個 `desktop文件` 是通過 alacarte 添加的。
#   所以名字爲 alacarte-made.desktop。
#   alacarte 是一個可以編輯 Applications 菜單的軟件
#   可以通過 `yum -y install alacarte` 來安裝。
cat ~/.local/share/applications/alacarte-made.desktop
>	[Desktop Entry]
>	Comment=sublime
>	Terminal=false
>	Name=sublime
>	Exec=/opt/sublime_text/sublime_text
>	Type=Application
>	Icon=/opt/sublime_text/Icon/256x256/sublime-text.png

查看mime類型應用程序默認關聯:

使用上一步得出的mime類型,查看默認關聯的應用程序

# plain text 格式的文件 默認是用 gedit 打開的。
xdg-mime query default text/plain
>	wine-extension-txt.desktop

# markdown 格式的文件 默認是用 gedit 打開的。
xdg-mime query default text/markdown
>	# <not available>

應用程序默認關聯文件

mimeapps.list 用於描述mime類型默認關聯的應用程序

desktop文件 存在於以下的兩個路徑:

  1. /usr/share/applications/mimeapps.list
  2. ~/.local/share/applications/mimeapps.list

修改mime類型應用程序默認關聯:

修改 plain text 格式 和 markdown 格式,默認用 sublime 打開。
指令如下:

# 修改 plain text 格式的文件 默認是用 sublime 打開。
xdg-mime default alacarte-made.desktop text/plain

# 修改 markdown 格式的文件 默認是用 sublime 打開。
xdg-mime default alacarte-made.desktop text/markdown

查詢一下 mimeapps.list。確認已經修改成功。

cat ~/.local/share/applications/mimeapps.list
>	[Default Applications]
>	text/plain=alacarte-made.desktop
>	text/markdown=alacarte-made.desktop

使用 xdg-open 打開文件

xdg-open 會以默認關聯的應用程序打開文件。

xdg-open file.md

ref

Linux 修改默認文件關聯打開程序

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章