Vim Tips Wiki
(Created page with 'To use Python scripting in Vim on Windows, Vim must be compiled against a particular version of Python ([http://vimdoc.sourceforge.net/htmldoc/if_pyth.html#python-dynamic :help p…')
 
(Not going to delete, still useful. However since I'm not certain what all needs changing I threw up a deprecated tag. At the very least we need github links instead of google code.)
Tag: sourceedit
 
(38 intermediate revisions by 6 users not shown)
Line 1: Line 1:
  +
{{TipNew
To use Python scripting in Vim on Windows, Vim must be compiled against a particular version of Python ([http://vimdoc.sourceforge.net/htmldoc/if_pyth.html#python-dynamic :help python-dynamic]). The official Vim 7.2 [http://www.vim.org/download.php#pc Windows installer from vim.org] installs "gvim.exe" and "vim.exe" compiled against Python 2.4. (Use command :version to see which Python version your Vim needs, there should be "python24.dll" for version 2.4, etc.) Those who want to use Python 2.5 or 2.6 are out of luck. They have the following options:
 
  +
|id=1650
  +
|previous=1649
  +
|next=1651
  +
|created=March 12, 2010
  +
|complexity=basic
  +
|author=Vlad.irnov
  +
|version=7.0
  +
|subpage=/201003
  +
|category1=Building Vim
  +
|category2=Python
  +
|category3=Windows
  +
}}
  +
{{Deprecated|These instructions are obsolete as Vim's official source repository is now in GitHub, there are official nightly builds, etc. Please improve them if you can.}}
  +
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 (<code>:echo has("python")</code>).
  +
# 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".
  +
# There are two somewhat incompatible Python versions: Python 2 and Python 3. They are invoked with different Vim commands: python/py and python3/py3. Some Vim plugins may target Python 2 and others Python 3. Ideally, Vim should be compiled with support for both Python 2 and Python 3.
   
#Use the [http://sourceforge.net/projects/cream/files/ Cream installer]. It installs the most up-to-date Vim compiled against the newest version of Python (probably 2.6).
+
'''You may not need this tip because the two main Windows installers for Vim usually already have Python support.''' The most up-to-date Vim version is available from http://sourceforge.net/projects/cream/files/ (Vim without Cream). 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.
#Scour the Internets for files "gvim.exe" and "vim.exe" compiled by kind strangers against the desired version of Python.
 
#Compile executables "gvim.exe" and "vim.exe" themselves. This tip shows how to do this. The advantage over the other two approaches is that it gives complete control over which Vim features are included.
 
   
 
==Prerequisites==
 
==Prerequisites==
 
* Vim version 7.3.x or 7.4.x is already installed. We only want to replace files <code>gvim.exe</code> and <code>vim.exe</code>.
NOTE: These instructions were developed on Windows 2000 using Vim 7.2. Replace paths with your own. Do not use paths with spaces in them.
 
  +
* Python 2.x and Python 3.x are installed. Let's assume '''Python 2.7''' is installed in <code>C:/Python27</code> and '''Python 3.3''' is installed in <code>C:/Python33</code>. The official Python installers from http://www.python.org place the DLL files (python27.dll and python33.dll) in the 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 [http://www.mingw.org/ MinGW tools]==
*Vim version 7.2 is already installed. We only want to replace files "gvim.exe" and "vim.exe".
 
 
Download and run automated MinGW installer from https://sourceforge.net/project/showfiles.php?group_id=2435
*Python version 2.x is installed. Let's assume it's '''Python 2.5''' and it's installed in '''C:/Python'''. The official Python installer from http://www.python.org puts python25.dll in %windir%/system32, so it's in the system search path.
 
  +
The "base" package should be enough (we only need C compiler). Choose install directory without spaces in its path.
*We will compile Vim using NORMAL feature set, plus feature "signs", no OLE. Adjust options in the batch file below if you want BIG or other feature set.
 
  +
(NOTE: There are problems with the current MinGW installer, mingw-get-setup.exe, as of Oct 14, 2013.)
   
  +
Alternatively, install MinGW using installer or individual packages from the TDM-GCC project: http://tdm-gcc.tdragon.net/
==Install [http://www.mingw.org/ MingGW tools]==
 
   
  +
Let's assume MingW is installed in <code>E:\MinGW</code>.
*Download '''Automated MinGW Installer''' (file "MinGW-5.1.6.exe") from https://sourceforge.net/project/showfiles.php?group_id=2435
 
*Put installer in a separate directory. During installation a bunch of compressed files is downloaded and logs are created. These files will remain after installation is finished.
 
*Launch installer. Choose Download and Install. Install type: current.
 
*Choose components to install: ''MinGW base tools'' (minimal type of install), ''MinGW Make'', all other are optional.
 
*Choose install directory. Let's assume it's '''C:\MinGW'''
 
*Add the "bin" directory of MinGW installation to system PATH environment variable. Go to Control Panel -> System -> Advanced -> Environment Variables. Edit or create variable "PATH". Append "'''C:\MinGW\bin;'''" for value. It doesn't matter if it's in User Variables or System Variables.
 
   
==Download Vim source files ==
+
==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.'''
*From http://ftp.vim.org/vim/unix/ download "vim-7.2.tar.bz2"
 
  +
** https://github.com/b4winckler/vim This is a <code>git</code> clone of [http://code.google.com/p/vim/ Vim's official Mercurial repository]. This may be the easiest way to get the most recent Vim source with all patches included, because you can download snapshots with a web browser without installing any other software. Google Code holds the official Vim repository but does not offer this feature.
*From http://ftp.vim.org/vim/extra/ download "vim-7.2-extra.tar.gz", "vim-7.2-lang.tar.gz".
 
  +
** [[Getting the Vim source with Mercurial]]
*Extract all in one directory: '''C:\devel\vim72'''. Any popular Windows archiver can extract these files: WinRAR, 7-Zip.
 
  +
** ftp://ftp.vim.org/pub/vim/unix/
  +
* Extract or check out all in one directory, for example <code>E:\devel\vim_sources\vim-7-4-050</code>.
   
  +
==Step 3 (optional): Apply patches ==
==Optional: edit Vim feature set==
 
  +
If you need to apply a patch, obtain Windows port of GNU patch (patch.exe) from http://gnuwin32.sourceforge.net/packages/patch.htm .
The NORMAL feature set does not include "signs". To add this feature, edit file "''C:\devel\vim72\src\feature.h''":
 
  +
Do a dry run:
add "|| defined(FEAT_NORMAL)" to +signs section.
 
  +
<pre>
  +
cd vim_source_dir
  +
patch.exe --dry-run -p0 < path_to_patch_file > path_to_logfile
  +
</pre>
  +
Apply the patch:
  +
<pre>
  +
cd vim_source_dir
  +
patch.exe -p0 < path_to_patch_file > path_to_logfile
  +
</pre>
  +
 
==Step 4: Build gvim.exe and vim.exe==
  +
Create a batch file <code>build_vim.bat</code> with the following content. It will build gvim.exe and vim.exe with Python 2.7 and Python 3.3 support, HUGE feature set, no OLE.
  +
You can remove options PYTHON3, PYTHON3_VER, DYNAMIC_PYTHON3 if you need only Python 2.7.
  +
  +
'''IMPORTANT: You must edit the paths and Python options.'''
   
==Build "gvim.exe" and "vim.exe"==
 
Create a batch file, "'''build_vim.bat'''", with the following content. Instead of "'''C:/Python'''" use directory where Python is installed on your system.
 
 
<pre>
 
<pre>
 
@echo off
 
@echo off
REM this file must be run in Vim source directory (e.g., C:\devel\vim72\src)
+
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 these folders and files can be deleted after compilation: /gobj/, /obj/, pathdef.c
 
   
  +
REM --- Specify Vim /src folder ---
REM build GUI version (gvim.exe)
 
  +
set VIMSRC=E:\devel\vim_sources\vim-7-4-050\src
mingw32-make.exe -f Make_ming.mak PYTHON="C:/Python" PYTHON_VER=25 DYNAMIC_PYTHON=yes NETBEANS=no FEATURES=NORMAL gvim.exe > build_vim_log.txt
 
  +
REM --- Add MinGW /bin directory to PATH ---
  +
PATH = E:\MinGW\bin;%PATH%
  +
REM --- Also make sure that PYTHON, PYTHON_VER below are correct. ---
   
  +
REM get location of this batch file
REM build console version (vim.exe)
 
  +
set WORKDIR=%~dp0
mingw32-make.exe -f Make_ming.mak PYTHON="C:/Python" PYTHON_VER=25 DYNAMIC_PYTHON=yes NETBEANS=no FEATURES=NORMAL GUI=no vim.exe >> build_vim_log.txt
 
  +
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 ...
  +
REM The following command will compile with both Python 2.7 and Python 3.3
 
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 ...
  +
REM The following command will compile with both Python 2.7 and Python 3.3
 
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
 
pause
 
</pre>
 
</pre>
   
  +
Run the batch file. It will build files "gvim.exe" and "vim.exe" and move them into the folder of the batch file. The build process can take a few minutes. A log file will also be created. Any errors will appear in the console window.
Move the batch file to the Vim source directory, that is '''C:\devel\vim72\src'''
 
   
Run the batch file to build "gvim.exe" and "vim.exe". It takes 4-6 min.
+
==Step 5: Replace gvim.exe and vim.exe with new files==
 
Shut down all instances of Vim. Navigate to Vim program folder: <code>C:\Programs\Vim\vim73</code> or similar. Back up the original <code>gvim.exe</code> and <code>vim.exe</code>: either rename them to <code>gvim_original.exe</code>, <code>vim_original.exe</code> or move them to another folder.
   
  +
Copy the new <code>gvim.exe</code> and <code>vim.exe</code> 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:
Any errors should appear in the console window. Clean up: after script is finished, delete folders "gobj" and "obj", files "pathdef.c", "build_vim_log.txt".
 
  +
<pre>
 
 
:py print 2**0.5
==Replace old "gvim.exe" and "vim.exe" with new files==
 
 
:py import sys; print sys.version
Exit Vim. Navigate to Vim program folder: "C:\Programs\Vim\vim72" or similar.
 
  +
:py3 print(2**0.5)
Back up original "gvim.exe" and "vim.exe": either rename them to gvim_original.exe, vim_original.exe or move them to another folder.
 
  +
:py3 import sys; print(sys.version)
  +
</pre>
   
  +
==Comments==
Move freshly built "gvim.exe" and "vim.exe" to Vim program folder. Start Vim and see if it works:
 
  +
This is a very ambitious tip! Following are some initial thoughts:
:: :py print 2**0.5
 
  +
*Our starting point for a "building Vim" tip is [[Build Vim with your name included]] (no useful content at the moment).
:: :py import sys; print sys.version
 
  +
*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 [http://code.google.com/p/vim/ 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).
 
[[User:JohnBot|JohnBot]] 06:14, March 30, 2010 (UTC)
   
  +
----
That's it!
 
  +
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<s>, so I don't see why it would be listed first</s>. Also, some explanation is needed: the tip says to select the Vim version via "Switch Tags" but is that really the Vim ''version''? [[User:JohnBeckett|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. [[User:Vlad.irnov|Vlad.irnov]] 18:42, April 4, 2011 (UTC)
   
  +
----
[[User:Vlad.irnov|Vlad.irnov]] 02:30, March 12, 2010 (UTC)
 
  +
Running make -f <Makefile> clean should remove relevant files and directories.
  +
--[[User:Kayosz|Kayosz]] ([[User talk:Kayosz|talk]]) 08:33, August 3, 2012 (UTC)
  +
: There are problems with <code>mingw32-make.exe -f Make_ming.mak clean</code>. There are errors because it tries to delete files that do not exist. And it does not finish the job and leaves some directories.[[User:Vlad.irnov|Vlad.irnov]] ([[User talk:Vlad.irnov|talk]]) 11:07, May 1, 2013 (UTC)

Latest revision as of 17:14, 5 January 2017

Tip 1650 Printable Monobook Previous Next

created March 12, 2010 · complexity basic · author Vlad.irnov · version 7.0


This tip is deprecated for the following reasons:

These instructions are obsolete as Vim's official source repository is now in GitHub, there are official nightly builds, etc. Please improve them if you can.

Getting Python scripting in Vim on Windows can be a bit tricky, see :help python-dynamic:

  1. Python must be installed.
  2. Vim must be compiled with the Python interface (:echo has("python")).
  3. 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".
  4. There are two somewhat incompatible Python versions: Python 2 and Python 3. They are invoked with different Vim commands: python/py and python3/py3. Some Vim plugins may target Python 2 and others Python 3. Ideally, Vim should be compiled with support for both Python 2 and Python 3.

You may not need this tip because the two main Windows installers for Vim usually already have Python support. The most up-to-date Vim version is available from http://sourceforge.net/projects/cream/files/ (Vim without Cream). 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.x or 7.4.x is already installed. We only want to replace files gvim.exe and vim.exe.
  • Python 2.x and Python 3.x are installed. Let's assume Python 2.7 is installed in C:/Python27 and Python 3.3 is installed in C:/Python33. The official Python installers from http://www.python.org place the DLL files (python27.dll and python33.dll) in the 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 and run automated MinGW installer from https://sourceforge.net/project/showfiles.php?group_id=2435 The "base" package should be enough (we only need C compiler). Choose install directory without spaces in its path. (NOTE: There are problems with the current MinGW installer, mingw-get-setup.exe, as of Oct 14, 2013.)

Alternatively, install MinGW using installer or individual packages from the TDM-GCC project: http://tdm-gcc.tdragon.net/

Let's assume MingW is installed in E:\MinGW.

Step 2: Download Vim source files[]

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 a batch file build_vim.bat with the following content. It will build gvim.exe and vim.exe with Python 2.7 and Python 3.3 support, HUGE feature set, no OLE. You can remove options PYTHON3, PYTHON3_VER, DYNAMIC_PYTHON3 if you need only Python 2.7.

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-4-050\src
REM --- Add MinGW /bin directory to PATH ---
PATH = E:\MinGW\bin;%PATH%
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 ...
REM The following command will compile with both Python 2.7 and Python 3.3
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 ...
REM The following command will compile with both Python 2.7 and Python 3.3
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

Run the batch file. It will build files "gvim.exe" and "vim.exe" and move them into the folder of the batch file. The build process can take a few minutes. A log file will also be created. Any errors will appear in the console window.

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:

:py print 2**0.5
:py import sys; print sys.version
:py3 print(2**0.5)
:py3 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.Vlad.irnov (talk) 11:07, May 1, 2013 (UTC)