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