1. 程式人生 > >用vim寫python程式碼時的一些配置

用vim寫python程式碼時的一些配置

本文轉自https://blog.csdn.net/lord_is_layuping/article/details/7706874?utm_source=blogxgwz6


關鍵是使Vim在發現所編輯的檔案是Python檔案時自動載入python的縮排檔案。預設的縮排方式很爛,不符合Python的編碼習慣。但我 們又不能在全域性的.vimrc(Windows中為_vimrc)修改,這會影響全域性。使用autocmd只能靠後綴來識別Python檔案,也不方便。 於是最好的方法是自定義Python縮排檔案,放到~/.vim/indent/(Windows下是$VIMDIR$\vimfiles\indent \    如C:\Program Files\Vim\vimfiles\indent)中,Vim發現Python檔案後,會自動載入這個plugin,而不是用預設的縮排檔案。可是,呵呵,編寫這些是有些麻煩的(唉,對我很困難)。但 不用擔心,有人以為我們寫好了,直接到這個頁面下載即可

http://www.vim.org/scripts/script.php?script_id=974


package script version date Vim version user release notes
python.vim 0.3 2005-05-23 6.0 Eric Mc Sween Changes: 
- Update one shiftwidth instead of aligning with parens that stand at the end of a line.
python.vim 0.2 2004-06-07 6.0 Eric Mc Sween Changes: 
- Fix: skip parentheses in strings and comments. 
- Line up elif/else and except/finally with the most probable corresponding if or try statement. 
- Dedent after 'pass'. (Jeffrey Collins)
python.vim
0.1 2004-04-26 6.0 Eric Mc Sween Initial upload


原始檔的註釋縮排有些問題,作如下修改:

setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except

改為:

setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except,0#

另外,在.vimrc(Windows中為_vimrcC:\Program Files\Vim\_vimrc)中加入以下語句以確定外掛功能開啟:filetype plugin indent on好了,現在就可以享受舒服的Python形式的自動縮進了。


----------------------------------------------------------------------------------------------------------------------------------------------------------

在vim中顯示的都是一個tab,對齊的很工整,

一執行就出錯“unindent does not match any outer indentation level”,搞了半天就是縮排不統一啊,

python又把縮排作為語法之一(的確挺好)。

解決辦法:

set tabstop=8

其他關於縮排的:

set shiftwidth=4 
set tabstop=4 
set expandtab 
set softtabstop=4 
set pastetoggle=<f7> 
set autoindent 
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class

如何拷貝程式碼而不破壞縮排?

開始拷貝前按F7,拷貝結束後再按一次F7。

如何改變整段程式碼的縮排?

按”v”進入檢視模式,選中要移動的程式碼塊。然後用“<”左縮排,或用“>”右縮排。縮排寬度預設為1個shiftwidth寬。如果要縮排多個shiftwidth寬,在按“<”或”>”之前先按相應數字。

ChangeLog:

Mon Aug 4 10:46:30 CST 2008,修正“如何”寫成“如果”的筆誤。

Sun Jul 8 CST 2007,新增改變整段程式碼縮排的方法。

Tue Jul 3 CST 2007,加入pastetoggle設定

詳細出處參考:http://www.jb51.net/softjc/7832.html


----------------------------------------------------------------------------------------------------------------------------------------------------------



用vim寫python程式碼 

轉自: http://chayegg.com/index.php/2011/09/code_python_with_vim/

在自己的home目錄下編輯.vimrc檔案,如果沒有新建一個
下面的配置能讓自己過得舒服一點

" 不要使用vi的鍵盤模式,而是vim自己的
set nocompatible

" 設定解碼
if has(“multi_byte”)
" When ‘fileencodings’ starts with ‘ucs-bom’, don’t do this manually
"set bomb
set fileencodings=ucs-bom,utf-8,chinese,taiwan,japan,korea,latin1
" CJK environment detection and corresponding setting
if v:lang =~ “^zh_CN”
" Simplified Chinese, on Unix euc-cn, on MS-Windows cp936
set encoding=utf-8
set termencoding=utf-8
if &fileencoding == ‘’
set fileencoding=utf-8
endif
elseif v:lang =~ “^zh_TW”
" Traditional Chinese, on Unix euc-tw, on MS-Windows cp950
set encoding=euc-tw
set termencoding=euc-tw
if &fileencoding == ‘’
set fileencoding=euc-tw
endif
elseif v:lang =~ “^ja_JP”
" Japanese, on Unix euc-jp, on MS-Windows cp932
set encoding=euc-jp
set termencoding=euc-jp
if &fileencoding == ‘’
set fileencoding=euc-jp
endif
elseif v:lang =~ “^ko”
" Korean on Unix euc-kr, on MS-Windows cp949
set encoding=euc-kr
set termencoding=euc-kr
if &fileencoding == ‘’
set fileencoding=ecu-kr
endif
endif
" Detect UTF-8 locale, and override CJK setting if needed
if v:lang =~ “utf8 &quot; v : l a n g =   &quot; U T F 8 &quot; || v:lang =~ &quot;UTF-8
set encoding=utf-8
endif
else
echoerr ‘Sorry, this version of (g)Vim was not compiled with “multi_byte”’
endif

