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

 

posted @ 2019-12-25 15:38  lsgxeva  阅读(2556)  评论(0编辑  收藏  举报