如何让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:快速保存退出

等功能,让编程更轻松 ~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值