" 自動格式化設定
filetype indent on
set autoindent
set smartindent

" 顯示未完成命令
set showcmd
" 偵測檔案型別
filetype on

" 載入檔案型別外掛
filetype plugin on

" 為特定檔案型別載入相關縮排檔案
filetype indent on

" 語法高亮
syntax on

" 顯示行號
set number

" tab寬度
set tabstop=4
set cindent shiftwidth=4
set autoindent shiftwidth=4

" 儲存檔案格式
set fileformats=unix,dos

" 檔案被其他程式修改時自動載入
set autoread

" 命令列補全
set wildmenu

" 開啟檔案時,總是跳到退出之前的游標處
autocmd BufReadPost *
\ if line("’"") > 0 && line("’"") <= line("$") |
\ exe “normal! g`”" |
\ endif

filetype plugin on "允許使用ftplugin目錄下的檔案型別特定指令碼
filetype indent on "允許使用indent目錄下的檔案型別縮排

“”""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" PYTHON 相關的設定 "
“”""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Python 檔案的一般設定,比如不 tab 等
"設定自動縮排為4,插入模式裡: 插入 <Tab> 時使用合適數量的空格。
"插入實際的製表,可用 CTRL-V<Tab>
autocmd FileType python setlocal expandtab | setlocal shiftwidth=4 |
\setlocal softtabstop=4 | setlocal textwidth=76 |
\setlocal tabstop=4

"搜尋逐字元高亮
set hlsearch
set incsearch

"設定程式碼樣式
colorscheme desert

"設定tags查詢位置
set tags=tags;
set autochdir

==========================================================
pydiction補全程式碼pydiction補全截圖
官方地址http://www.vim.org/scripts/script.php?script_id=850
下載zip包,在home目錄下查詢.vim資料夾,如果沒有建立這個目錄
官網有安裝說明”install details”
完成後.vim的檔案結構如下:

.vim
└── after
    └── ftplugin
        ├── pydiction
        │   └── complete-dict
        └── python_pydiction.vim

然後配置.vimrc,新增語句

let g:pydiction_location = '~/.vim/after/ftplugin/pydiction/complete-dict'

後面的值是complete-dict檔案的路徑
用vim編輯一個py檔案,import os.<TAB>,這時候應該出現提示,證明成功了
ctrl+n ctrl+p選擇列表裡的提示項
===========================================================
其實7.2版本的vim自身已經提供了比較強悍的補全功能, vim的OMNI補全(也叫”全能補全”)
os.<CTRL+x , CTRL+o>,如果開啟了vim的python模組,現在應該有一個分割視窗顯示函式的引數,以及__doc__資訊

如果需動態輸入重新整理提示內容,在配置檔案中加入

set completeopt=longest,menu

全能補全截圖
============================================================

ctags提示
tags截圖
首先確定安裝了ctags軟體,執行tags生成指令碼

ctags -R *.py

這時會生成tags檔案

安裝taglist外掛,網址http://www.vim.org/scripts/script.php?script_id=273
安裝完成後,編輯py檔案,執行vim命令

:Tlist

會出現taglist視窗,如果需tags檔案中的關鍵詞補全,CTRL+n,如果需跟蹤關鍵詞檔案CTRL+],跳回來CTRL+t

============================================================

程式碼模板,主頁地址 http://www.vim.org/scripts/script.php?script_id=2540
程式碼模板截圖
安裝後,這個外掛預設的快捷鍵是<TAB>
但是pydiction_location預設的快捷鍵也是<TAB>,這裡修改 pydiction_location的快捷鍵
找到.vim/after/ftplugin/python_pydiction.vim檔案
修改

" Make the Tab key do python code completion:
inoremap <silent> <buffer> <TAB>

" Make the Tab key do python code completion:
inoremap <silent> <buffer> <C-P>

這樣就把pydiction_location的快捷鍵修改為CTRL+p了
然後編輯py檔案,輸入 cl<TAB>,就會出現class的定義模板了,
這些模板定義在.vim/syntax資料夾下,可自行修改

==============================================================

py語法檢查外掛 http://www.vim.org/scripts/script.php?script_id=2441
程式碼檢查截圖
安裝以後會用紅色,提示py程式碼的錯誤