# 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.

#vim: ai ts=4 sts=4 et sw=4 ft=python

## .vimrc ##

I am using following settings in my ~/.vimrc for python

autocmd! bufwritepost .vimrc source %

" Better copy & paste
" When you want to paste large blocks of code into vim, press F2 before you
" paste. At the bottom you should see -- INSERT (paste) --.

"" set pastetoggle=
"" set clipboard=unnamed

" Mouse and backspace
"set mouse=a " on OSX press ALT and click
set bs=2 " make backspace behave like normal again

" Rebind key
" I like to have it here becuase it is easier to reach than the default and
" it is next to m and n which I use for navigating between tabs.

" Bind nohl
" Removes highlight of your last search
"  stands for CTRL and therefore  stands for CTRL+n
noremap :nohl
vnoremap :nohl
inoremap :nohl

" Quicksave command
"" noremap :update
"" vnoremap :update
"" inoremap :update

" Quick quit command
"" noremap e :quit " Quit current window
"" noremap E :qa! " Quit all windows

" bind Ctrl+ keys to move around the windows, instead of using Ctrl+w +
" Every unnecessary keystroke that can be saved is good for your health :)
map j
map k
map l
map h

" easier moving between tabs
map n :tabprevious
map m :tabnext

" map sort function to a key
vnoremap s :sort

" easier moving of code blocks
" Try to go into visual mode (v), thenselect several lines of code here and
" then press > several times.
"" vnoremap < z"
let g:jedi#popup_on_dot = 0
let g:jedi#popup_select_first = 0
map b Oimport ipdb; ipdb.set_trace() # BREAKPOINT

" Better navigating through omnicomplete option list
function! OmniPopup(action)
if pumvisible()
if a:action == 'j'
return "\"
elseif a:action == 'k'
return "\"
endif
endif
return a:action
endfunction

inoremap =OmniPopup('j')
inoremap =OmniPopup('k')

" Python folding
" mkdir -p ~/.vim/ftplugin
set nofoldenable

# A perl Data.Dumper clone for Python

Perl中的Data::Dumper很好用，可以非常方便的将任意数据类型dump成可读的形式，debug的时候用来查看运行期的某种复杂数据结构非常的方便。

# 旋转矩阵

when int i=5;
output:

 1  2  3  4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9


when int i=6;
output:

 1  2  3  4  5  6
20 21 22 23 24  7
19 32 33 34 25  8
18 31 36 35 26  9
17 30 29 28 27 10
16 15 14 13 12 11


This is a simple python program which hate if/else and switch.
--------------------------------------------------

MAX = 10
matrix = [[0 for col in range(MAX)] for row in range(MAX)]
(x, y, count, link) = (-1, 0, 1, {1:(1,0,2), 2:(0,1,3), 3:(-1,0,4), 4:(0,-1,1)})