This page looks best with JavaScript enabled

The Right Way to Use Vim on Windows

 ·  ☕ 2 min read

I had used Cygwin before, and Git Bash too. But neither of them works well with Vim, so I finally settled on MSYS2 as my console.

1. Comparing Cygwin, MinGW, and MSYS2

On Windows, the key to using Vim is finding a suitable console. When installing Vim plugins, a suitable console helps you avoid most of the pitfalls and lets you focus on using Vim.

FeatureCygwinMinGW/MSYSMSYS2
Is it GNUNoYesYes
More software supportSupports the vast majority of GNU softwareSupports common software; Git, Vim, and others need separate supportSupports most GNU software
More Linux-likeCygwin on Windows is like Wine on LinuxImplements major Linux programs such as BashNative 64/32-bit support
GCC compilationIncludes MinGW32 cross-compilation, supporting both compilation of programs that depend on cygwin.dll and standalone Windows programs; can compile Linux applications directlySupports standalone Windows program compilationSupports standalone Windows program compilation
Chinese supportDirectly supports Chinese display and input methodsRequires configuration to support Chinese display and input; deleting one Chinese character requires deleting twiceSupports Chinese display and input methods, Chinese help system, and Chinese prompts (for some software)
SpeedSlowFastFast

The choice here is MSYS2 without hesitation, because there is a pitfall (the msys-python27.dll issue) that cannot be worked around.

MSYS2 bundles pacman. pacman is the package manager of Arch Linux, and its common commands are as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
pacman -S package_name  #安装软件包
pacman -R package_name  #删除软件包
pacman -Rs package_name #顺便删除软件包相关依赖
pacman -Syu             #升级系统中的所有包
pacman -Ss package      #查询软件包
pacman -Qs package      #查询已安装的包
pacman -Qi package      #显示查找的包的信息
pacman -Ql package      #显示你要找的包的文件都安装的位置
pacman -Sw package      #下载但不安装包
pacman -U  /path/package.pkg.tar.gz #安装本地包
pacman -Scc             #清理包缓存,下载的包会在/var/cache 这个目录
pacman -Sf pacman       #重新安装包

Visit the MSYS2 website to download and install MSYS2.

2. Updating the Mirrors

The mirror configuration files for MSYS2 live in /etc/pacman.d.

1
2
 ls /etc/pacman.d/
gnupg  mirrorlist.mingw32  mirrorlist.mingw64  mirrorlist.msys

Add the USTC mirror to each of the three files beginning with mirrorlist.

Edit /etc/pacman.d/mirrorlist.mingw32 and add the following at the beginning of the file:

1
Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686

Edit /etc/pacman.d/mirrorlist.mingw64 and add the following at the beginning of the file:

1
Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64

Edit /etc/pacman.d/mirrorlist.msys and add the following at the beginning of the file:

1
Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch

Refresh the cache data by running:

1
pacman -Sy

3. Installing and Configuring Vim

  • Install Git (Vim will be installed along with it)

Run the command:

1
pacman -S git
  • Install Python 2

Run the command:

1
pacman -S python2
  • Configure Vim
1
2
3
4
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
vim ~/.vimrc
# 编辑 vimrc 内容
# ...

After entering Vim, run :PluginInstall to install the relevant Vim plugins.

4. Dealing with Garbled Characters

  • MSYS2 displays garbled characters

Create the file /usr/bin/win:

1
2
#!/bin/bash
$@ |iconv -f gbk -t utf-8

Create the file /etc/profile.d/alias.sh:

1
2
3
4
5
6
7
8
alias ls="/bin/ls --color=tty --show-control-chars"
alias grep="/bin/grep --color"
alias ll="/bin/ls --color=tty --show-control-chars -l"

alias ping="/bin/win ping"
alias netstat="/bin/win netstat"
alias nslookup="/bin/win nslookup"
alias ipconfig="/bin/win ipconfig"

Besides the method above, you can also right-click the window and choose Options to set up Chinese display:

  • Vim displays garbled Chinese characters

Add the following to the .vimrc file:

1
2
3
4
5
6
7
8
9
" 默认编码
if has('vim_starting')
    if &encoding !=? 'utf-8'
        let &termencoding = &encoding
    endif
    set encoding=utf-8
    set fileencoding=utf-8
    set fileencodings=ucs-bom,utf-8,default,cp936
endif

5. Configuring ConEmu or Cmder

In Settings, add a new entry under Startup -> Tasks.

1
set  MSYS2_PATH_TYPE=inherit & set CHERE_INVOKING=1 & %ConEmuDrive%\interpreter\msys2_32\usr\bin\bash.exe --login -i -new_console:C:"%ConEmuDrive%\interpreter\msys2_32\msys2.ico"

6. References


微信公众号
WRITTEN BY
微信公众号