Since last week, I have been tossing about Vim IDE for Python & Go. I will share the whole construction process and cost articles. This article is a nanny-level tutorial for the Python version. In fact, I also wrote the Go version. Yes, you can like this article if you want to read it, and I will post the next article

效果图

When it comes to IDE, there are always people who are arguing about which one is the best editing tool in the world, but this article will not cover or mislead you. I believe that different people and different usage scenarios are different. There is no one-trick-all editor in the world.

If it is on the desktop, PyCharm and VS Code have done well enough, it is difficult to have a third editor can match it.

But if it is on the server side? There seems to be no choice, Vim is almost your only choice.

Vim is a very productive tool, and even in the eyes of some people, it is a devilish editor. The reason for this is that it has a very high barrier to use and a very steep learning curve.

One is that it is a proprietary compiler for the programmer community. You need to learn and understand its design philosophy, and you need to remember a lot of complex operating instructions.

The second is that for engineering project code, it is not an out-of-the-box editor. You need to install a lot of plug-ins and perform a lot of configuration to become a good IDE tool.

There are four reasons why I use Vim as a development tool:

  1. directly : After the normal local IDE encoding is completed, it is more troublesome to upload to the remote server for compilation and testing. It is more straightforward for me to directly encode on the SSH server.
  2. worry- : For multiple languages, there is no need to install multiple proprietary editors, such as PyCharm, Goland, etc., and there is no need to work hard to crack various paid software.
  3. Convenient : Improve iPad productivity, you can write code online without a computer when you go out, saving you having to bring a heavy computer every time.
  4. pretend to be : Don’t you think it’s cool? (escape...

If you don't know anything about Vim operation, then please learn about the daily use of Vim first, otherwise the following content is not suitable for you.

1. Preparation

This article is a demonstration of the operation in the Mac environment, but it is also applicable to the Linux environment (a little difference, I will point out the corresponding position), if you only have a Windows system, you can use GVim.

Before starting the installation and configuration, let me talk about the overall idea of this article:

  1. Prepare the running environment: install Python or Go environment
  2. Prepare Vim version: use the highest version of Vim 8.2
  3. Plug-in installation environment: Plug-ins are on Github and other external networks, and you need to configure some agents
  4. Plug-in installation: one-click batch installation of plug-ins
  5. Plug-in configuration: After the plug-in is installed, it needs some configuration to be easy to use
  6. Plug-in use: Demonstrate how to use each plug-in

2. Prepare the operating environment

Vim natively supports Python. When you install Vim of version 8.2, Python will be installed automatically, but the installed version is not the version you need, but it doesn't matter, the Python version used by Vim is configurable.

The version I use here is Python 3.10.0

$ python3 --version
Python 3.10.0

3. Install/Upgrade Vim 8.2

Normal Mac or Linux machines will come with Vim tools, but the version may be relatively low. If you use these versions of Vim, some of the later plug-ins will not be installed or can not be used, such as YouCompleteMe a very important plug-in, if you don’t use it Vim 8.1+, you will be prompted every time you use vim, which greatly affects the experience

$ vim main.go
YouCompleteMe unavailable: requires Vim 8.1.2269+.
Press ENTER or type command to continue

These plugins have been continuously updated for many years, and it is understandable that the old version of Vim no longer provides support.

If you use Linux, the whole process will be much smoother. Here I am using CentOS 7.6 Linux.

First find out which vim packages are installed in the system, and then use yum remove to uninstall it

[root@iswbm ~]# yum list installed | grep -i vim
vim-common.x86_64                       2:7.4.629-8.el7_9              @updates
vim-enhanced.x86_64                     2:7.4.629-8.el7_9              @updates
vim-filesystem.x86_64                   2:7.4.629-8.el7_9              @updates
vim-minimal.x86_64                      2:7.4.629-8.el7_9              @updates
[root@iswbm ~]#
[root@iswbm ~]# yum remove vim-common vim-enhanced vim-filesystem vim-minimal

Later I will use source code compilation to install Vim 8.2, but the compilation needs to install the following basic dependencies

[root@iswbm ~]# yum install -y gcc make ncurses ncurses-devel
[root@iswbm ~]# 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

Download the source code from Github

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

Enter the vim/src directory and execute the following three commands to compile and install

[root@iswbm ~]# ./configure --prefix=/usr/local/vim \
--enable-pythoninterp=yes \
--enable-python3interp=yes \
--with-python-command=python \
--with-python3-command=python3
[root@iswbm ~]# make && make install
[root@iswbm ~]# 

Not surprisingly, after the command is executed, you only need to configure a software link, and you can use the 8.2 version of Vim normally.

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

4. Plug-in installation

The improved functions of Vim itself are already very powerful, but it is too difficult to get started. Installing some customized plug-ins can make the management and use of the entire Vim interface more in line with human intuition and lower the barrier to use.

Which plug-ins should be installed, and what you want to make Vim look like?

You don't have to think about it, just copy the homework with the IDE software on the desktop.

For me personally, the features that I use the most IDEs in my daily life are:

  • Automatic code completion
  • Code tracking jump
  • Static code inspection
  • Run debug code
  • Global search code
  • Project code bookmark
  • Code version management
  • Code highlighting
  • Project file tree
  • Single file code structure tree
  • Multiple files can be opened at the same time
  • Markdown real-time preview

Then I can check this function to find the corresponding plug-in

  • YouCompleteMe : Provide automatic code completion and code tracking jump
  • auto-pairs : A plug-in that automatically completes parentheses, including parentheses, square brackets, and curly brackets
  • NERDTree :Provide project file tree, support bookmark function
  • vim-nerdtree-tabs : You can open multiple code files to make nerdtree's tab more friendly
  • nerdtree-git-plugin : You can see the git version information in the navigation directory
  • tagbar : A plug-in that can view the variable and function list in the current code file, and switch and jump to the location of the corresponding variable and function in the code
  • vim-airline : Vim status bar plugin, including display line number, column number, file type, file name, and Git status
  • vim-gitgutter : git information can be displayed in the document
  • vim-one : code color scheme
  • markdown-preview.vim : Markdown preview support
  • mathjax-support-for-mkdp : Markdown math formula preview support
  • vim-godef : code tracking in go, enter gd to automatically jump
  • fatih/vim-go : a series of go related tools such as static inspection
  • ultisnips / vim-snippets : automatically generate code blocks

So how to install these plug-ins?

It's very simple, you just need to use vi in your ~/.vimrc file, paste the following configuration to the end of the file

" 插件开始的位置
call plug#begin('~/.vim/plugged')

" 代码自动完成,安装完插件还需要额外配置才可以使用
Plug 'ycm-core/YouCompleteMe'

" 用来提供一个导航目录的侧边栏
Plug 'scrooloose/nerdtree'

" 可以使 nerdtree 的 tab 更加友好些
Plug 'jistr/vim-nerdtree-tabs'

" 可以在导航目录中看到 git 版本信息
" Plug 'Xuyuanp/nerdtree-git-plugin'

" 查看当前代码文件中的变量和函数列表的插件,
" 可以切换和跳转到代码中对应的变量和函数的位置
" 大纲式导航, Go 需要 https://github.com/jstemmer/gotags 支持
Plug 'preservim/tagbar'

" 自动补全括号的插件,包括小括号,中括号,以及花括号
Plug 'jiangmiao/auto-pairs'

" Vim状态栏插件,包括显示行号,列号,文件类型,文件名,以及Git状态
Plug 'vim-airline/vim-airline'

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
" 可以快速对齐的插件
Plug 'junegunn/vim-easy-align'

" 可以在文档中显示 git 信息
Plug 'airblade/vim-gitgutter'

" markdown 插件
Plug 'iamcco/mathjax-support-for-mkdp'
Plug 'iamcco/markdown-preview.vim'

" 下面两个插件要配合使用,可以自动生成代码块
Plug 'SirVer/ultisnips'
Plug 'honza/vim-snippets'

" go 主要插件
Plug 'fatih/vim-go', { 'tag': '*' }

" go 中的代码追踪,输入 gd 就可以自动跳转
Plug 'dgryski/vim-godef'

" 可以在 vim 中使用 tab 补全
"Plug 'vim-scripts/SuperTab'

" 可以在 vim 中自动完成
"Plug 'Shougo/neocomplete.vim'


" 插件结束的位置,插件全部放在此行上面
call plug#end()

Then enter the command :wq save and exit vi.

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

Vundle is a very well-known and long-established Vim plugin management tool. However, as more and more vim plugins are installed, the efficiency of using Vundle to manage these plugins becomes lower and lower, and the time consuming to start vim is also increasing.

And vim-plug is a very lightweight and efficient vim plug-in management tool. It supports fully asynchronous, multi-threaded parallel installation of plug-ins, supports git branches, tags, etc., can roll back and updates plug-ins, and also supports on-demand loading, and can specify to load corresponding vim plug-ins for specific file types. Greatly speed up vim startup time.

Therefore, I will use the vim-plug management tool here, and use the following command to install the vim-plug plug-in management tool

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

Then please restart your terminal and make sure to re-initialize, otherwise PlugInstall later, it may report that the command does not exist.

Not an editor command: PlugInstall

After restarting the terminal, enter vim enter :PlugInstall start the installation process

If you don't have network problems (it's just a matter of science), then the installation will go smoothly. .

