# Nanny tutorial! Build VIM into an IDE (Python)

2022-01-30 13:19:07

I've been tossing about since last week , Did something about it Vim IDE for Python & Go, I will sort out the whole construction process and share this article , This is Python Version of nanny tutorial , Actually, I wrote Go Version of , If you want to read this article, you can like it , I'll post it in the next article

Speaking of IDE, There will always be people who will because of which one is The best editing tool in the world And quarrel , But this article will not cover 、 I don't want to mislead everyone , I believe in different people 、 Different usage scenarios have different optimal solutions , There is no one size fits all editor in the world .

If it's on the desktop ,PyCharm and VS Code Have done well enough , It's hard to have a third editor to match .

But if you want to say on the server side ？ There seems to be no choice ,Vim It's almost your only choice .

Vim It's a very productive tool , Even in the eyes of some people , It's a devilish editor , Why do you say that , Because it has a very high starting threshold , The learning curve is very steep .

One is , It is a proprietary compiler for the programmer community , You need to learn extra and understand its design concept , And you need to remember a lot of complex operating instructions .

Two is , For engineering project code , It's not an editor out of the box , You need to install a lot of plug-ins 、 It takes a lot of configuration to become a hand-held IDE Tools .

The reason , I use Vim As a development tool , There are four reasons ：

1. direct ： Normal local IDE After coding , To upload the remote server for compilation and testing , More trouble , I am directly in SSH It is more direct for the server to code .
2. worry ： There is no need to install multiple proprietary editors for multiple languages , such as PyCharm、Goland etc. , And don't bother to crack all kinds of paid software .
3. convenient ： Improve iPad The productivity of , You can write code online without a computer when you go out , You don't have to bring a heavy computer every time .
4. Pack to force ： Don't you think it's cool ？（ flee ...

If you are right about Vim Know nothing about the operation , Then please go and find out Vim Daily use of , Otherwise, the following is not for you .

## 1. preparation

This article is in Mac Operating demonstration under the environment of , But the same applies to Linux Environmental Science （ A little difference , I'll point out... In the corresponding position ）, If all you have is Windows System , have access to GVim.

Before you start installing the configuration , Let's first talk about the overall idea of this article ：

1. Prepare the operating environment ： install Python perhaps Go Environmental Science
2. Get ready Vim edition ： Use Vim 8.2 Highest version
3. Plug in installation environment ： The plug-ins are all there Github And other external networks , You need to configure some agents
4. Plug in installation ： One click batch installation of plug-ins
5. The plug-in configuration ： After the plug-in is installed , It takes some configuration to make it easy to use
6. The plug-in USES ： Demonstrate how to use each plug-in

## 2. Prepare the operating environment

Vim Primordial right Python Provided support , When you install 8.2 Version of Vim when , It will be installed automatically Python , But the installed version is not the version you need , But it doesn't matter ,Vim Run using Python Version is configurable .

The version I use here is Python 3.10.0

$python3 --version Python 3.10.0 Copy code  ## 3. install / upgrade Vim 8.2 natural Mac perhaps Linux Machines come with their own Vim Tools , It's just that the version may be lower , If you use these versions Vim , Some plug-ins will not be installed or used , Like YouCompleteMe This very important plug-in , If you don't use Vim 8.1+ , Every time you use vim Will prompt you , It really affects the experience $ vim main.go
YouCompleteMe unavailable: requires Vim 8.1.2269+.
Press ENTER or type command to continue
Copy code 

These plug-ins have been continuously updated for many years , For the old version Vim No more support, which is understandable .

If you use it Linux , The whole process will be much smoother , What I'm using here is CentOS 7.6 Of Linux.

First, find the... Installed in the system vim What's in the bag , And then use yum remove Go uninstall it

[[email protected] ~]# yum list installed | grep -i vim
[[email protected] ~]#
[[email protected] ~]# yum remove vim-common vim-enhanced vim-filesystem vim-minimal
Copy code 

Later, I will use the source code compilation method to install Vim 8.2, However, the following basic dependencies need to be installed for compilation

[[email protected] ~]# yum install -y gcc make ncurses ncurses-devel
[[email protected] ~]# yum install ctags git tcl-devel \
ruby ruby-devel \
lua lua-devel \
luajit luajit-devel \
python python-devel \
perl perl-devel \
perl-ExtUtils-ParseXS \
perl-ExtUtils-XSpp \
perl-ExtUtils-CBuilder \
perl-ExtUtils-Embed
Copy code 

git clone https://github.com/vim/vim.git
Copy code 

Get into vim/src Directory execute the following three commands to compile and install

[[email protected] ~]# ./configure --prefix=/usr/local/vim \
--enable-pythoninterp=yes \
--enable-python3interp=yes \
--with-python-command=python \
--with-python3-command=python3
[[email protected] ~]# make && make install
[[email protected] ~]#
Copy code 

If nothing else , After command execution , You just need to configure another software link , It can be used normally 8.2 Version of Vim 了 .

[[email protected] ~]# ln -s /usr/local/vim/bin/vim /usr/bin/vim
[[email protected] src]# vim --version | head -n 1
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Oct 19 2021 22:05:46)
Copy code 

## 4. Plug in installation

Vim The function improved by itself has been very powerful , But it's too difficult to get started , Install some customized plug-ins , Can make the whole Vim Interface management and use are more in line with human intuition , Lower the use threshold .

Which plug-ins to install , It depends on what you want to do Vim What it looks like ？

There's no need to keep your head down , Compared to the desktop IDE Software to copy homework OK Yes .

For me personally , I use it everyday IDE The most functions are ：

• Automatic code completion
• Code tracking jump
• Static code checking
• Run debug code
• Global search code
• Project code bookmark
• Code version management
• Code highlight
• The file tree of the project
• Single file code structure tree
• Multiple files can be opened at the same time
• Markdown Real-time Preview

Then I'll find the corresponding plug-in according to this function

• YouCompleteMe： Provide automatic code completion and code tracking jump
• auto-pairs： Plug ins that automatically complete parentheses , Including parentheses , brackets , And curly braces
• NERDTree： Provide the file tree of the project 、 Support bookmark function
• vim-nerdtree-tabs： You can open multiple code files , send nerdtree Of tab More friendly
• nerdtree-git-plugin： You can see it in the navigation directory git Version information
• tagbar： Plug ins that can view the list of variables and functions in the current code file , And switch and jump to the position of the corresponding variables and functions in the code
• vim-airline：Vim Status bar plugin , Including display line number , Column number , file type , file name , as well as Git state
• vim-gitgutter： Can be displayed in the document git Information
• vim-one： Code color scheme
• markdown-preview.vim：Markdown Preview support
• mathjax-support-for-mkdp：Markdown Mathematical formula preview support
• vim-godef：go Code tracking in , Input gd You can jump automatically
• fatih/vim-go： Static inspection and a series of go Related tools
• ultisnips / vim-snippets： Automatic generation Code block

So how to install these plug-ins ？

It's simple , You just use vi In your ~/.vimrc In file , Paste the following paragraph to the end of the configuration file

"  Where the plug-in starts
call plug#begin('~/.vim/plugged')

"  Code autocomplete , After installing the plug-in, you need additional configuration to use it
Plug 'ycm-core/YouCompleteMe'

"  Sidebar to provide a navigation directory
Plug 'scrooloose/nerdtree'

"  You can make  nerdtree  Of  tab  More friendly
Plug 'jistr/vim-nerdtree-tabs'

"  You can see it in the navigation directory  git  Version information
" Plug 'Xuyuanp/nerdtree-git-plugin'

"  Plug in to view the list of variables and functions in the current code file ,
"  You can switch and jump to the corresponding variables and functions in the code
"  Outline navigation , Go  need  https://github.com/jstemmer/gotags  Support
Plug 'preservim/tagbar'

"  Plug ins that automatically complete parentheses , Including parentheses , brackets , And curly braces
Plug 'jiangmiao/auto-pairs'

" Vim Status bar plugin , Including display line number , Column number , file type , file name , as well as Git state
Plug 'vim-airline/vim-airline'

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
"  Plug ins that can be quickly aligned
Plug 'junegunn/vim-easy-align'

"  Can be displayed in the document  git  Information

" markdown  plug-in unit
Plug 'iamcco/mathjax-support-for-mkdp'
Plug 'iamcco/markdown-preview.vim'

"  The following two plug-ins should be used together , Code blocks can be generated automatically
Plug 'SirVer/ultisnips'
Plug 'honza/vim-snippets'

" go  Main plug-ins
Plug 'fatih/vim-go', { 'tag': '*' }

" go  Code tracking in , Input  gd  You can jump automatically
Plug 'dgryski/vim-godef'

"  Can be in  vim  Use in  tab  completion
"Plug 'vim-scripts/SuperTab'

"  Can be in  vim  Automatic completion in
"Plug 'Shougo/neocomplete.vim'

"  Where the plug-in ends , Plug ins are all on this line
call plug#end()
Copy code 

Then type the command :wq Save and exit vi.

There are many management tools for installing plug-ins , such as Vundle,vim-plug etc. .

Vundle It is a very famous and long history Vim Plug in management tools . But with the installation vim More and more plug-ins , Use Vundle It becomes less and less efficient to manage these plug-ins ,vim It takes more and more time to start .

and vim-plug It is a very lightweight and efficient vim Plug in management tools . It supports fully asynchronous 、 Multi threaded parallel installation plug-ins , Support git Branch 、 Labels etc. , The plug-in can be rolled back and updated 、 It also supports on-demand loading of plug-ins (On-demand loading), You can specify to load the corresponding file for a specific file type vim plug-in unit , Greatly accelerated vim Starting time .

So I will use vim-plug This management tool , Use the following command to install vim-plug Plug in management tools

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
Copy code 

Then please restart your terminal , Guaranteed reinitialization , Or wait for you to execute PlugInstall When , It is possible to report that the command does not exist .

Not an editor command: PlugInstall
Copy code 

After restarting the terminal , Enter open again vim Input :PlugInstall Start the installation process

If you don't have network problems （ Just science ）, Then the installation will be smooth ..

Input :PlugStatus You will see that all plug-ins are installed OK.

## 5. YouCompleteMe

The plug-in installation above , In fact, the things we do are relatively simple , Is to put Github Pull from the warehouse on the to the local ~/.vim/plugged Catalog

In general , These plug-ins are out of the box , There will be no complex dependencies , But only one plug-in is special , It is YouCompleteMe  , It claims to be the hardest to install Vim plug-in unit .

I'm in the local Mac The machine was installed for two nights , To solve all the dependencies , But in Linux It went quite smoothly in the .

The specific installation steps are

1. Get into ~/.vim/plugged/YouCompleteMe Plug-in directory , modify .gitmodules Medium github.com by Mirror website hub.fastgit.org
2. Then install the first level dependency ：git submodule update --init
3. After the first level dependency is properly installed , Revise third_party/ycmd All dependencies in the directory .gitmodules Medium github.com by Mirror website hub.fastgit.org
4. Then recursively install its dependent packages ：git submodule update --init --recursive
5. Finally, execute python3 install.py --all ,--all All language features supported by the plug-in will be installed .

stay Mac During installation on , There are quite a lot of problems , It also involves changing YouComplete Code for , Finally, it can be installed normally , You may encounter similar problems during installation , If there are questions , Feel free to leave a comment in the comments section , I'll try my best .

## 6. Set the mirror proxy

The process of installing plug-ins above will actually go to Github Download the corresponding plug-in , But for various indescribable reasons , Access... On the mainland server github It's very slow. , Even inaccessible .

I don't have any network settings , 20 A plug-in , None of them were installed successfully .

So here it is , You have to find a way , Let your server access at normal speed Github, As for how to do , Some black technologies are inconvenient for me to elaborate here , Let's introduce one that can be made public 、 And a very effective way .

modify ~/.vim/autoload/plug.vim take

let fmt = get(g:, 'plug_url_format', 'https://git::@github.com/%s.git')
Copy code 

Change to

let fmt = get(g:, 'plug_url_format', 'https://git::@hub.fastgit.org/%s.git')
Copy code 

Will this line

\ '^https://git::@github\.com', 'https://github.com', '')
Copy code 

Change to

\ '^https://git::@hub.fastgit\.org', 'https://hub.fastgit.org', '')
Copy code 

And then into vim perform :PlugInstall That's all right.

## 7. The plug-in USES

After most plug-ins are installed , Can be used immediately , However, some plug-ins need to be configured to be more convenient .

Because there are many configurations , I won't post it directly here , If you are interested, add me v：hello-wbm, I need to find the configuration table .

YouComplete

Use IDE The most basic appeal , Isn't it possible when you code , Automatically give a prompt , And then automatically complete it ,vim With YouComplete After the blessing of , It's fine too 100% Restore the desktop coding experience .

NERDTree

After opening the file , Use F9 Or input NERDTreeToggle It will open the file tree in the sidebar , This is a NERDTree The convenience provided to us .

tagbar

open Python After the document , Use F9 Or input :tagbar You can open it tagbar window , In this window, you can see all the structures of the file 、 function 、 Variable etc. , These can be called tag, When you locate a tag when , Enter directly to jump to the position of the code window on the left .

vimgrep

vimgrep Can be used for code search of engineering projects , For students who often read the source code, it is an essential weapon , It is Vim Built-in tools , Very powerful .

run out vimgrep After searching , Under normal circumstances , There will be no feedback , If you need to see the search results , And jump to the corresponding position , have access to QuickFix , Just type in :cw perhaps :copen

## 8. Run code

Use Vim After writing the code , Imagine PyCharm Run the code directly with the shortcut keys , I need you here. .vimrc Write the following configuration in .

This configuration , Not only Python , also Bash and Golang

" F5 to run sh/python3
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'sh'
:!time bash %
elseif &filetype == 'python'
exec "!time python3 %"
elseif &filetype == 'go'
exec "!time go run %"
endif
endfunc
Copy code 

After the configuration , Use F5 You can run the current script directly .

## 9. stay iPad Write the code

If you're like me , It has its own server , Then follow the above steps to Vim After configuration , You can go to iPad through SSH Connect to the server and write the code .

If you don't have a server , As long as you can add me v: hello-wbm, I'll give you a one-year alicloud server , Quota co., LTD. , I can only say first come, first served .

I happen to have one in my hand 2020 The paragraph iPad Pro, Usually it is also used for video , I'm really a little sorry Pro This configuration , With Vim This artifact , Productivity up A little bit ..

## 10. At the end

It is necessary to state , The reason why it took five days , In fact, I put some computers in my hand , The server is fully configured Vim IDE, Different machines , The problems encountered are a little different , Among them, in my Mac On , Most problems encountered , Toss for the longest time , Some of these problems , I Google No answer , Finally, I looked at the code , Modified part of the code to run .

in addition , about Vim Come on , The most important thing is .vimrc file , I may have missed some configuration explanations in the above explanation , If you find that the use is not so smooth , You can download my .vimrc file ：wwe.lanzoui.com/i9gD5vrzufi

This article is about Python Version of Vim IDE Set up guide , The code demonstration is basically Python Code , According to my idea in the article, operate step by step , You can build your own online IDE Environmental Science .

I don't just write Python Code , And write some Go Code for , Vim about Python Native provides more support , And by contrast ,Go However, more plug-ins need to be installed to achieve a good coding experience , But because most of this number is Python developer , This part , I'll write another Vim for Go The article . Interested friends can tell me in the comment area , I'll send you the address .

Okay , The above is the whole content of this article , If you have any questions about the installation configuration , Welcome to the comments section ~

The original is from personal blog ：iswbm.com/591.html

## At the end of the article , Insert a benefit

Double eleven is coming , Alibaba cloud has also started activities , It's just that I can show you Piao Alicloud's servers .

To put it bluntly, everyone You can spend nothing , You can get the server , The specification is 2c2m（2vcpu 2G memory） Machine .

Yesterday I sent a message in my circle of friends , Now there are 400 People signed up for , Borrow this article today to say , There are friends who want to join , You can add me v（hello-wbm）, Take everyone to collect the wool .

I've written a lot on nuggets Python Related articles , These include Python Utilities ,Python Efficient skills ,PyCharm Using skills , I'm glad to get the recognition and support of many friends .

With their encouragement , I sorted the past articles into three books PDF e-book

PyCharm Chinese guide

《PyCharm Chinese guide 》 Use 300 Multiple sheets GIF The form of dynamic graph , Explained in detail the most practical development 105 individual PyCharm Efficient use skills , The content is easy to understand , Suitable for all Python developer .

Python Guide to the Dark Arts

《Python Guide to the Dark Arts 》 Now we have v3.0 Version of , The bag has gathered 100 Multiple development tips , Very suitable for fragment reading in your spare time .

Python Chinese guide

learn Python The best learning material is always Python Official documents , Unfortunately, most of the official documents now are in English , Although there is a Chinese translation , But the progress is really worrying . In order to take care of students with poor English , I wrote one myself Zero based friends On line Python file -- 《Python Chinese guide 》

** If it helps , Remember to help me Like it ~

https://en.pythonmana.com/2022/01/202201301319050578.html