如何让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:快速保存退出
等功能,让编程更轻松 ~~