Enter :PlugStatus will see all the plug-ins are installed OK.

5. YouCompleteMe

The above plug-in installation is actually relatively simple, which is to pull the warehouse on Github to the local ~/.vim/plugged directory

Under normal circumstances, these plugins are used out of the box and will not have complicated dependencies, but the only plugin is special, it is YouCompleteMe , which is known as the most difficult Vim plugin to install.

I installed it on my local Mac machine for two nights before I resolved all the dependencies, but it went smoothly on Linux.

The specific installation steps are

  1. Enter the ~/.vim/plugged/YouCompleteMe plug-in directory and modify .gitmodules in 0617e54168bac2 to the mirror website hub.fastgit.org
  2. Then install the first level dependency: git submodule update --init
  3. A dependence properly installed, and then modify third_party/ycmd directory of all dependent .gitmodules in github.com to mirror sites hub.fastgit.org
  4. Then recursively install its dependent package: git submodule update --init --recursive
  5. Finally execute python3 install.py --all , --all will install all language functions supported by the plug-in.

During the installation process on the Mac, I encountered quite a few problems. It also involved changing the YouComplete code. Finally, it was able to install normally. You may also encounter similar problems during the installation process. If you have any problems, welcome Leave a message in the comment area and I will try my best to answer.

6. Set up the mirror proxy

