Use vim to create your own python editor and vimpython Editor

Source: Internet
Author: User
Tags virtual environment

Use vim to create your own python editor and vimpython Editor
Basic Configuration

The vim configuration is in the user's main directory.~ /. VimrcFile. If not, you need to create it yourself:

cd ~touch .vimrc

First, do some simple configuration:

Set nocompatible "Disable compatibility with vi Mode set number" display row number set nowrap "not auto-folding set showmatch" show matching brackets set scroloff = 3 "3 rows from top and bottom" set encoding = UTF-8 "encoding set fenc = UTF-8" encoding set mouse = a "enable mouse set hlsearch" Search highlight syntax on "syntax highlight

Add pep8-style configurations for the py file:

Au BufNewFile, BufRead *. py \ set tabstop = 4 "tab width \ set softtabstop = 4 \ set shiftwidth = 4 \ set textwidth = 79" maximum row width \ set expandtab "tab is replaced by Space key \ set autoindent" auto indent \ set fileformat = unix "Save File Format
Split Window

Vim can open multiple files during editing:

: Vs or: vsplitSplits the current window vertically and displays the current file in the new window above.

: Vs filenameSplits the current window vertically, and the new file is displayed in the new window.

: Sp or: sv or: splitSplits the current window horizontally and displays the current file in the new window on the left.

: Sp filenameSplits the current window vertically. New files are displayed in the new window on the left.

: NewCreate a new file and split it vertically

: VnewCreate and split files horizontally

If you want to open a new window on the right or below, add the Configuration:

set splitbelowset splitright

You can use the mouse to switch between windows. If you do not want to use the mouse, the switch button is as follows:

  • Ctrl-w-jSwitch to the split window below
  • Ctrl-w-kSwitch to the split window above
  • Ctrl-w-lSwitch to the split window on the right
  • Ctrl-w-hSwitch to the left-side split window

If you think there are more than three buttons, you can set the shortcut key:

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>

In this way, you do not need to press the w key.

Code folding

Code folding is required when there are many lines of code:

set foldmethod=indentset foldlevel=99

Use the zc button to create a fold, and use za to open or close the fold.

Za is often mistakenly entered. You can use the space key to replace za:

nnoremap <space> za
One-click execution of python code

