vim寫程序常用技巧

   1.我們可以使用gf快捷鍵來進入任意一個被#include <header.h>的頭文件.

       2.使用ctrl+O來回到使用gf之前的文件中。

   3.找到的一個不錯的vimrc修改了一下可以自動更新ctags文件

   4.增加了對系統頭文件進行ctrl+]來查找的功能

   5.增加了對splint進行代碼檢查的快捷鍵映射

   6.如果想編譯時自動定位到出錯的地方,那麼寫一個Makefile,然後在vim中執行make應該是個不錯的主意,我想!

   7. 此文件在~/.vimrc或/etc/vim/vimrc


"Set mapleader
let g:mapleader = ","

set nocompatible

set backspace=indent,eol,start

"顯示行號
set nu

"打開語法高亮
syntax on
"let asmsyntax="gas"
let asmsyntax="nasm"
"設置字體
set guifont=DejaVu\ Sans\ Mono\ 12

"設置縮進
set softtabstop=8
set shiftwidth=8
set expandtab

"關閉toolbar
set guioptions-=T

"關閉自動備份
set nobackup

"自動格式化
set formatoptions=tcrqn

"在行和段開始處使用製表符
set smarttab

"在normal模式下使用系統剪貼板
"set clipboard+=unnamed

"自動縮進設置
set cindent
set smartindent
set incsearch
set autoindent
set cinoptions=:0
"Show matching bracets
set showmatch

"Get out of VI's compatible mode
set nocompatible

"Have the mouse enabled all the time
set mouse=a

"Set to auto read when a file is changed from the outside
set autoread

"Enable filetype plugin
filetype plugin indent on

"設置配色方案爲torte
"colo torte
colo desert
"colo tango
"設置支持的文件編碼類項,目前設置爲utf-8和gbk兩種類型
set fenc=utf-8
set fileencodings=utf-8,chinese,gb18030,gbk,gb2312,cp936
set enc=utf-8
let &termencoding=&encoding


"設置斷詞
set linebreak

"設置搜索結果高亮顯示
set hlsearch

"設置記錄的歷史操作列表
set history=200

"設置摺疊
set foldenable
set foldcolumn=2
set foldlevel=3

"打開目錄時不顯示隱藏目錄和文件
let g:netrw_hide= 1
let g:netrw_list_hide= '^\..*'

"AutoCommand
" 鼠標跳到上次關閉時,編輯的位置
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ endif
"新建.c,.h.cpp,.sh,.java,.php,.py文件自動打開Taglist
autocmd BufNewFile *.[ch],*.cpp,*.sh,*.java,*.php,*.py exec ":call SetTitle()"
"讀入.c,.h.cpp,.sh,.java,.php,.py文件自動打開Taglist
"autocmd BufRead *.[ch],*.cpp,*.sh,*.java,*.php,*.py exec ":Tlist"
"新建文件後,自動定位到文件末尾
autocmd BufNewFile * normal G
"如果是新建的php文件,則自動定位到最後第二行
autocmd BufNewFile *.php normal k

"寫入.c,.h.cpp,.sh,.java,.php,.py文件自動更新ctags
autocmd BufWrite *.[ch],*.cpp,*.sh,*.java,*.php,*.py exec ":!ctags -R *"
"
"讀入python文件,設置縮進格式
autocmd BufNewFile,BufRead *.py set cinwords=if,elif,else,for,while,try,expect,finally,def,class

"讀入C文件,設置摺疊方式爲syntax
autocmd BufNewFile,BufRead *.[ch],*.cpp set foldmethod=syntax

"讀入其它文件,設置摺疊方式爲indent
autocmd BufNewFile,BufRead *.py,*.sh,*.java,*.php set foldmethod=indent

"設置Java代碼的自動補全
autocmd FileType java setlocal omnifunc=javacomplete#Complete
"autocmd FileType java set tags=./tags,./../tags,./../../tags

"設置輸入代碼的自動補全
"autocmd BufEnter * call DoWordComplete()

"設置當回覆郵件時自動定位到最後一行
autocmd BufRead /tmp/mutt-* normal G
"autocmd BufRead /tmp/mutt-* normal $ 
"綁定自動補全的快捷鍵<C-X><C-O>到<leader>;
imap <leader>; <C-X><C-O>

"綁定複製到系統剪貼板快捷鍵
vmap <leader>c "+y
nmap <leader>c "+y

"綁定粘貼系統剪貼板內容快捷鍵
"imap <leader>v <ESC>"+p "不設置insert模式下的快捷鍵,因爲會造成無法輸入,v
vmap <leader>v <ESC>"+p
nmap <leader>v "+p

