MSYS2 的日常操作


 

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開發環境搭建

  1. 下載 MSYS2 installer,建議安裝路徑不要包含空格(個人裝在了 D:\MSYS2 下)
  2. 安裝完畢后,開始菜單中生成的三個快捷方式為:
    1
    2
    3
    MSYS2 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
  3. 修改軟件源,修改 /etc/pacman.d/ 目錄下的三個文件,換上清華的 repo,.msys.mingw32.mingw64 分別對應 MSYS2 里的三套系統,分別在三個文件最上方填寫(注意對應):
    1
    2
    3
    Server = 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
  4. 任選一個(mingw32.exemingw64.exemsys2.exe)進入 MSYS2 的終端,更新系統:
    1
    pacman -Syu
    提示需退出后更新, 強行關閉后重進, 再重復上述命令即可。 更新其他包:
    1
    pacman -Su
  5. 安裝開發工具鏈:
    1
    2
    3
    4
    5
    6
    pacman -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
  6. 添加環境變量 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
2
3
4
5
6
7
# For a description of the file format, see the Users Guide
# http://cygwin.com/cygwin-ug-net/using.html#mount-table
# DO NOT REMOVE NEXT LINE. It remove cygdrive prefix from path
none / cygdrive binary,posix=0,noacl,user 0 0
C:\Users\xxx\Desktop /desktop
D:\MSYS2\home\xxx\Notes /notes
F:\WorkingDirectory\Work\__current /current

配置文件

於個人 home 目錄中,個性化各種配置:

  • 比如與 mintty 相關的配置:
    .minttyrc
  • 比如常用的 .bashrc
    .bashrc
  • 比如 Git 需要代理:
    .gitconfig
  • 比如忽略大小寫的 .inputrc
    .inputrc
  • 比如 Vim 配置文件 .vimrc

編譯 Vim

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pacman -S ncurses-devel libcrypt-devel
./configure --with-features=huge \
--enable-luainterp=yes \
--enable-perlinterp=yes \
--enable-pythoninterp=yes \
--enable-python3interp=yes \
--with-lua-prefix=/usr/local \
--enable-fontset=yes \
--enable-cscope=yes \
--enable-multibyte \
--disable-gui \
--enable-fail-if-missing \
--prefix=/usr/local \
--with-compiledby='Professional operations'

這里提醒自己下,折騰有度,比如嘗試在 MSYS2 中編譯 ycm 十幾次未遂這種行為是不可取的。

替換默認終端

將自帶的 Mintty 換成 Windows 下很棒的終端模擬器:ConEmu

  1. 設置為默認的系統終端程序,替換掉殘廢的 cmd:Settings - Integration - Default term - Force ConEmu as default terminal for console applications
  2. 如果希望「劫持」某些程序調試時使用的終端程序(比如 VS 在調試時會喚起系統默認的 cmd),在下面的輸入框中填入 explorer.exe|devenv.exe
  3. 對於 MSYS2 的集成:
    1. 新建 Task:set MSYSTEM=MINGW64 & "/path/to/MSYS-connector -cur_console:n"
    2. Connector 網址:cygwin-connector,不同版本的設置如下:
      • Cygwin:conemu-cyg-32.exe and conemu-cyg-64.exe
      • MSYS 1.0:conemu-msys-32.exe
      • MSYS 2.0:conemu-msys2-32.exe and conemu-msys2-64.exe
      復制到 sh.exe 同目錄下即可
  4. 啟動時的相關設定(Settings - Startup - Environment)
    1
    2
    3
    set PATH=%ConEmuBaseDir%\Scripts;%PATH%
    chcp utf-8
    set LANG=en_US.utf-8
  5. 當前的配置文件 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

C.sublime-build
1
2
3
4
5
6
7
{
// "shell_cmd": "gcc -std=c11 -Wall -s \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"",
"shell_cmd": "/path/to/conemu /single -run cmd /c -cur_console:n \"gcc -std=c11 -Wall -s ${file} -o ${file_path}/${file_base_name} && ${file_path}/${file_base_name} & pause \"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c"
}

問題

  • 如果發現程序的輸出被緩沖:可嘗試利用工具 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
    2
    pacman -R ncurses-devel
    pacman -U ncurses-6.0.20170708-2-x86_64.pkg.tar.xz --force
  • 2018-02-10  如果集成在 VSCode 中的 MSYS2 也出現上述情況, 可考慮直接使用 connector:settings.json
    1
    2
    3
    {
    "terminal.integrated.shell.windows": "E:/UtilityPrograms/ConEmu/ConEmu/conemu-msys2-64.exe",
    }
    或者如果僅需為清屏設置快捷鍵:keybindings.json
    1
    2
    3
    4
    5
    {
    "key": "ctrl+l",
    "command": "workbench.action.terminal.clear",
    "when": "terminalFocus"
    }
 
 
  • 本文作者: 安安

 

==================== End

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM