2014-01-10 Python on vim
[TOC]

modeline ##

You can use modeline in python source file to change Vim's setting. Vim will read top and bottom 3 lines comments and use the comment start with vim: as modeline setting. By do this, opener can use same setting to edit this file. This can avoid mixing of space and tab char in source code.
Add the following line to the top or bottom of your python source file.
1
#vim: ai ts=4 sts=4 et sw=4 ft=python
Copied!

.vimrc ##

I am using following settings in my ~/.vimrc for python
1
" Automatic reloading of .vimrc
2
autocmd! bufwritepost .vimrc source %
3
4
" Better copy & paste
5
" When you want to paste large blocks of code into vim, press F2 before you
6
" paste. At the bottom you should see ``-- INSERT (paste) --``.
7
8
"" set pastetoggle=<F2>
9
"" set clipboard=unnamed
10
11
" Mouse and backspace
12
"set mouse=a " on OSX press ALT and click
13
set bs=2 " make backspace behave like normal again
14
15
" Rebind <Leader> key
16
" I like to have it here becuase it is easier to reach than the default and
17
" it is next to ``m`` and ``n`` which I use for navigating between tabs.
18
let mapleader = ","
19
20
" Bind nohl
21
" Removes highlight of your last search
22
" ``<C>`` stands for ``CTRL`` and therefore ``<C-n>`` stands for ``CTRL+n``
23
noremap <C-n> :nohl<CR>
24
vnoremap <C-n> :nohl<CR>
25
inoremap <C-n> :nohl<CR>
26
27
" Quicksave command
28
"" noremap <C-Z> :update<CR>
29
"" vnoremap <C-Z> <C-C>:update<CR>
30
"" inoremap <C-Z> <C-O>:update<CR>
31
32
" Quick quit command
33
"" noremap <Leader>e :quit<CR> " Quit current window
34
"" noremap <Leader>E :qa!<CR> " Quit all windows
35
36
" bind Ctrl+<movement> keys to move around the windows, instead of using Ctrl+w + <movement>
37
" Every unnecessary keystroke that can be saved is good for your health :)
38
map <c-j> <c-w>j
39
map <c-k> <c-w>k
40
map <c-l> <c-w>l
41
map <c-h> <c-w>h
42
43
" easier moving between tabs
44
map <Leader>n <esc>:tabprevious<CR>
45
map <Leader>m <esc>:tabnext<CR>
46
47
" map sort function to a key
48
vnoremap <Leader>s :sort<CR>
49
50
" easier moving of code blocks
51
" Try to go into visual mode (v), thenselect several lines of code here and
52
" then press ``>`` several times.
53
"" vnoremap < <gv " better indentation
54
"" vnoremap > >gv " better indentation
55
56
" Show whitespace
57
" MUST be inserted BEFORE the colorscheme command
58
"" autocmd ColorScheme * highlight ExtraWhitespace ctermbg=red guibg=red
59
"" au InsertLeave * match ExtraWhitespace /\s\+$/
60
61
" Color scheme
62
" mkdir -p ~/.vim/colors && cd ~/.vim/colors
63
" wget -O wombat256mod.vim http://www.vim.org/scripts/download_script.php?src_id=13400
64
set t_Co=256
65
color wombat256mod
66
67
" Enable syntax highlighting
68
" You need to reload this file for the change to apply
69
filetype off
70
filetype plugin indent on
71
syntax on
72
73
" Showing line numbers and length
74
set number " show line numbers
75
set tw=79 " width of document (used by gd)
76
set nowrap " don't automatically wrap on load
77
set fo-=t " don't automatically wrap text when typing
78
if version > 703
79
set colorcolumn=80
80
endif
81
highlight ColorColumn ctermbg=233
82
83
" easier formatting of paragraphs
84
vmap Q gq
85
nmap Q gqap
86
87
" Useful settings
88
set history=700
89
set undolevels=700
90
91
" Real programmers don't use TABs but spaces
92
set tabstop=4
93
set softtabstop=4
94
set shiftwidth=4
95
set shiftround
96
set expandtab
97
98
" Make search case insensitive
99
set hlsearch
100
set incsearch
101
set ignorecase
102
set smartcase
103
104
" Disable stupid backup and swap files - they trigger too many events
105
" for file system watchers
106
set nobackup
107
set nowritebackup
108
set noswapfile
109
110
" Setup Pathogen to manage your plugins
111
" mkdir -p ~/.vim/autoload ~/.vim/bundle
112
" curl -so ~/.vim/autoload/pathogen.vim https://raw.github.com/tpope/vim-pathogen/HEAD/autoload/pathogen.vim
113
" Now you can install any plugin into a .vim/bundle/plugin-name/ folder
114
call pathogen#infect()
115
116
" ============================================================================
117
" Python IDE Setup
118
" ============================================================================
119
120
" Settings for vim-powerline
121
" cd ~/.vim/bundle
122
" git clone git://github.com/Lokaltog/vim-powerline.git
123
set laststatus=2
124
125
" Settings for ctrlp
126
" cd ~/.vim/bundle
127
" git clone https://github.com/kien/ctrlp.vim.git
128
let g:ctrlp_max_height = 30
129
set wildignore+=*.pyc
130
set wildignore+=*_build/*
131
set wildignore+=*/coverage/*
132
133
" Settings for jedi-vim
134
" sudo easy_install jedi
135
" cd ~/.vim/bundle
136
" git clone git://github.com/davidhalter/jedi-vim.git
137
let g:jedi#related_names_command = "<leader>z"
138
let g:jedi#popup_on_dot = 0
139
let g:jedi#popup_select_first = 0
140
map <Leader>b Oimport ipdb; ipdb.set_trace() # BREAKPOINT<C-c>
141
142
" Better navigating through omnicomplete option list
143
" See http://stackoverflow.com/questions/2170023/how-to-map-keys-for-popup-menu-in-vim
144
set completeopt=longest,menuone
145
function! OmniPopup(action)
146
if pumvisible()
147
if a:action == 'j'
148
return "\<C-N>"
149
elseif a:action == 'k'
150
return "\<C-P>"
151
endif
152
endif
153
return a:action
154
endfunction
155
156
inoremap <silent><C-j> <C-R>=OmniPopup('j')<CR>
157
inoremap <silent><C-k> <C-R>=OmniPopup('k')<CR>
158
159
" Python folding
160
" mkdir -p ~/.vim/ftplugin
161
" wget -O ~/.vim/ftplugin/python_editing.vim http://www.vim.org/scripts/download_script.php?src_id=5492
162
set nofoldenable
Copied!
最近更新 1yr ago
复制链接