Share an article that’s been messing with VIM lately, configuring VIM as a handy Lightweight IDE for Python.
There are already plenty of good vim tutorials on the web. But everyone has their own habits, so the tutorial is for reference only, only in the process of using to experience the specific problems, and then solve the problem to viM teaching smoothly. This article will skip over the vim operations and start directly with configuration.
I. Configuration files
- Basic configuration
All configuration for vim is done in the ~/.vimrc file. Default is no need to create their own:
cd ~
touch .vimrc
vim .vimrc
Copy the code
Here is the basic configuration of my own.vimrc file, with comments in double quotes:
"Set nocompatible" display line number "set number" hide scrollbar "set guiOptions -=r set guiOptions -=L set guiOptions -=b" Hide top TAB bar" Set showtabline=0 "set guifont=Monaco:h13 syntax on" let g:solarized_termcolors=256 "Solarized theme set under terminal setting" Set background=dark "Set background color" colorScheme solarized set nowrap "Set not to fold" set FileFormat = Unix "Set to save files in Unix format" set cindent "Set c-style indentation format" Set tabStop =4 "Set table length" Set Shiftwidth =4 "same as above" set showMatch "display matching brackets" set scrolloff=5 "5 lines from top and bottom" set Laststatus =2 "set fenc= UTF-8" file encoding "set backspace=2 set mouse=a" Enable mouse "set Selection =exclusive set Selectmode =mouse,key set matchtime=5 set ignorecase "ignorecase" set incsearch set hlsearch "highlight search item" set noexpandtab "Do not allow to extend the table" set whichwrap+=<,>,h, L set autoread set cursorline"Copy the code
- One-click execution of Python code
Other popular editors have handy one-click functionality or plug-ins, such as Command + B for Sublime Text and Script for Atom. Vim does this by adding the following code to.vimrc to enable F5 to execute Python code in one click, and since vim’s Quickfix function is called, it also supports jumping to the error line if an error occurs.
Map <F5> :Autopep8<CR> :w<CR> :call RunPython()<CR> function RunPython() let mp = &makeprg let ef = &errorformat let exeFile = expand("%:t") setlocal makeprg=python\ -u set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m silent make % copen let &makeprg = mp let &errorformat = ef endfunctionCopy the code
However, there is a minor problem with this one-click execution. Due to Quickfix, standard input() and raw_input() are not supported. As long as these two functions are present in the code, they are 100% stuck. Input can be supported without QuickFix, but when used in terminals, it jumps back to terminal input and output; If you use Macvim, there will be a lot of output data and you can’t see all of it. Since I use less standard input when writing Python, I decided to use Quickfix for practical and aesthetic reasons.
Second, the plug-in
This article focuses on Vundle and YouCompleteMe because they are a bit more complicated to install than the others, which are almost dummies:
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
Plugin 'Lokaltog/vim-powerline'
Plugin 'scrooloose/nerdtree'
Plugin 'Yggdroot/indentLine'
Plugin 'jiangmiao/auto-pairs'
Plugin 'tell-k/vim-autopep8'
Plugin 'scrooloose/nerdcommenter'
Copy the code
Vundle
Killer plugin. This is the only plug-in that needs to be installed manually. With this, installing other plug-ins is much easier.
- 1, install the project first need to get the source code from Github, in the terminal command as follows (please prepare git) :
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Copy the code
If the reminder directory does not exist, please create a new directory first:
cd ~
mkdir .vim
cd .vim
mkdir bundle
Copy the code
Then you need to add some configuration to the.vimrc file to take effect:
Filetype off set RTP +=~/.vim/bundle/ vundle. vim call Vundle #begin() Plugin 'VundleVim/ vundle. vim' Plugin 'your Plugin' call vundle#end() filetype plugin indent onCopy the code
- 2. Using Vundle to install the plug-in is very simple, only takes two steps. First add the Plugin command to the.vimrc file:
Plugin 'Lokaltog/vim-powerline'Copy the code
Here is how to install a plug-in on vim-script, with the name of the plug-in in quotes. For other plug-in installation methods, please refer to the project home page:
Plugin 'L9'
Copy the code
After adding. Vimrc configuration, :wq save and exit vim, restart vim and enter the following command, vim will automatically download and install the plug-in
:PluginInstall
Copy the code
When you see the command line appear Done! This means that all plug-ins are installed!
YouCompleteMe
Killer plugin. Vim offers an IDE-like auto-complete experience and works with Syntastic to provide real-time grammar checking. YouCompleteMe is different from other plug-ins in that it needs to be installed, compiled manually, and configured in.vimrc.
YouCompleteMe supports the following languages:
C, objectiv-C, C#, C++, objectiv-C ++, Php, Javascript, Typescript, Python, Go, Rust
- Installation requires only one command:
Plugin 'Valloric/YouCompleteMe'
Copy the code
- Compiled commands:
cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer
Copy the code
The — Clang-completer parameter is used to add C series language auto-completion, if not necessary, it can not be added, it also supports completion of other languages, see details
The official documentation.
- There are a lot of configuration options involved with YouCompleteMe that I don’t fully understand, and I personally find Syntactic’s prompts slightly ugly, so I don’t install them. This refers to the list of the author’s use, please check the official document for details:
"Default configuration file path" let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' "No longer asks if ycm_extra_conf.py is loaded when opening vim" let G: ycm_confirm_EXTRA_conf =0 set Completeopt =longest,menu "Python interpreter path" let G: ycm_path_to_PYTHon_interpreter ='/usr/local/bin/python' "Let g: ycm_seed_identifierS_with_syntax =1 Let g:ycm_complete_in_comments=1 let g: ycm_collect_identifierS_from_COMMENTs_and_strings = 0 "Number of characters to complete" Let g:ycm_min_num_of_chars_for_completion=2 "Let g:ycm_autoclose_preview_window_after_completion=1" "Let g: ycm_cache_OMnifunc =0" is also enabled in the string "let g:ycm_complete_in_strings = 1" automatically closes the preview window after leaving insertion mode "autocmd InsertLeave * if pumvisible () = = 0 | pclose | endif "enter or select the current item" inoremap < expr > < CR > pumvisible ()? '< C - > y', '\ < CR >' "up and Down or so key behavior" inoremap < expr > < Down > pumvisible ()? '\<C-n>' : '\<Down>' inoremap <expr> <Up> pumvisible() ? '\<C-p>' : '\<Up>' inoremap <expr> <PageDown> pumvisible() ? '\<PageDown>\<C-p>\<C-n>' : '\<PageDown>' inoremap <expr> <PageUp> pumvisible() ? '\<PageUp>\<C-p>\<C-n>' : '\<PageUp>'Copy the code
vim-powerline
For beautifying the status bar, the original status bar is ugly and displays very little information, so this beautiful and useful status bar comes in handy. Out of the box, easy and fast.
As for the triangle effect on the official website, additional fonts need to be installed. The author said that the configuration has not been successful for a long time, and this does not affect the use of visual effect is also good, so I am not bothered to do this.
NERDTree
Add a tree directory to Vim to make it easier to view and switch files.
- .vIMRC configuration:
"F2 Open and close tree "map <F2> :NERDTreeToggle<CR> let NERDTreeChDirMode=1" Display bookmarks "let NERDTreeShowBookmarks=1" Set ignore file type "let NERDTreeIgnore = [' \ ~ $', '\. Pyc $',' \. SWP $] "window size" let NERDTreeWinSize = 25Copy the code
indentLine,vim-autopep8
IndentLine indentLine is an indent indicator line made of pure characters. Since Python judges code blocks by indentation, it’s easy to see how handy indentation instructions are. IndentLine is turned off by default, so it needs to be configured in.vimrc to see the effect.
Autopep8 is an automatic formatting tool. After installation, enter autopep8 or press F8 in Normal mode to automatically format codes according topep8 standards.
The following is a personal configuration for them, please refer to the project home page for other configurations:
"Indent Indicator line" Let G :indentLine_char=' 酶 'Let G :indentLine_enabled =1 "autopep8 Setting "Let G: autopep8_DISABLE_show_diff =1Copy the code
auto-pairs,nerdcommenter
These two plug-ins make writing code much easier. Auto-pairs auto-completes parentheses and quotation marks. Enter the left quotation mark and the other half of the left parentheses.
Nerdcommenter is the same author as NERDTree, and its main function is to facilitate the writing of comments. Please refer to the project home page for details. Note that the nerdcommenter leader defaults to “\”, which can be changed with the following command:
let mapleader=','
Copy the code
General ides and many editors provide command+/ to quickly add (and uncomment) comments, but Vim requires different shortcuts to add and uncomment, and multi-line comments are just a bit of a teacup. To make one-click comments and de-comments, add a setting to the.vimrc file:
map <F4> <leader>ci <CR>
Copy the code
Multi-line comments simply press V in Normal mode and move the cursor to select the line to comment, then press F4 to comment all selected lines.
conclusion
At this point, all configuration is complete and you are ready to have fun with Python using Vim.
The level is limited, if there are mistakes, welcome to correct.
If you want to learn more about learning Python, you can take a look at the hundreds of points we spent over a month compiling over hundreds of hours:
Python Automatic Development from beginner to Master basic Python tutorial notes