summaryrefslogtreecommitdiffstats
path: root/vimrcs/codecmd.vim
blob: 4add725d5df1b39c0ce965489ea805608087b3b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
set expandtab			" 设置tab键替换为tabstop规定的空格数
set tabstop=4			" 编辑时tab键占用的空格数
set shiftwidth=4		" 设置tab的间隔(格式化时)
set softtabstop=4		" 设置退格键退格的空格数,让vim把连续的空格当作一个tab
autocmd FileType asm,nasm,vim,sh,makefile set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab

set showmatch			" 在输入括号时光标会短暂地跳到与之相匹配的括号处
set nowrap				" 设置自动折行

set textwidth=500		" 设置自动换行的长度
set lbr					" 设置自动换行
set foldmethod=syntax	" 设置按语法折叠代码
set nofoldenable		" 在打开文件时不要折叠

" 自动补全配置
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

" 要求格式化之后的代码不要移动光标,并且不要删除换行符和空行,保留原来的视图
autocmd BufReadPost,BufWritePre *.html,*.vim,*.sh,*.py normal! g1G=G`"z<CR>
" 在保存时自动使用clang-format格式化代码
augroup FormatAutocmd
	autocmd!
	autocmd BufWritePre *.c,*.cpp,*.h :let save_cursor = getpos(".")
	autocmd BufWritePre *.c,*.cpp,*.h :normal! ggVG
	autocmd BufWritePre *.c,*.cpp,*.h :ClangFormat
	autocmd BufWritePost *.c,*.cpp,*.h :call setpos('.', save_cursor)
augroup END

command! -nargs=0 ClangFormat :call ClangFormat()
autocmd FileType c,h,cpp,hpp,cc nnoremap <buffer> gg=G :ClangFormat<CR>
" 定义ClangFormat命令:
" 当前文件的各级父目录下如果存在.clang-format,则使用
" 否则使用默认规则,缩进为4
function! ClangFormat()
	let clang_format = "clang-format"
	" find .clang-format file from cur path upward
	let clang_format_file = findfile(".clang-format", ".;")
	if clang_format_file != ""
		let clang_format = clang_format . " -style=file"
	else
		let clang_format = clang_format . " -style='{IndentWidth: 4}'"
	endif
	let save_cursor = getpos(".")
	let save_view = winsaveview()
	let save_cursor = getpos(".")
	let save_view = winsaveview()
	execute ":silent %! ".clang_format
	call setpos('.', save_cursor)
	call winrestview(save_view)
endfunction

" 新建.c/.cpp/.sh/.py文件,自动插入文件头
autocmd BufNewFile *.cpp,*.[ch],*.sh exec ":call SetTitle()"
func SetTitle()
	if &filetype == 'sh'
		call setline(1,"#!/bin/bash")
		call append(line("."),   "" )
		call append(line(".")+1, "\##########################################################################" )
		call append(line(".")+2, "\# File Name    : ".expand("%"))
		call append(line(".")+3, "\# Encoding     : utf-8")
		call append(line(".")+4, "\# Author       : We-unite")
		call append(line(".")+5, "\# Email        : weunite1848@gmail.com")
		call append(line(".")+6, "\# Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime()))
		call append(line(".")+7, "\##########################################################################" )
		call append(line(".")+8, "")
	elseif &filetype =='py'
		call setline(1,"#!/usr/bin/env python")
		call append(line("."),   "" )
		call append(line(".")+1, "\##########################################################################" )
		call append(line(".")+2, "\# File Name    : ".expand("%"))
		call append(line(".")+3, "\# Encoding     : utf-8")
		call append(line(".")+4, "\# Author       : We-unite")
		call append(line(".")+5, "\# Email        : weunite1848@gmail.com")
		call append(line(".")+6, "\# Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime()))
		call append(line(".")+7, "\##########################################################################" )
		call append(line(".")+8, "")
	else
		call setline(1, "/*************************************************************************")
		call append(line("."),   "  > File Name    : ".expand("%"))
		call append(line(".")+1, "  > Encoding     : utf-8")
		call append(line(".")+2, "  > Author       : We-unite")
		call append(line(".")+3, "  > Email        : weunite1848@gmail.com ")
		call append(line(".")+4, "  > Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime()))
		call append(line(".")+5, " ************************************************************************/")
		call append(line(".")+6, "")
		if &filetype == 'cpp'
			call append(line(".")+7, "#include <iostream>")
			call append(line(".")+8, "using namespace std;")
			call append(line(".")+9, "")
		elseif &filetype == 'c'
			call append(line(".")+7, "#include <stdio.h>")
			call append(line(".")+9, "")
		endif
	endif
	" 光标移动到文件末尾
	normal G
endfunc