如何讓vim編輯器用起來更順手

如何讓vim編輯器用起來更順手

在用戶目錄下編寫一個 .vimrc 文件
內容如下:

"顯示行號
set number
"不顯示行號,正常模式輸入:進入行底模式,set nonumber
"設置tab鍵的寬度,默認是8,一般配置爲4
set tabstop=4
"設置縮進的寬度
set shiftwidth=4
"使用空格代替tab,在Makefile中縮進要使用tab
set noexpandtab
"自動保存
set autowrite
"不生成臨時文件
set noswapfile
"自動縮進
set autoindent
"智能縮進
set smartindent

"在插入模式下使用Ctrl+hjkl代表方向鍵
inoremap<C-h> <Left>
inoremap<C-j> <Down>
inoremap<C-k> <Up>
inoremap<C-l> <Right>

"---快捷編譯---
"1編譯函數
func! CompileCode()
	"寫入文件
	exec "w"
	"判斷文件類型
	if &filetype == "cpp"
		exec "!g++ % && ./a.out"
	elseif &filetype == "c"
		exec "!gcc -std=gnu99 % -lm -lpthread && ./a.out"
	endif
endfunc

"2映射快捷鍵
map <C-x> :call CompileCode()<CR>
imap <C-x> <ESC>:call CompileCode()<CR>
vmap <C-x> <ESC>:call CompileCode()<CR>

"---快捷保存退出---
"1保存退出函數
func! SaveExit()
	exec "wq"
endfunc

"2映射快捷鍵
map <C-z> :call SaveExit()<CR>
imap <C-z> <ESC>:call SaveExit()<CR>
vmap <C-z> <ESC>:call SaveExit()<CR>


" 當新建 .h .c .hpp .cpp .mk .sh等文件時自動調用SetTitle 函數
autocmd BufNewFile *.h exec ":call SetTitle()" 

" 定義函數SetTitle,自動插入文件頭 
func SetTitle()
	if expand("%:e") == 'h'
		call setline(1,"/*****************************************************************") 
		call append(line("."),   "*   Copyright (C) ".strftime("%Y")." ZhiZhen Ltd. All rights reserved.")
		call append(line(".")+1, "*   FileName:".expand("%:t")) 
		call append(line(".")+2, "*   Author:zzxxwyz")
		call append(line(".")+3, "*   Date:".strftime("%Y年%m月%d日")) 
		call append(line(".")+4, "*   Description:") 
		call append(line(".")+5, "*****************************************************************/") 
		call append(line(".")+6, "")
		call append(line(".")+7, "#ifndef ".toupper(expand("%:t:r"))."_H") 
		call append(line(".")+8, "#define ".toupper(expand("%:t:r"))."_H") 
		call append(line(".")+9, "")
		call append(line(".")+10, "")
		call append(line(".")+11, "")
		call append(line(".")+12, "#endif//".toupper(expand("%:t:r"))."_H") 
		exec ":12"
	endif
endfunc

可實現 ->

Ctrl+x:快速編譯
Ctrl+z:快速保存退出

等功能,讓編程更輕鬆 ~~

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