使用vim编写python

学习python过程中使用了idle、Pycharm,始终不顺手。vim或emacs是终极目标,本文记录一下vim编写python的配置过程。

修改vim配置文件增强python编写

ubuntu vim 配置文件位置在/etc/vim/vimrc,修改此文件即可。我做了两个修改,1是不退出vim运行脚本,2是显示行号、高亮等。vimrc文件增加内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
""""""""""""""""""""""
"Quickly Run
""""""""""""""""""""""
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'c'
exec "!g++ % -o %<"
exec "!time ./%<"
elseif &filetype == 'cpp'
exec "!g++ % -o %<"
exec "!time ./%<"
elseif &filetype == 'java'
exec "!javac %"
exec "!time java %<"
elseif &filetype == 'sh'
:!time bash %
elseif &filetype == 'python'
exec "!time python2.7 %"
elseif &filetype == 'html'
exec "!firefox % &"
elseif &filetype == 'go'
" exec "!go build %<"
exec "!time go run %"
elseif &filetype == 'mkd'
exec "!~/.vim/markdown.pl % > %.html &"
exec "!firefox %.html &"
endif
endfunc

" enable syntax highlighting
syntax enable

" show line numbers
set number

" set tabs to have 4 spaces
set ts=4

" indent when moving to the next line while writing code
set autoindent

" expand tabs into spaces
set expandtab

" when using the >> or << commands, shift lines by 4 spaces
set shiftwidth=4

" show a visual line under the cursor's current line
set cursorline

" show the matching part of the pair for [] {} and ()
set showmatch

" enable all Python syntax highlighting features
let python_highlight_all = 1

安装vim-python mode,集成python编写、运行、调试、检查的插件。

安装pathogen

1
2
mkdir -p ~/.vim/autoload ~/.vim/bundle
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

安装pymode

  • 开启pathogen vi ~/.vimrc 添加内容

    1
    2
    3
    4
    5
    6
    " Pathogen load
    filetype off
    call pathogen#infect()
    call pathogen#helptags()
    syntax on
    filetype plugin indent on
  • 拷贝文件

    1
    2
    3
    git clone https://github.com/python-mode/python-mode.git
    cd python-mode
    cp -R * ~/.vim
  • 编辑helptags,打开vim,命令模式下执行

    1
    :helptags ~/.vim/doc/

参考链接

https://www.zhihu.com/question/19655689
https://www.fullstackpython.com/vim.html