天天看點

vim基本配置

vim基本配置:包括tab鍵替換成4個空格

Edit Vim configuration file ".vimrc" in your HOME directory, add below lines:

set et
set ci
set sw=4
set ts=4
           

 After new setting take effect, each time you press TAB key, it will automatically replaced by 4 SPACE and do indentation.

You can still insert real Tabs as [Ctrl]-V [Tab].

If you open an unwell indentation source code file, you can press "gg=G", all TABs will be replaced by 4 SPACE. 

        在使用vim編輯器的時候  很多系統裡預設的vim tapstop是8,而很多時候我們需要的tapstop是4,比如在寫python的時候,我們都知道python嚴重依賴縮進,是以tapstop是8的話 ,這個腳本比較大的時候那就相當的不好看了,是以在這種情況下修改tapstop為4還是很有必要的。

        怎麼設定這個東東呢?很簡單  打開/etc/vimrc 加入類似這樣的内容就可以了:

set tabstop=4

或者

set  ts=4

        然後你再用vim編輯個檔案并按tab試試,就會感覺到明顯的不一樣。

但是問題又來了,在python腳本裡,如果縮進使用tab那麼就不再推薦在同一腳本裡使用其他的符号來縮進,因為這很有可能導緻諸多相容性問題,比如你既使用了tab又在某些地方使用了空格來縮進,額,恭喜你,你很有可能會遇到無法執行此腳本的錯誤。

這個時候怎麼辦呢?最顯而易見的方法隻有一個,那就是統一使用相同的縮進方法,該是做出艱難的選擇的時候了:要麼用tab  要麼用空格 。而很多經常使用python的同學可能會發現使用空格縮進比tab來縮進似乎更加明智,是以也極力推薦統一使用空格,這是為什麼呢?當你vim編輯一個檔案的時候,你能一眼就看出縮進使用的是tab嗎?最有可能發生的是把tab當成了空格而不是把空格看成tab。為了避免這樣的困擾,統一使用空格看起來是更好的選擇。

現在你再打開vim,按下tab,再按backspace,你會發現tab出來的縮進隻需要backspace一次就能删除,這足以說明這段空白是tab;然後你再次修改vimrc,并加入:

set expandtab

set et

再次打開vim,你會發現tab出來的空白已經變成空格了,而這個時候一個tab就真正成為4個空格了,而不僅僅是4個空格的縮進距離。這才是我們真正想要的。

那麼如果你編輯了一個檔案并且想要在别人修改這個檔案的時候不會出現類似的疑問怎麼辦呢?方法有兩個,一個是你告訴他,你使用的是空格還是tab,另一個方法是使用vim的modeline,當别人打開這個檔案的時候會自動使用相同的配置。什麼是modeline呢?就是往檔案裡面寫入特定的行,當vim打開這個檔案的時候會自動讀取這一行的參數配置并調整自己的設定到這個配置,例如你可能經常會看到在有些源碼的檔案末尾會出現這樣的行:

# vim:et:ts=4:sw=4:

這個就是modeline。當其他人的vimrc裡打開了set modeline的時候,就會自動讀入這個配置。

更多的資訊請參考vim :help auto-setting  和:help modeline

set nocompatible "關閉vi相容
set enc=utf-8
"set number "顯示行号
filetype plugin on "檔案類型
set history=500 "曆史指令
syntax on "文法高亮
"set autoindent "ai 自動縮進
"set smartindent "智能縮進
set showmatch "括号比對
set ruler "右下角顯示光标狀态行
set nohls "關閉比對的高亮顯示
set incsearch "設定快速搜尋
set foldenable "開啟代碼折疊
"set fdm=manual "手動折疊
set foldmethod=syntax "自動文法折疊
set modeline "自動載入模式行
"自動插入modeline
func! AppendModeline()
let l:modeline = printf(" vim: set ts=%d sw=%d tw=%d :",
\ &tabstop, &shiftwidth, &textwidth)
let l:modeline = substitute(&commentstring, "%s", l:modeline, "")
call append(line("$"), l:modeline)
endfunc
"按\ml,自動插入modeline
nnoremap <silent> <Leader>ml :call AppendModeline()<CR>
"空格展開折疊
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>

"set tabstop=4
"set shiftwidth=4
set ts=4
set sw=4
set expandtab
"自動tab

 

if has("autocmd")
filetype plugin indent on
endif
autocmd filetype python setlocal et sta sw=4 sts=4


"根據檔案類型自動插入檔案頭
autocmd BufNewFile *.py,*.sh exec ":call SetTitle()"
func SetTitle()
if &filetype == 'sh'
call setline(1, "\#!/bin/bash")
call append(line("."), "\# Author:[email protected]")
call append(line(".")+1, "")
else
call setline(1, "\#!/bin/env python")
call append(line("."), "\#coding:utf-8")
call append(line(".")+1, "\#Author:[email protected]")
call append(line(".")+2, "")
endif
endfunc 
"建立檔案後自動定位至檔案末尾
autocmd BufNewFile * normal G
"F2去空行
nnoremap <F2> :g/^\s*$/d<CR>           

繼續閱讀