diff options
Diffstat (limited to 'vimrcs/codecmd.vim')
-rw-r--r-- | vimrcs/codecmd.vim | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/vimrcs/codecmd.vim b/vimrcs/codecmd.vim new file mode 100644 index 0000000..4fb5ab9 --- /dev/null +++ b/vimrcs/codecmd.vim | |||
@@ -0,0 +1,85 @@ | |||
1 | set expandtab " 设置tab键替换为tabstop规定的空格数 | ||
2 | set tabstop=4 " 编辑时tab键占用的空格数 | ||
3 | set shiftwidth=4 " 设置tab的间隔(格式化时) | ||
4 | set softtabstop=4 " 设置退格键退格的空格数,让vim把连续的空格当作一个tab | ||
5 | autocmd FileType asm,nasm,vim,sh,makefile set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab | ||
6 | |||
7 | set showmatch " 在输入括号时光标会短暂地跳到与之相匹配的括号处 | ||
8 | set nowrap " 设置自动折行 | ||
9 | |||
10 | set textwidth=500 " 设置自动换行的长度 | ||
11 | set lbr " 设置自动换行 | ||
12 | set foldmethod=syntax " 设置按语法折叠代码 | ||
13 | set nofoldenable " 在打开文件时不要折叠 | ||
14 | |||
15 | " 自动补全配置 | ||
16 | autocmd FileType python set omnifunc=pythoncomplete#Complete | ||
17 | autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS | ||
18 | autocmd FileType html set omnifunc=htmlcomplete#CompleteTags | ||
19 | autocmd FileType css set omnifunc=csscomplete#CompleteCSS | ||
20 | autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags | ||
21 | autocmd FileType php set omnifunc=phpcomplete#CompletePHP | ||
22 | autocmd FileType c set omnifunc=ccomplete#Complete | ||
23 | |||
24 | " 要求格式化之后的代码不要移动光标,并且不要删除换行符和空行,保留原来的视图 | ||
25 | autocmd BufReadPost,BufWritePre *.html,*.vim,*.sh,*.py normal! g1G=G`"z<CR> | ||
26 | " 在保存时自动使用clang-format格式化代码 | ||
27 | augroup FormatAutocmd | ||
28 | autocmd! | ||
29 | autocmd BufWritePre *.c,*.cpp,*.h :let save_cursor = getpos(".") | ||
30 | autocmd BufWritePre *.c,*.cpp,*.h :normal! ggVG | ||
31 | autocmd BufWritePre *.c,*.cpp,*.h :ClangFormat | ||
32 | autocmd BufWritePost *.c,*.cpp,*.h :call setpos('.', save_cursor) | ||
33 | augroup END | ||
34 | " 定义ClangFormat命令 | ||
35 | command! -nargs=0 ClangFormat :silent %!clang-format -style="{IndentWidth: 4}" | ||
36 | " 映射gg=G到ClangFormat | ||
37 | autocmd FileType c,h,cpp,hpp,cc nnoremap <buffer> gg=G :ClangFormat<CR> | ||
38 | |||
39 | " 新建.c/.cpp/.sh/.py文件,自动插入文件头 | ||
40 | autocmd BufNewFile *.cpp,*.[ch],*.sh exec ":call SetTitle()" | ||
41 | func SetTitle() | ||
42 | if &filetype == 'sh' | ||
43 | call setline(1,"#!/bin/bash") | ||
44 | call append(line("."), "" ) | ||
45 | call append(line(".")+1, "\##########################################################################" ) | ||
46 | call append(line(".")+2, "\# File Name : ".expand("%")) | ||
47 | call append(line(".")+3, "\# Encoding : utf-8") | ||
48 | call append(line(".")+4, "\# Author : We-unite") | ||
49 | call append(line(".")+5, "\# Email : weunite1848@gmail.com") | ||
50 | call append(line(".")+6, "\# Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime())) | ||
51 | call append(line(".")+7, "\##########################################################################" ) | ||
52 | call append(line(".")+8, "") | ||
53 | elseif &filetype =='py' | ||
54 | call setline(1,"#!/usr/bin/env python") | ||
55 | call append(line("."), "" ) | ||
56 | call append(line(".")+1, "\##########################################################################" ) | ||
57 | call append(line(".")+2, "\# File Name : ".expand("%")) | ||
58 | call append(line(".")+3, "\# Encoding : utf-8") | ||
59 | call append(line(".")+4, "\# Author : We-unite") | ||
60 | call append(line(".")+5, "\# Email : weunite1848@gmail.com") | ||
61 | call append(line(".")+6, "\# Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime())) | ||
62 | call append(line(".")+7, "\##########################################################################" ) | ||
63 | call append(line(".")+8, "") | ||
64 | else | ||
65 | call setline(1, "/*************************************************************************") | ||
66 | call append(line("."), " > File Name : ".expand("%")) | ||
67 | call append(line(".")+1, " > Encoding : utf-8") | ||
68 | call append(line(".")+2, " > Author : We-unite") | ||
69 | call append(line(".")+3, " > Email : weunite1848@gmail.com ") | ||
70 | call append(line(".")+4, " > Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime())) | ||
71 | call append(line(".")+5, " ************************************************************************/") | ||
72 | call append(line(".")+6, "") | ||
73 | if &filetype == 'cpp' | ||
74 | call append(line(".")+7, "#include <iostream>") | ||
75 | call append(line(".")+8, "using namespace std;") | ||
76 | call append(line(".")+9, "") | ||
77 | elseif &filetype == 'c' | ||
78 | call append(line(".")+7, "#include <stdio.h>") | ||
79 | call append(line(".")+8, "#include <stdlib.h>") | ||
80 | call append(line(".")+9, "") | ||
81 | endif | ||
82 | endif | ||
83 | " 光标移动到文件末尾 | ||
84 | normal G | ||
85 | endfunc | ||