Grab your Vim and go!

Time:2022-8-5

first fromshort book

Every Vimer has its own configuration, just like every swordsman has his own sword (it should be). (Strive) As a legendary full-stack developer, changing machines is as frequent as changing clothes; do you miss that set of fingerings that belong to you on a machine in a "foreign country"? I'll tell you what I'mWindowsSome experience with packaging Vim. Grab your Vim and go!

The configuration mentioned below is mainly from my own Vim configurationVimConf, you can refer to if you are interested!

One-click installation of Vimrc

Almost all configurations of Vim can be saved in a vimrc file, so by synchronizing vimrc, copying it directly to the corresponding location and specifying it, the corresponding Vim configuration can be restored. Therefore, many Vimer practices are to create a Github repository to save vimrc, and on a new machine, as long as the repository is cloned, the configuration can be synchronized to the machine.

After getting the configuration file, you need to copy the file to the corresponding location and apply it, and you may also need some other auxiliary files. Therefore, in order to do it once and for all, an initialization script is generally added for "one-click installation". The location of the configuration files on Linux and Windows is not the same, therefore, a script is often required for each.

Let's talk about the configuration file under Windows after Clonescript fileshould do something.

First you have to have GVim

where gvim > NUL 2> NUL
if %ERRORLEVEL% NEQ 0 (
    echo GVim is needed >&2
    set /A ret=1
    goto :end
)
for /f "tokens=*" %%i in ('where gvim 2^> NUL') do set vim_folder=%%i
if /I "%vim_folder:~-3%" EQU "bat" (
    findstr /b /c:"set VIM_EXE_DIR=" "!vim_folder!" > tmp\vim_folder.txt 2> NUL
    set /p vim_folder=<tmp\vim_folder.txt
    set vim_folder=!vim_folder:~16!
) else (
    call :get_parent_dir "%vim_folder%" vim_folder
)
if not exist "%vim_folder%\gvim.exe" (
    echo Failed to locate GVim
    set /A ret=1
    goto :end
)
echo Found GVim in %vim_folder%

passwhere gvimGet the path to the current GVim executable. Possibly a startup scriptGVim.bat, so you need to further analyze the script to get the real path of GVim.

There is nothing without GVim, so GVim must be installed.

Second you have to have Git

where git > NUL 2> NUL

Again, nothing is possible without Git, so Git must be installed.

update submodule

I include a color matching submodule in my vimrcDetorte, so the module needs to be initialized here.

git submodule update --init > NUL 2> NUL

After execution, configure thedetorteThe directory will contain files related to the color scheme, and these files need to be copied to the specified location below.

Copy the color scheme

Since I am using my own color scheme, I need to copy it to the specified location so that Vim can detect the scheme.

set detorte_folder=%vimfiles_folder%\colors
xcopy /Y /i detorte\colors %detorte_folder% /s /e > NUL 2> NUL

Install Vundle

I use Vundle for Vim plugin management, so in order to install the Vim plugin later, I need to download and install Vundle here. We clone directly from Vundle's Github to the specified directory%vimfiles_folder%\bundle\Vundle.vim, so that you can use Vundle directly in Vim later.

set vundle_repo=https://github.com/VundleVim/Vundle.vim.git
set vundle_folder=%vimfiles_folder%\bundle\Vundle.vim
if not exist %vundle_folder% (
    git clone %vundle_repo% %vundle_folder% > NUL 2> NUL
    if %ERRORLEVEL% NEQ 0 (
        echo Failed to clone Vundle.vim >&2
        set /A ret=1
        goto :end
    )
)

Install gadgets

We also need some small tools to assist the big killer of Vim. A convenient way to do this is to dedicate a Github repository to keep these gadgets.

Curl

Vundle needs to usecurl.exeto search for plugins, so we need to downloadcurl.exe. We will download and upload these required gadgets to a Github repository in advance, herecall :clone_win_utilswill download the repository to a temporary directory. In addition, the executable file needs to be copied to the Vim installation directory so that Vim can detect it.

if not exist "%vim_folder%\curl.exe" (
    call :clone_win_utils
    if !ERRORLEVEL! NEQ 0 (
        set /A ret=1
        goto :end
    )
    copy /Y %win_utils_folder%\curl.exe "%vim_folder%\" > NUL 2> NUL
    if !ERRORLEVEL! NEQ 0 (
        echo Failed to copy curl.exe, make sure you run this script as Administrator
        set /A ret=1
        goto :end
    )
)

Ag

ag.exeis a ratiogrepA better file search tool, and it is even more addicting to develop under Windows. In addition, many plugins of Vim can also be accelerated by using Ag. Similar to Curl, we also download the tool from the repository prepared in advance, and then copy it to the Vim directory.

