Reprint: Jane book vim environment configuration author Fromradio left as a memo for infringement please contact delete

Source: Internet
Author: User
Tags sublime text git commands

Create a Vim environment for Python by fromradio 2016.08.04 11: 36 * Word Count 1766 Read 504 Comments 0 Likes 12
Most Python users may be accustomed to using notebook editors such as Sublime Text 3. However, in the actual working environment, you may encounter situations that require working in a GUI-free environment, such as logging in to a virtual machine for debugging. At this time vim has become a commonly used editor choice, so how to make vim handy becomes an important issue.

The first problem is the vim version. The default vim of the red hat 6 version in the author's working environment is 7.2, which cannot support the YouCompleMe plug-in that will be used later. Therefore, we need to install vim7.4 version. First uninstall the current vim

yum erase vim-common
This will be uninstalled along with the relevant vim-enhance, and then go to download the relevant rpm of vim7.4:

vim-filesystem: http://rpm.pbone.net/index.php3/stat/4/idpl/30823637/dir/scientific_linux_6/com/vim-filesystem-7.4.629-5.el6.x86_64.rpm.html
vim-common: http://rpm.pbone.net/index.php3/stat/4/idpl/30823635/dir/scientific_linux_6/com/vim-common-7.4.629-5.el6.x86_64.rpm.html
vim-enhanced: http://rpm.pbone.net/index.php3/stat/4/idpl/31369581/dir/centos_6/com/vim-enhanced-7.4.629-5.el6.x86_64.rpm.html
Use after uploading to the server

yum install -y
The instructions are to install three packages, and the vim version is now 7.4.


vim interface.png
Now start to configure the required vim plugin, check if vim supports Python, enter in vim environment

: echo has (‘python‘) || has (‘python3‘)
If the result is 1, it indicates support, otherwise you need to check whether the current Python is installed correctly.

Vim is very extensible, so many developers have developed a lot of small tools to facilitate their own code writing, making vim work like a modern ide. So, first of all we need a plugin manager.

Vundle
Vim has some plug-in managers. Vundle is recommended as a plug-in manager. Think of it as a pip package manager in the middle of Python, making package installation and updating easier.

Use the following command to install:

git clone https://github.com/gmarik/Vundle.vim.git ~ / .vim / bundle / Vundle.vim
This command downloads the Vundle plugin manager to vim's bundles directory. Now you can extend vim by editing the ~ / .vimrc configuration file. (Usually use the user's configuration file, the entire vim configuration file is in the directory / etc / vimrc)

First create this file

touch ~ / .vimrc
Then edit to configure, the following are some necessary configuration items

"Required (Since the Jianshu does not support the vim environment, a double quote will be added at the end of the comment, and the real environment only needs the beginning of the sentence)"
set nocompatible
filetype off

"Add Vundle to the run directory"
set rtp + = ~ / .vim / bundle / Vundle.vim
call vundle # begin ()

"First let Vundle manage Vundle, must"
Plugin ‘gmarik / Vundle.vim’

"Add the plugins you need here"

call vundle # end ()
filetype plugin indent on
Then save and close the configuration file, open a file with vim, enter the command

: PluginInstall
This is the plugin that Vundle will automatically download, install, and update all configuration files.

Enrich vim functions Learn vim components
One of the most important features of vim is to completely abandon the mouse, so there are many shortcut keys to help programmers quickly write code, so the first step is to become familiar with key combinations.

Split the current window
Using the command: sv <filename> will split the current window horizontally and open the new file below the current file. Similarly, using the command: vs <filename> will split the window vertically and open the new file on the right.

The shortcut key is Ctrl-w + Ctrl-j, k, l, h by default, I remap it to the case without Ctrl-w

nnoremap <C-J> <C-W> <C-J>
nnoremap <C-K> <C-W> <C-K>
nnoremap <C-L> <C-W> <C-L>
nnoremap <C-H> <C-W> <C-H>
The shortcut key becomes:

Ctrl-j jump to the lower window
Ctrl-k jump to the upper window
Ctrl-l jump to the right window
Ctrl-h jump to the left window
Code folding
Most IDEs support user folding functions or class definitions, first enable this function

"Open folding function"
set foldmethod = indent
set foldlevel = 99
The folded shortcut key is za. If you find it inconvenient, you can map it to other key positions.

Note that the folding function that comes with vim is based on indentation. Sometimes it is not so easy to use. It is recommended to use the plug-in SimpylFold.

Plugin ‘tmhedberg / SimpylFold’
Python indent
In the implementation part, we hope to have two points, one is to comply with the PEP8 standard, and the other is to better automatically indent. Add in vimrc:

au BufNewFile, BufRead * .py
        \ set tabstop = 4 |
        \ set tabstop = 4 |
        \ set softtabstop = 4 |
        \ set shiftwidth = 4 |
        \ set textwidth = 79 |
        \ set expandtab |
        \ set autoindent |
        \ set fileformat = unix
This allows the indentation of * .py files to be standard four spaces, with a single line no longer than 80 characters, and the file is stored in unix format without conversion problems.

For other files, we can define the format, for example, two spaces in JavaScript are used for indentation, so there is

au BufNewFile, BufRead * .js, * .html, * .css
        \ set tabstop = 2 |
        \ set softtabstop = 2 |
        \ set shiftwidth = 2
Auto indent function we use plugin indentpython.vim

Plugin ‘vim-scripts / indentpython`
Mark unnecessary spaces
Sometimes we need to avoid some extra spaces, hoping that the editor can display some of them, first install the plugin vim-bad-whitespace

Plugin ‘bitc / vim-bad-whitespace’
Mark what is considered a bad space

au BufRead, BufNewFile * .py, *. pyw, *. c, *. h match BadWhitespace / \ s \ + $ /
UTF8 support
This Chinese is more important when editing, add

set encoding = utf-8
Grammar checking and highlighting
Install grammar detection plugin and PEP8 check tool

Plugin ‘scrooloose / syntastic’
Plugin ‘nvie / vim-flake8’
Turn on Python code highlighting strategy

let python_highlight_all = 1
syntax on
Color scheme
Here I use solarized and Zenburn

Plugin ‘jnurmine / Zenburn’
Plugin ‘altercation / vim-colors-solarized’
Super search
The ctrlP tool can help users search almost everything in the vim environment

Plugin ‘kien / ctrlp.vim’
Now you can use the shortcut key Ctrl-p to start the search, which will return you the most similar to your input, and the tool will also search for the mark.

Git integration
The plug-in vim-fugitive helps users to execute git commands in the vim environment, see VIMcasts for details.

to sum up
I have been testing for a better online code editing environment for a few days in the past few days. The current set of processes is the one used now, which is easier to use, but due to various problems with online machines, the best A plugin YouCompletMe was not successfully installed, and it was temporarily abandoned. Interested readers must install this plugin on their own machines, which is quite easy to use.

Resource collection
VIM Tutor
VIMcasts
Official Vim docs
Open Vim
Learn Vimscript the Hard Way
Reprinted: brief book vim environment configuration Author fromradio Leave for memo If infringement please contact to delete

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.