The above process of installing the plug-in will actually go to Github to download the corresponding plug-in, but due to various indescribable reasons, accessing github on the mainland server is very slow, or even inaccessible.

Without any network settings, none of the 20 plug-ins were installed successfully.

So here, you have to figure out a way to make your server accessible to Github at a normal speed. As for how to do it, it is not convenient for me to elaborate on some of the black technologies here. I will introduce you to a public and very effective one. method.

Modify ~/.vim/autoload/plug.vim change

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

Change to

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

Will this line

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

Change to

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

Then enter vim and execute :PlugInstall .

7. Plug-in use

After most of the plug-ins are installed, they can be used immediately, but some plug-ins require some configuration before they can be used more easily.

Because there are so many configurations, I won’t post them directly here. If you are interested, add me v:hello-wbm. Ask me for the configuration table.

YouComplete

The most basic requirement of using an IDE is to be able to automatically give a prompt when you are coding, and then automatically complete it. With the blessing of YouComplete, vim can also restore 100% of the coding experience on the desktop.

NERDTree

After opening the file, use F9 or enter NERDTreeToggle to open the file tree in the sidebar, which is the convenience provided by NERDTree

tagbar

After opening the Python file, use F9 or enter :tagbar to open the tagbar window, in this window you can see all the structures, functions, variables, etc. of the file, which can all be called tags, when you locate a tag When, press Enter to jump to the position of the code window on the left.