if not exist "%vim_folder%\ag.exe" (
    call :clone_win_utils
    if !ERRORLEVEL! NEQ 0 (
        set /A ret=1
        goto :end
    )
    copy /Y %win_utils_folder%\ag.exe "%vim_folder%\" > NUL 2> NUL
    if !ERRORLEVEL! NEQ 0 (
        echo Failed to copy ag.exe, make sure you run this script as Administrator
        set /A ret=1
        goto :end
    )
)

Ctags

Needless to say about Ctags, everyone who uses Vim understands it! similar processing method.

if not exist "%vim_folder%\ctags.exe" (
    call :clone_win_utils
    if !ERRORLEVEL! NEQ 0 (
        set /A ret=1
        goto :end
    )
    copy /Y %win_utils_folder%\ctags.exe "%vim_folder%\" > NUL 2> NUL
    if !ERRORLEVEL! NEQ 0 (
        echo Failed to copy ctags.exe, make sure you run this script as Administrator
        set /A ret=1
        goto :end
    )
)

GNU Global

Combined with Ctags, it can reverse query references. similar processing method.

if not exist "%vim_folder%\gtags.exe" (
    call :clone_win_utils
    if !ERRORLEVEL! NEQ 0 (
        set /A ret=1
        goto :end
    )
    copy /Y %win_utils_folder%\global\ "%vim_folder%\" > NUL 2> NUL
    if !ERRORLEVEL! NEQ 0 (
        echo Failed to copy GNU Global, make sure you run this script as Administrator
        set /A ret=1
        goto :end
    )
)

markdown2ctags.py

This Python script is to makeTagbarThe plugin supports Markdown syntax to display the outline, which will be used in vimrc.

copy /Y markdown2ctags.py %vimfiles_folder%\ > NUL 2> NUL

copy vimrc

Does this need to be explained?

set vimrc_file=%USERPROFILE%\_vimrc
copy /Y .vimrc %vimrc_file% > NUL 2> NUL

At this point, start GVim,:VundleInstallAfter installing the configured plug-in, you can enjoy the familiar finger aerobics.

One-click deployment

For remote desktop servers or machines that cannot be connected to the Internet, the one-click installation mentioned above is not very applicable. In fact, we can generate a &quot;green&quot; GVim on the installed machine, bring all the configuration and Vim plug-ins, and then copy it to another machine to run directly.win_init.cmd portableA compressed package containing GVim will be generated in the root directory. Below, we describe the magic in detail.

mkdir %vim_portable_folder% 2> NUL

copy /Y .vimrc %vim_portable_folder%\_vimrc > NUL
set /A portable_ret=!portable_ret!+!ERRORLEVEL!

call :get_file_name "%vim_folder%" vim_exe_folder
xcopy /Y /i "%vim_folder%" "%vim_portable_folder%\!vim_exe_folder!" /s /e > NUL
set /A portable_ret=!portable_ret!+!ERRORLEVEL!

call :get_parent_dir "%vim_folder%" vim_install_folder
xcopy /Y /i "!vim_install_folder!\vimfiles" "%vim_portable_folder%\vimfiles" /s /e > NUL
set /A portable_ret=!portable_ret!+!ERRORLEVEL!

xcopy /Y /i "%vimfiles_folder%" "%vim_portable_folder%\vimfiles" /s /e > NUL
set /A portable_ret=!portable_ret!+!ERRORLEVEL!
  1. Create a new directory to store the green version of GVim;

  2. copy vimrc;

  3. Copy the directory where the GVim executable file is located (ie the main program);

  4. Copy the installation directory of GVim;

  5. copyvimfilesDirectory, mainly to install the plugins installed by Vundle and ourDetortecolor copy;

Well, now package this directory and copy it to another machine to have an identical Vim configuration.

perfectionist

Why not add a startup shortcut to start GVim?

The following script will generate ainit.cmdscript. Running the script once on the destination machine will add a startup script to the system disk.

(echo @echo off
     echo setlocal EnableExtensions
     echo set dest_gvim=C:\Windows\gvim.bat
     echo if exist "%%dest_gvim%%" EXIT /B 0
     echo set cur_dir=%%~dp0
     echo if /I "%%cur_dir:~-1%%" EQU "\" set cur_dir=%%cur_dir:~0,-1%%
     echo set exe_dir=%%cur_dir%%\!vim_exe_folder!
     echo echo @echo off ^> %%dest_gvim%%
     echo echo start "" "%%exe_dir%%\gvim.exe" %%%%* ^>^> %%dest_gvim%%
     echo pause
    ) >> "%init_cmd%"

Epilogue

Since then, Vimer and Vim live happily together!

However, one day, Vimer was unhappy again. I heard that it was to write Markdown notes? Well, go out and turn left:Vim and Markdown, the key is not separated from the hand!