MSYS2 的日常操作
來源 https://notes.shinemic.cn/daily-operations-of-msys2/
參考 https://creaink.github.io/post/Computer/Windows/win-msys2.html
在自己的 Windows 10 系統上使用 MSYS2 已經有段時間了,Windows 上的類 *nix 工作環境簡直不要太完美,辦事效率杠杠的(如果沒事少折騰的話)。為啥不用 Cygwin?因為很卡很慢,連 DUANG 的特效也拯救不了它,而且它的 POSIX 權限問題會把文件系統弄的一團糟。下面開始記錄從剛開始的小白逐漸成為現在熟練使用的 大牛 大白的辛酸歷程…
MSYS2 安裝與配置
這里主要參考 MSYS2開發環境搭建。
- 下載 MSYS2 installer,建議安裝路徑不要包含空格(個人裝在了
D:\MSYS2
下) - 安裝完畢后,開始菜單中生成的三個快捷方式為:
1
2
3MSYS2 MinGW 32-bit - D:\MSYS2\msys2_shell.cmd -mingw32
MSYS2 MinGW 64-bit - D:\MSYS2\msys2_shell.cmd -mingw64
MSYS2 MSYS - D:\MSYS2\msys2_shell.cmd -msys - 修改軟件源,修改
/etc/pacman.d/
目錄下的三個文件,換上清華的 repo,.msys
,.mingw32
,.mingw64
分別對應 MSYS2 里的三套系統,分別在三個文件最上方填寫(注意對應):1
2
3Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64 - 任選一個(
mingw32.exe
,mingw64.exe
,msys2.exe
)進入 MSYS2 的終端,更新系統:1
pacman -Syu
1
pacman -Su
- 安裝開發工具鏈:
1
2
3
4
5
6pacman -S base-devel
pacman -S gcc
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw-w64-i686-toolchain
pacman -S mingw64/mingw-w64-x86_64-clang
pacman -S mingw32/mingw-w64-i686-clang - 添加環境變量
PATH
。由於個人PATH
中存有Perl
的開發工具路徑,所以考慮將 MSYS2 的路徑置於其后:1
2
3
4
5
6
7...
\Strawberry\perl\bin
\Strawberry\perl\site\bin
\MSYS2\usr\bin
\MSYS2\mingw64\bin
\Strawberry\c\bin
...
個性化設置
自動掛載文件
/etc/fstab 1 |
# For a description of the file format, see the Users Guide |
配置文件
於個人 home
目錄中,個性化各種配置:
編譯 Vim
1 |
pacman -S ncurses-devel libcrypt-devel |
這里提醒自己下,折騰有度,比如嘗試在 MSYS2 中編譯 ycm 十幾次未遂這種行為是不可取的。
替換默認終端
將自帶的 Mintty 換成 Windows 下很棒的終端模擬器:ConEmu。
- 設置為默認的系統終端程序,替換掉殘廢的 cmd:
Settings
-Integration
-Default term - Force ConEmu as default terminal for console applications
- 如果希望「劫持」某些程序調試時使用的終端程序(比如 VS 在調試時會喚起系統默認的 cmd),在下面的輸入框中填入
explorer.exe|devenv.exe
- 對於 MSYS2 的集成:
- 新建 Task:
set MSYSTEM=MINGW64 & "/path/to/MSYS-connector -cur_console:n"
- Connector 網址:cygwin-connector,不同版本的設置如下:
- Cygwin:
conemu-cyg-32.exe
andconemu-cyg-64.exe
- MSYS 1.0:
conemu-msys-32.exe
- MSYS 2.0:
conemu-msys2-32.exe
andconemu-msys2-64.exe
sh.exe
同目錄下即可 - Cygwin:
- 新建 Task:
- 啟動時的相關設定(
Settings
-Startup
-Environment)
:1
2
3set PATH=%ConEmuBaseDir%\Scripts;%PATH%
chcp utf-8
set LANG=en_US.utf-8 - 當前的配置文件 conemu.xml
新版本的 ConEmu 已經自帶了適配各種系統下的 connector
,所以在 Task 里面關於 connector
的路徑直接填寫其程序名即可。(見 cygwin/msys terminal connector)
題外話 - Sublime Text 調用 ConEmu
對於一般程序,在設置中的 Integration
- Default term
- List of ...
中添加程序,ConEmu 將被自動喚起以替代 cmd,但添加 Sublime Text 的 sublime_text.exe
並沒有反應,只能曲線救國,比如 C 類型文件的編譯配置文件 C.sublime-build
:
1 |
{ |
問題
- 如果發現程序的輸出被緩沖:可嘗試利用工具
winpty
git clone
時出現字符相關的奇怪問題:使用 MSYS 而不是 MinGW64 或者 MinGW32- 2018-02-09 ncurses 版本升級為
ncurses-6.1.20180127-1-x86_64
后 ConEmu 終端會出現各種光標問題,可通過回退版本至ncurses-6.0.20170708-2-x86_64
來解決:1
2pacman -R ncurses-devel
pacman -U ncurses-6.0.20170708-2-x86_64.pkg.tar.xz --force - 2018-02-10 如果集成在 VSCode 中的 MSYS2 也出現上述情況, 可考慮直接使用
connector
:settings.json1
2
3{
"terminal.integrated.shell.windows": "E:/UtilityPrograms/ConEmu/ConEmu/conemu-msys2-64.exe",
}1
2
3
4
5{
"key": "ctrl+l",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus"
}
==================== End