如何為沒有副檔名的檔案設定 nano 預設語法突出顯示?

如何為沒有副檔名的檔案設定 nano 預設語法突出顯示?

/etc/nginx/sites-available/default當我編輯沒有擴展名(例如或 )的檔案時,我試圖為 nano 設定「預設」語法突出顯示方案,/etc/ssh/sshd_config但我似乎無法使其工作。

我已將c語法突出顯示文件複製到/usr/share/nano/default.nanorc然後添加include "/usr/share/nano/deafult.nanorc"到我的~/.nanorc文件中,但我仍然沒有突出顯示沒有擴展名的文件。

有人知道如何啟用此功能嗎?

答案1

你可以自己定義這個。這是來自 Arch Linux 論壇的一個很好的例子。

複製以下程式碼並儲存在/usr/share/nano/作為奈米文件

# config file highlighting

syntax "conf" "(\.(conf|config|cfg|cnf|rc|lst|list|defs|ini|desktop|mime|types|preset|cache|seat|service|htaccess)$|(^|/)(\w*crontab|mirrorlist|group|hosts|passwd|rpc|netconfig|shadow|fstab|inittab|inputrc|protocols|sudoers)$|conf.d/|.config/)"

# default text
color magenta "^.*$"
# special values
icolor brightblue "(^|\s|=)(default|true|false|on|off|yes|no)(\s|$)"
# keys
icolor cyan "^\s*(set\s+)?[A-Z0-9_\/\.\%\@+-]+\s*([:]|\>)"
# commands
color blue "^\s*set\s+\<"
# punctuation
color blue "[.]"
# numbers
color red "(^|\s|[[/:|<>(){}=,]|\])[-+]?[0-9](\.?[0-9])*%?($|\>)"
# keys
icolor cyan "^\s*(\$if )?([A-Z0-9_\/\.\%\@+-]|\s)+="
# punctuation
color blue "/"
color brightwhite "(\]|[()<>[{},;:=])"
color brightwhite "(^|\[|\{|\:)\s*-(\s|$)"
# section headings
icolor brightyellow "^\s*(\[([A-Z0-9_\.-]|\s)+\])+\s*$"
color brightcyan "^\s*((Sub)?Section\s*(=|\>)|End(Sub)?Section\s*$)"
color brightcyan "^\s*\$(end)?if(\s|$)"
# URLs
icolor green "\b(([A-Z]+://|www[.])[A-Z0-9/:#?&$=_\.\-]+)(\b|$| )"
# XML-like tags
icolor brightcyan "</?\w+((\s*\w+\s*=)?\s*("[^"]*"|'[^']*'|!?[A-Z0-9_:/]))*(\s*/)?>"
# strings
color yellow "\"(\\.|[^"])*\"" "'(\\.|[^'])*'"
# comments
color white "#.*$"
color blue "^\s*##.*$"
color white "^;.*$"
color white start="<!--" end="-->"

然後將此配置包含在 /etc/nanorc 檔案中

## Configuration files (catch-all syntax)
include "/usr/share/nano/conf.nanorc"

程式碼片段中的第一個程式碼行包含一個正規表示式,該表達式定義應使用此語法突出顯示的檔案名稱。每當您遇到與此不匹配的配置文件,但您仍然想使用語法突出顯示打開它時,您可以使用 nano 的 -Y 開關手動選擇此語法,如下所示:

nano -Y conf myConfigFile

來源:奈米語法突出顯示:配置文件的所有語法

答案2

nanorc 檔案應該有

syntax "default"

後面沒有正規表示式。

答案3

Nano 語法使用 和 突出顯示所有文件沒有擴大:

從預設檔案建立備份

yes | cp /usr/share/nano/default.nanorc /usr/share/nano/default.nanorc.backup

從設定檔建立副本

yes | cp /usr/share/nano/sh.nanorc /usr/share/nano/default.nanorc

將語法副檔名變更為預設值

sed -i -e 's/syntax "sh" "\\.sh$"/syntax "default"/g' /usr/share/nano/default.nanorc

將所有 .nanrc 檔案儲存在包含檔案中

ls -1 /usr/share/nano/*.nanorc | sed 's/^\//include \//' >> ~/.nanorc

相關內容