1.檢查主題是否異常,嘗試更換主題解決終端顏色問題;2.若主題無異常,則修改終端配置文件中的colorscheme屬性調(diào)整配色方案;3.通過設(shè)置terminal.integrated.colors自定義終端顏色;4.在settings.json中配置workbench.colorcustomizations和editor.tokencolorcustomizations自定義主題配色;5.確保files.encoding設(shè)為utf8并配置終端編碼解決亂碼問題;6.嘗試更換終端字體以排除字體兼容性問題。通過上述步驟可逐步排查并解決vscode終端字體顏色異常及亂碼問題。
解決vscode終端字體顏色異常,主要通過調(diào)整主題配色和終端配置來搞定。有時候,一個不小心,終端就抽風(fēng)了,顏色亂七八糟,看著就難受。
解決方案
首先,檢查你的VSCode主題。換個主題試試,看看是不是主題本身的問題。有時候,某些主題對終端的顏色支持不太好,或者跟你的系統(tǒng)環(huán)境有沖突。如果換了主題就好了,那問題就簡單了,以后就用這個新主題,或者找找看有沒有針對你之前主題的終端顏色補丁。
如果換主題沒用,那就要深入到終端配置里去看看了。VSCode的終端實際上是調(diào)用你系統(tǒng)自帶的終端,所以終端的顏色設(shè)置會受到系統(tǒng)環(huán)境的影響。打開VSCode的設(shè)置,搜索“terminal.integrated.profiles.windows”,(如果你用的是Mac或者linux,就把windows改成相應(yīng)的系統(tǒng)名稱),這里可以看到VSCode使用的終端配置。
在settings.json里,你可以找到類似這樣的配置:
"terminal.integrated.profiles.windows": { "PowerShell": { "source": "PowerShell", "icon": "terminal-powershell" }, "Command Prompt": { "path": [ "${env:windir}Sysnativecmd.exe", "${env:windir}System32cmd.exe" ], "icon": "terminal-cmd" }, "git bash": { "source": "Git Bash" } }, "terminal.integrated.defaultProfile.windows": "PowerShell"
這里定義了不同的終端類型和它們的配置。你可以針對你使用的終端類型,修改它的顏色方案。比如,如果你用的是PowerShell,你可以這樣修改:
"terminal.integrated.profiles.windows": { "PowerShell": { "source": "PowerShell", "icon": "terminal-powershell", "colorScheme": "Dracula" // 或者其他的顏色方案 } },
colorScheme可以指定一個顏色方案。VSCode內(nèi)置了一些顏色方案,你也可以自己定義。要自定義顏色方案,可以搜索“terminal.integrated.colors”,這里可以配置終端的各種顏色,比如foreground(前景色)、background(背景色)、ansiBlack(黑色)等等。
"terminal.integrated.colors": { "terminal.background": "#282a36", "terminal.foreground": "#f8f8f2", "terminal.ansiBlack": "#21222c", "terminal.ansired": "#ff5555", "terminal.ansiGreen": "#50fa7b", "terminal.ansiYellow": "#f1fa8c", "terminal.ansiBlue": "#6272a4", "terminal.ansiMagenta": "#ff79c6", "terminal.ansiCyan": "#8be9fd", "terminal.ansiWhite": "#f8f8f2", "terminal.ansiBrightBlack": "#6272a4", "terminal.ansiBrightRed": "#ff6e6e", "terminal.ansiBrightGreen": "#69ff94", "terminal.ansiBrightYellow": "#ffffb5", "terminal.ansiBrightBlue": "#d6acff", "terminal.ansiBrightMagenta": "#ff92df", "terminal.ansiBrightCyan": "#a4ffff", "terminal.ansiBrightWhite": "#ffffff" }
改完之后,重啟VSCode,看看終端顏色是不是正常了。
如何找到適合自己的VSCode主題?
這其實是個很主觀的問題。首先,明確你喜歡什么風(fēng)格的顏色。是喜歡深色系的,還是淺色系的?是喜歡對比度高的,還是柔和的?可以去VSCode Marketplace上搜索,有很多主題可以預(yù)覽??梢园凑赵u分、下載量等排序,看看大家都在用什么。
另外,可以關(guān)注一些前端大佬或者技術(shù)社區(qū),看看他們推薦的主題。他們通常會分享一些自己覺得好用的主題,而且會給出一些使用心得。
還有一點,主題這東西,用久了會膩的??梢远ㄆ趽Q換主題,保持新鮮感。
VSCode主題配色方案如何自定義?
自定義主題配色方案,主要通過修改VSCode的settings.json文件來實現(xiàn)。VSCode允許你覆蓋主題的顏色,從而定制出你自己的配色方案。
首先,打開settings.json文件??梢酝ㄟ^File -> Preferences -> Settings打開設(shè)置界面,然后在搜索框里輸入settings.json,找到Edit in settings.json鏈接,點擊就可以打開這個文件了。
然后,在settings.json里,可以添加workbench.colorCustomizations和editor.tokenColorCustomizations這兩個配置項。
workbench.colorCustomizations用于修改VSCode界面的顏色,比如側(cè)邊欄、狀態(tài)欄、標(biāo)題欄等等。
"workbench.colorCustomizations": { "activityBar.background": "#282a36", "statusBar.background": "#44475a", "titleBar.activeBackground": "#44475a" }
editor.tokenColorCustomizations用于修改代碼的顏色,比如關(guān)鍵字、變量、注釋等等。這個配置項比較復(fù)雜,需要了解TextMate語法。
"editor.tokenColorCustomizations": { "comments": "#6272a4", "keywords": "#ff79c6", "strings": "#f1fa8c", "variables": "#8be9fd" }
你可以參考一些現(xiàn)有的主題的配色方案,然后根據(jù)自己的喜好進行修改。修改完之后,保存settings.json文件,VSCode會自動應(yīng)用新的配色方案。
VSCode終端亂碼如何解決?
終端亂碼通常是編碼問題導(dǎo)致的。首先,確認(rèn)你的VSCode的編碼設(shè)置是否正確。打開settings.json文件,搜索files.encoding,確保它的值是utf8。
"files.encoding": "utf8"
如果files.encoding已經(jīng)是utf8了,那就要檢查你的終端的編碼設(shè)置。不同的終端有不同的設(shè)置方法。
對于PowerShell,可以在終端里輸入$PSDefaultParameterValues[“Out-File:Encoding”] = “utf8″來設(shè)置編碼。也可以把這行代碼添加到你的PowerShell配置文件里,讓它每次啟動都自動設(shè)置編碼。PowerShell配置文件的路徑通常是$PROFILE。
對于Git Bash,可以在~/.bashrc文件里添加export LANG=”zh_CN.UTF-8″和export LC_ALL=”zh_CN.UTF-8″來設(shè)置編碼。
設(shè)置完之后,重啟VSCode,看看終端亂碼是不是解決了。
另外,有時候亂碼也可能是字體問題導(dǎo)致的。可以嘗試更換終端字體,看看是不是字體不支持某些字符。在settings.json里,可以搜索terminal.integrated.fontFamily來設(shè)置終端字體。