"設定開關Taglist插件的快捷鍵爲F4,可以在VIM的左側欄列出函數列表等
map <F4> :Tlist<CR>

"設置程序的運行和調試的快捷鍵F5和Ctrl-F5
map <F5> :call CompileRun()<CR>
map <C-F5> :call Debug()<CR>
"設置手動更新tags文件
map <F8> :!ctags -R * <CR>
map <F9> :!splint % <CR>
"設置tab操作的快捷鍵,綁定:tabnew到<leader>t,綁定:tabn, :tabp到<leader>n,
"<leader>p
map <leader>t :tabnew<CR>
map <leader>n :tabn<CR>
map <leader>p :tabp<CR>

"設置空格鍵開關摺疊
nmap <SPACE> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>

"使用<leader>r打開上次運行的命令
nmap <leader>r :<UP>

"用cscope支持
set csprg=/usr/bin/cscope
let Tlist_Ctags_Cmd='/usr/local/bin/ctags'
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
let Tlist_Use_Right_Window=1
"默認打開Taglist
let Tlist_Auto_Open=1

"設置搜索的tags文件範圍
set tags=./tags,./../tags,./../../tags,/usr/include/tags,/usr/src/linux-3.2.6/include/tags

"使用<leader>e打開當前文件同目錄中的文件
if has("unix")
        map <leader>e :e <C-R>=expand("%:p:h") . "/" <CR>
else
        map <leader>e :e <C-R>=expand("%:p:h") . "\" <CR>
endif

"定義CompileRun函數,用來調用進行編譯和運行
func CompileRun()
        exec "w"
        "C程序
        if &filetype == 'c'
                exec "!gcc % -g -o %<"
                exec "!./%<"
                "Java程序
        elseif &filetype == 'java'
                exec "!javac %"
                exec "!java %<"
                "php程序
        elseif &filetype == 'php'
                exec "!php %"
                "bash程序
        elseif &filetype == 'sh'
                exec "!bash %"
                "python程序
        elseif &filetype == "python"
                exec "!python %"
        endif
endfunc
"結束定義CompileRun

"定義Debug函數,用來調試程序
func Debug()
        exec "w"
        "C程序
        if &filetype == 'c'
                exec "!gcc % -g -o %<"
                exec "!gdb %<"
                "Java程序
        elseif &filetype == 'java'
                exec "!javac %"
                exec "!jdb %<"
                "Php程序
        elseif &filetype == 'php'
                exec "!php %"
                "bash程序
        elseif &filetype == 'sh'
                exec "!bash -x %"
                "python程序
        elseif &filetype == 'python'
                exec "!pdb %"
        endif
endfunc
"結束定義Debug

"定義函數SetTitle,自動插入文件頭
func SetTitle()
        "如果文件類型爲.sh文件
        if &filetype == 'sh' || &filetype == 'python'
                call setline(1, "\#========================================================================")
                call append(line("."), "\# Author: findstr")
                call append(line(".")+1, "\# Email: [email protected]")
                call append(line(".")+2, "\# File Name: ".expand("%"))
                call append(line(".")+3, "\# Description: ")
                call append(line(".")+4, "\#   ")
                call append(line(".")+5, "\# Edit History: ")
                call append(line(".")+6, "\#   ".strftime("%Y-%m-%d")."    File created.")
                call append(line(".")+7, "\#========================================================================")
                call append(line(".")+8, "")
                "其它程序文件
        else
                call setline(1, "/**")
                call append(line("."), "=========================================================================")
                call append(line(".")+1, " Author: findstr")
                call append(line(".")+2, " Email: [email protected]")
                call append(line(".")+3, " File Name: ".expand("%"))
                call append(line(".")+4, " Description: (C)  ".strftime("%Y-%m"). "  findstr")
                call append(line(".")+5, "   ")
                call append(line(".")+6, " Edit History: ")
                call append(line(".")+7, "   ".strftime("%Y-%m-%d")."    File created.")
                call append(line(".")+8, "=========================================================================")
                call append(line(".")+9, "**/")
                call append(line(".")+10, "")
        endif
        "如果爲php文件,添加相應頭和尾
        if &filetype == 'php'
                call append(0, "<?php")
                call append(line("$"), "?>")
        endif
        "如果爲sh文件,添加相應的頭
        if &filetype == 'sh'
                call append(0, "\#!/bin/bash")
                "如果爲python文件,添加相應的頭和編碼設定
        elseif &filetype == 'python'
                call append(0, "\#!/usr/bin/python")
                call append(1, "\# -*- coding: utf-8 -*-")
        endif
endfunc


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章