To execute python code directly in vim, you can add (from the https://www.zhihu.com/question/20271508 ):

map <F5> :call RunPython()<CR>func! RunPython()    exec "W"    if &filetype == 'python'        exec "!time python2.7 %"    endifendfunc

In this way, press F5 to run the python code automatically.

Plug-ins

Vundle is the most important plug-in vim. vundle is used to manage other plug-ins of vim.

Vundle

Vundle is short for Vim bundle. It uses git to manage the vim plug-in. With it, it is much easier to install other plug-ins.

Project address: https://github.com/vundlevim/vundle.vim.

First download the source code:

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

If ~ The/. vim/bundle directory does not exist. Create a directory:

cd ~mkdir .vimcd .vimmkdir bundle

Put the following configuration at the beginning of the. vimrc file:

set nocompatible              " be iMproved, requiredfiletype off                  " required" set the runtime path to include Vundle and initializeset rtp+=~/.vim/bundle/Vundle.vimcall vundle#begin()" let Vundle manage Vundle, requiredPlugin 'VundleVim/Vundle.vim'" All of your Plugins must be added before the following linecall vundle#end()            " requiredfiletype plugin indent on    " required

If you want to download a plug-in, such as automatically indent the indentpython. vim plug-in, you need

Plugin 'vim-scripts/indentpython.vim'

Place it between call vundle # begin () and call vundle # end (). Save the configuration and execute it in vim.

:PluginInstall

You can automatically download the indentpython. vim plug-in.

Bundle can manage and download several different plug-ins:

The plug-in Plug-in on github 'tpope/vim-fugitive 'is from the git plug-ins 'git: // git.wincent.com/command-t.git'plug-In 'file: /// home/gmarik/path/to/plugin '"The sparkup vim script is in a subdirectory of this repo called vim. "Pass the path to set the runtimepath properly. "Plugin 'rstacruz/sparkup', {'rtp ': 'vim/'} if there is an old plug-in, download the new plug-in and rename it to avoid conflict with the Plugin 'ascenator/v2 ', {'name': 'newd9 '}

In addition to running: PluginInstall in vim, you can also run:

vim +PluginInstall +qall

Other common commands:

:PluginList       - lists configured plugins:PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate:PluginSearch foo - searches for foo; append `!` to refresh local cache:PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
YouCompleteMe

Very useful Automatic completion plug-ins, that is, relatively heavy.

Address: http://valloric.github.io/YouCompleteMe/

Github address: https://github.com/Valloric/YouCompleteMe

After you install YouCompleteMe, You need to manually compile it and then configure it in. vimrc.

For use in ubuntu, first prepare some tools:

sudo apt-get install build-essential cmake
sudo apt-get install python-dev python3-dev

Install with vundle:

Plugin 'Valloric/YouCompleteMe'

Compile:

cd ~/.vim/bundle/YouCompleteMe./install.py --clang-completer

The -- clang-completer parameter is used to add the Automatic completion of the C series language. It can also be left blank:

cd ~/.vim/bundle/YouCompleteMe./install.py

Please wait. It will take a long time...

Copy the default configuration file to your home directory:

cp third_party/ycmd/examples/.ycm_extra_conf.py ~/

Some common options of YCM can be adjusted according to your preferences:

Let g: ycm_min_num_of_chars_for_completion = 2 "number of characters to complete" let g: ycm_python_binary_path = 'python' "Path of the python interpreter where the jedi module is located" let g: ycm_seed_identifiers_with_syntax = 1 "enable keyword query for languages" let g: ycm_autoclose_preview_window_after_completion = 1 "automatically closes the preview window after completion"

Switch YCM:

let g:ycm_auto_trigger = 0   "turn offlet g:ycm_auto_trigger = 1   "turn on
Support for vim8 completion plug-in

YouCompleteMe actually uses jedi-vim to complete python code. If you think YCM is too heavy, you can use maralla/completor. vim that supports vim8 to complete the Code:

Download:

Plugin 'maralla/completor.vim'

Download jedi:

pip install jedi

Configuration:

let g:completor_python_binary = '/path/to/python/with/jedi/installed'

Setting is much easier than YCM.

Automatic indent plug-in

To write python code, automatic indentation is required. You can use the indentpython. vim plug-in:

Plugin 'vim-scripts/indentpython.vim'
Syntax check

Install the syntastic plug-in. Vim checks the code syntax every time it saves the file:

Plugin 'vim-syntastic/syntastic'

Add the flake8 code style check:

Plugin 'nvie/vim-flake8'

Run F7 to perform the flake8 check.

Color Scheme

The solarized color scheme has been popular for a long time. The github address is https://github.com/altercation/vim-colors-solarized.

Manual download:

$ cd ~/.vim/bundle$ git clone git://github.com/altercation/vim-colors-solarized.git$ mv vim-colors-solarized ~/.vim/bundle/

Or download vundle:

Plugin 'altercation/vim-colors-solarized'

Solarized has two colors: dark and light. Configuration:

syntax enableset background=light or darkcolorscheme solarized

You can also switch between the gui mode and terminal mode:

if has('gui_running')    set background=lightelse    set background=darkendif

Another color-based Zenburn solution:

Plugin 'jnurmine/Zenburn'

Two color schemes:

if has('gui_running')  set background=dark  colorscheme solarizedelse  colorscheme Zenburnendif
Nerdtree

Add a tree directory to vim at https://github.com/scrooloose/nerdtree.

Download:

Plugin 'scrooloose/nerdtree'

Add the shortcut key to switch the tree directory:

map <C-n> :NERDTreeToggle<CR>

Ctrl + n to enable the directory.

Set to ignore the. pyc file:

let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']

Add git support for nerdtree:

Plugin 'Xuyuanp/nerdtree-git-plugin'

If you want to use the tab key:

Plugin 'jistr/vim-nerdtree-tabs'
Vim-powerline

Beautify the status bar to display information such as the current virtual environment, Git branch, and files being edited.

Plugin 'Lokaltog/vim-powerline'

IndentLine

Indent indicator line at https://github.com/yggdroot/indentline.

Installation:

Plugin 'Yggdroot/indentLine'

Python uses code indentation to determine the code block. indentation is very convenient.

Vim-autopep8

The automatic formatting tool runs after installation: Autopep8 can automatically format the code according to pep8 standards.

Address: https://github.com/yggdroot/indentline.

First install autopep8:

$ pip install autopep8
Plugin 'tell-k/vim-autopep8'

You can set the shortcut key F8 instead of Autopep8:

autocmd FileType python noremap <buffer> <F8> :call Autopep8()<CR>
Auto-pairs

The address is https://github.com/jiangmiao/auto-pairs.

Plugin 'jiangmiao/auto-pairs'
Ctrlp. vim

Search for the plug-in. In vim normal mode, press ctrl + p and enter the file you are looking.

Address: https://github.com/kien/ctrlp.vim.

Plugin 'kien/ctrlp.vim'
Vim-fugitive

Git integration plugin. You can run the git command in vim at https://github.com/tpope/vim-fugitive.

Plugin 'tpope/vim-fugitive'

  

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.