vimgrep

Vimgrep can be used for code search of engineering projects. It is an indispensable tool for students who often read the source code. It is a tool that comes with Vim and is very powerful.

After using vimgrep to search, under normal circumstances, there will be no feedback. If you need to view the search results and jump to the corresponding location, you can use QuickFix, as long as you enter :cw or :copen

8. Run the code

After using Vim to write the code, if you want to run the code directly like PyCharm, you need to write the following configuration .vimrc

This configuration includes not only Python, but 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

After configuration, use F5 to run the current script directly.

9. Write code on the iPad

If you, like me, have your own server, then after you configure Vim according to the above steps, you can connect to the server via SSH on the iPad to write code.

If you don't have a server, as long as you can add me v: hello-wbm, I will give you a one-year Aliyun server. The quota is limited. I can only say that it is first come first served.

I happen to have a 2020 iPad Pro in my hand, which is mostly used for video. I am a bit sorry for the Pro configuration. With Vim, the artifact, productivity has increased a little bit. .

10. Write at the end

It is necessary to explain that the reason why it took such a long time for five days is that I actually configured Vim IDE on all of my computers, including the server. Different machines have different problems. Among them, on my Mac, I encountered the most problems and took the longest time. Some of these problems were not answered by Google. Finally, I read the code and modified part of the code before I ran.

In addition, for Vim, the most important thing is the .vimrc file. In the above explanation, I may have missed some configuration explanations. If you find that the use is not so smooth, you can download my .vimrc file: https://wwe. lanzoui.com/i9gD5vrzufi

This article is the Python version of the Vim IDE building guide. The code demonstration basically uses Python code. According to my thoughts in the article step by step, you can build your own set of online IDE environment.

I not only write Python code, but also some Go code. Vim provides more native support for Python. In contrast, Go has to install more plug-ins to achieve a good coding experience, but due to its large number Most of them are Python developers. In this part, I will write another Vim for Go article. Interested friends can tell me in the comment area, and I will send you the address.

Well, the above is the entire content of this article. If you have any questions about the installation and configuration, please feel free to point it out in the comment section~

The original text was first published on personal blog: https://iswbm.com/591.html

At the end of the article, we will insert a benefit

Double Eleven is coming soon, and Alibaba Cloud is also starting to engage in activities. It just so happens that I can take you to Piao Alibaba Cloud's server.

To put it bluntly, everyone can get a server without spending a penny. The specification is a 2c2m (2vcpu 2G memory) machine.

Yesterday, I posted it in the circle of friends. Now 400 people have signed up to participate. I will borrow this article today to talk about it. If you want to participate, you can add my v (hello-wbm) to bring everyone together.

Talk a bit

I have written a lot of Python-related articles on SegmentFault, including Python utility tools, Python high-efficiency skills, and PyCharm usage skills. I am very happy to get the recognition and support of many friends who know.

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

PyCharm Chinese Guide

"PyCharm Chinese Guide" uses more than 300 GIF dynamic images to explain in detail 105 PyCharm efficient use skills that are most suitable for actual development. The content is easy to understand and suitable for all Python developers.

Online experience address: https://pycharm.iswbm.com

Python Black Magic Guide

"Python Black Magic Guide" currently ushered in the v3.0 version, which contains more than 100 development tips, which is very suitable for reading fragments in leisure time.

Online experience address: https://magic.iswbm.com

Python Chinese Guide

The best learning material for learning Python is always the official Python documentation. Unfortunately, most of the official documentation is now in English. Although there is a Chinese translation, the progress is really worrying. In order to take care of classmates who are not good at English, I wrote an online Python document for friends with zero foundation - "Python Chinese Guide"

Online experience address: https://python.iswbm.com

It is helpful, remember to help point a praise yo ~


Python编程时光
135 声望26 粉丝