created March 12, 2010 · complexity basic · author Vlad.irnov · version 7.0
Getting Python scripting in Vim on Windows can be a bit tricky, see :help python-dynamic:
- Python must be installed.
- Vim must be compiled with the Python interface (
:echo has("python")
). - The version of Python against which Vim was compiled must match the installed Python version, that is the Python DLL name, such as
python27.dll
.
You may not need this tip because the two main Windows installers for Vim already have Python support. The most up-to-date Vim version is available from http://sourceforge.net/projects/cream/files/ (Vim without Cream) and has Vim compiled for Python 2.7 (as of version 7.3.829). You may find this tip useful if you want to compile your own Vim on Windows if, for example, you need Vim for a different version of Python.
Prerequisites
- Vim version 7.3 is already installed. We only want to replace files
gvim.exe
andvim.exe
. - Python is installed. Let's assume it is Python 2.7 and it is installed in
C:/Python27
. The official Python installer from http://www.python.org placespython27.dll
in system path, usually C:\Windows\system32 or C:\Windows\SysWOW64. - These instructions are for compiling 32-bit Vim. Python must be 32-bit.
Step 1: Install MinGW tools
- Download automated MinGW installer (file
mingw-get-inst-20120426.exe
or similar) from https://sourceforge.net/project/showfiles.php?group_id=2435 - Run the installer. Default options are OK (we only need C compiler). Choose install directory without spaces in its path. Let's assume it's E:\MinGW.
Step 2: Download Vim source files
- Get Vim source files from one of the links below. The source version should match your currently installed Vim version.
- https://github.com/b4winckler/vim This is a
git
clone of Vim's official Mercurial repository. This may be the easiest way to get the most recent Vim source with all patches included. You can also select any previous version via branches/tags menu. - Getting the Vim source with Mercurial
- ftp://ftp.vim.org/pub/vim/pc/vim73src.zip
- ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2
- https://github.com/b4winckler/vim This is a
- Extract or check out all in one directory, for example E:\devel\vim_sources\vim-7-3-829.
Step 3 (optional): Apply patches
If you need to apply a patch, obtain Windows port of GNU patch (patch.exe) from http://gnuwin32.sourceforge.net/packages/patch.htm . Do a dry run:
cd vim_source_dir patch.exe --dry-run -p0 < path_to_patch_file > path_to_logfile
Apply the patch:
cd vim_source_dir patch.exe -p0 < path_to_patch_file > path_to_logfile
Step 4: Build gvim.exe and vim.exe
Create batch file build_vim.bat
with the following content. It will build gvim.exe and vim.exe with Python 2.7, HUGE feature set, no OLE. IMPORTANT: You must edit the paths and Python options.
@echo off REM Run this batch file from any directory to build gvim.exe and vim.exe. REM But first edit the paths and Python version number. REM --- Specify Vim /src folder --- set VIMSRC=E:\devel\vim_sources\vim-7-3-829\src REM --- Add MinGW /bin directory to PATH --- PATH = %PATH%;E:\MinGW\bin; REM --- Also make sure that PYTHON, PYTHON_VER below are correct. --- REM get location of this batch file set WORKDIR=%~dp0 set LOGFILE=%WORKDIR%log.txt echo Work directory: %WORKDIR% echo Vim source directory: %VIMSRC% REM change to Vim /src folder cd /d %VIMSRC% REM --- Build GUI version (gvim.exe) --- echo Building gvim.exe ... mingw32-make.exe -f Make_ming.mak PYTHON="C:/Python27" PYTHON_VER=27 DYNAMIC_PYTHON=yes FEATURES=HUGE GUI=yes gvim.exe > "%LOGFILE%" REM The following command will compile with both Python 2.7 and Python 3.3 REM mingw32-make.exe -f Make_ming.mak PYTHON="C:/Python27" PYTHON_VER=27 DYNAMIC_PYTHON=yes PYTHON3="C:/Python33" PYTHON3_VER=33 DYNAMIC_PYTHON3=yes FEATURES=HUGE GUI=yes gvim.exe > "%LOGFILE%" REM --- Build console version (vim.exe) --- echo Building vim.exe ... mingw32-make.exe -f Make_ming.mak PYTHON="C:/Python27" PYTHON_VER=27 DYNAMIC_PYTHON=yes FEATURES=HUGE GUI=no vim.exe >> "%LOGFILE%" REM The following command will compile with both Python 2.7 and Python 3.3 REM mingw32-make.exe -f Make_ming.mak PYTHON="C:/Python27" PYTHON_VER=27 DYNAMIC_PYTHON=yes PYTHON3="C:/Python33" PYTHON3_VER=33 DYNAMIC_PYTHON3=yes FEATURES=HUGE GUI=no vim.exe >> "%LOGFILE%" echo Moving files ... move gvim.exe "%WORKDIR%" move vim.exe "%WORKDIR%" echo Cleaning Vim source directory ... REM NOTE: "mingw32-make.exe -f Make_ming.mak clean" does not finish the job IF NOT %CD%==%VIMSRC% GOTO THEEND IF NOT EXIST vim.h GOTO THEEND IF EXIST pathdef.c DEL pathdef.c IF EXIST obj\NUL RMDIR /S /Q obj IF EXIST obji386\NUL RMDIR /S /Q obji386 IF EXIST gobj\NUL RMDIR /S /Q gobj IF EXIST gobji386\NUL RMDIR /S /Q gobji386 IF EXIST gvim.exe DEL gvim.exe IF EXIST vim.exe DEL vim.exe :THEEND pause
This batch file will compile Vim agains Python 2.7. If you want Python 3.x, change options PYTHON, PYTHON_VER, DYNAMIC_PYTHON to PYTHON3, PYTHON3_VER, DYNAMIC_PYTHON3 respectively. The best is to compile Vim with both Python 2 and Python 3 support because many Vim plugins were written for Python 2. Use the following command (it is commented out in the above code):
mingw32-make.exe -f Make_ming.mak PYTHON="C:/Python27" PYTHON_VER=27 DYNAMIC_PYTHON=yes PYTHON3="C:/Python33" PYTHON3_VER=33 DYNAMIC_PYTHON3=yes FEATURES=HUGE GUI=yes gvim.exe > "%LOGFILE%"
Step 5: Replace gvim.exe and vim.exe with new files
Shut down all instances of Vim. Navigate to Vim program folder: C:\Programs\Vim\vim73
or similar. Back up the original gvim.exe
and vim.exe
: either rename them to gvim_original.exe
, vim_original.exe
or move them to another folder.
Copy the new gvim.exe
and vim.exe
files to the Vim program folder (by copying the files, the default permissions of the folder should be applied to the copies). Start Vim and see if it works:
:version :py print 2**0.5 :py import sys; print sys.version
Comments
This is a very ambitious tip! Following are some initial thoughts:
- Our starting point for a "building Vim" tip is Build Vim with your name included (no useful content at the moment).
- Is the following correct: "The official Python installer ... puts python25.dll in %windir%/system32"? Perhaps that is an install option? I have installed Python several times and do not recall it dumping stuff in system32.
- There should be a "clean" option in the makefile that can be used, rather than the advice to delete certain directories (and pathdef.c is in the deleted directory so need to mention it).
- It is not really useful to build Vim without getting the current patches. The correct (and very easy) procedure now is to use the Mercurial repository set up by Bram to hold all the patched source and runtime files. We need documentation on how to do this.
- Using a suitable build from Vim-without-Cream is definitely the best approach for 99% of Windows users. Some hunting around would probably find a version with an old version of Python if required (it would have an older Vim, although one that would be newer than 7.2 with no patches).
JohnBot 06:14, March 30, 2010 (UTC)
What is the github stuff? Björn Winckler is a well known Vimmer, but the github is an unofficial clone of the official release available via Mercurial, so I don't see why it would be listed first. Also, some explanation is needed: the tip says to select the Vim version via "Switch Tags" but is that really the Vim version? JohnBeckett 10:13, April 4, 2011 (UTC)
- The GitHub mirror looks like the easiest way to download the most recent Vim source with all patches included, as well as any previous 7.x version. Only a web browser is needed. I assume that each tagged snapshot corresponds to Vim 7.x with indicated patches applied, but I cannot prove it. Getting the source from the official Mercurial repo requires installing Hg and figuring out how to use it. (I got the minimum, portable, CLI-only version and it takes up >17MB.) The Gettig_the_Vim_source_with_Mercurial tip is too much for someone who just wants to download the source and is not interested in Mercurial. And it says examples are for Linux. Vlad.irnov 18:42, April 4, 2011 (UTC)
Running make -f <Makefile> clean should remove relevant files and directories. --Kayosz (talk) 08:33, August 3, 2012 (UTC)
- There are problems with
mingw32-make.exe -f Make_ming.mak clean
. There are errors because it tries to delete files that do not exist. And it does not finish the job and leaves some directories.