Skip to content

Instantly share code, notes, and snippets.

@reddec
Created May 20, 2016 11:54
Show Gist options
  • Save reddec/2d35054d31a3bc067d752989f880c89e to your computer and use it in GitHub Desktop.
Save reddec/2d35054d31a3bc067d752989f880c89e to your computer and use it in GitHub Desktop.
vimrc for C
" Restore cursor position, window position, and last search after running a
" command.
function! Preserve(command)
" Save the last search.
let search = @/
" Save the current cursor position.
let cursor_position = getpos('.')
" Save the current window position.
normal! H
let window_position = getpos('.')
call setpos('.', cursor_position)
" Execute the command.
execute a:command
" Restore the last search.
let @/ = search
" Restore the previous window position.
call setpos('.', window_position)
normal! zt
" Restore the previous cursor position.
call setpos('.', cursor_position)
endfunction
" Re-indent the whole buffer.
function! Indent()
call Preserve('normal gg=G')
endfunction
function! Reformat()
call Preserve('normal gggqG')
endfunction
set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
set colorcolumn=110
set autoread
set formatprg=clang-format-3.6\ -style=Google
set makeprg=make\ -j2
highlight ColorColumn ctermbg=darkgray
augroup project
autocmd!
autocmd BufWritePre *.h,*.c call Reformat()
autocmd BufRead,BufNewFile *.h,*.c set filetype=c.doxygen
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment