Skip to content

Instantly share code, notes, and snippets.

@cedmundo
Last active March 21, 2018 00:32
Show Gist options
  • Save cedmundo/b1fbf490cffc527f5477c929e8f67d7e to your computer and use it in GitHub Desktop.
Save cedmundo/b1fbf490cffc527f5477c929e8f67d7e to your computer and use it in GitHub Desktop.
My personal vimrc for golang, requires vundle.
" Description: Vimrc with golang compatibility. Replaces default vimrc
" Author: Carlos Edmundo Martinez Mendoza.
" Created: 2016-03-09 00:00:00
" Necesary for lots of cool vim things
set nocompatible
" {{{ Auto commands
"
" Automatically cd into the directory that the file is in
autocmd VimEnter * silent! :cd%:p:h
" Remove trailing whitespace that is in the file
autocmd BufRead,BufWrite * if ! &bin | silent! %s/\s\+$//ge | endif
" Restore cursor position to where it was before
augroup JumpCursorOnEdit
au!
autocmd BufReadPost *
\ if expand("<afile>:p:h") !=? $TEMP |
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ let JumpCursorOnEdit_foo = line("'\"") |
\ let b:doopenfold = 1 |
\ if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
\ let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
\ let b:doopenfold = 2 |
\ endif |
\ exe JumpCursorOnEdit_foo |
\ endif |
\ endif
" Need to postpone using "zv" until after reading the modelines.
autocmd BufWinEnter *
\ if exists("b:doopenfold") |
\ exe "normal zv" |
\ if(b:doopenfold > 1) |
\ exe "+".1 |
\ endif |
\ unlet b:doopenfold |
\ endif
augroup END
" }}}
" {{{ Vundle
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-fugitive'
Plug 'fatih/vim-go'
Plug 'gilgigilgil/anderson.vim'
call plug#end()
filetype plugin indent on
" }}}
" {{{ Vundle plugin settings
" Airline
" let g:airline_powerline_fonts = 1
" Syntastic
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
" Golang settings
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1
let g:go_highlight_interfaces = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
let g:go_fmt_command = "goimports"
let g:go_fmt_fail_silently = 1
let g:go_fmt_autosave = 1
let g:syntastic_go_checkers = ['golint', 'govet', 'errcheck']
let g:syntastic_mode_map = { 'mode': 'active', 'passive_filetypes': ['go'] }
" GoTags
let g:tagbar_type_go = {
\ 'ctagstype' : 'go',
\ 'kinds' : [
\ 'p:package',
\ 'i:imports:1',
\ 'c:constants',
\ 'v:variables',
\ 't:types',
\ 'n:interfaces',
\ 'w:fields',
\ 'e:embedded',
\ 'm:methods',
\ 'r:constructor',
\ 'f:functions'
\ ],
\ 'sro' : '.',
\ 'kind2scope' : {
\ 't' : 'ctype',
\ 'n' : 'ntype'
\ },
\ 'scope2kind' : {
\ 'ctype' : 't',
\ 'ntype' : 'n'
\ },
\ 'ctagsbin' : 'gotags',
\ 'ctagsargs' : '-sort -silent'
\ }
" }}}
" {{{ Misc settings
" Needed for syntax highlighting and stuff
syntax enable
set grepprg=grep\ -nH\ $*
" Set default encoding as UTF-8
set encoding=utf8
" Use Unix as the standard file type
set ffs=unix,dos,mac
" Turn backup off, since most stuff is in SVN, git et.c anyway...
set nobackup
set nowb
set noswapfile
" Cool colors
colorscheme anderson
" Show line numbers
set number
" Who doesn't like autoindent?
set autoindent
" Spaces are better than a tab character
set expandtab
set smarttab
" Gofmt outputs
set shiftwidth=8
set softtabstop=8
" Enable mouse support in console
set mouse=a
" Got backspace?
set backspace=indent,eol,start
" Incremental searching is sexy
set incsearch
" Highlight things that we find with the search
set hlsearch
" Better command-line completion
set wildmenu
" Use visual bell instead of beeping when doing something wrong
set visualbell
" Use <F11> to toggle between 'paste' and 'nopaste'
set pastetoggle=<F11>
" Create a new tab or switch if already open when jumping
set switchbuf+=usetab,newtab
" Set leader key
let mapleader = ","
" Airline always on
" set laststatus=0
set ruler
set rulerformat=%l\:%c
" I want to see hidden characters, because reasons
" set listchars=eol:$
" set list
" Color at 80
" set colorcolumn=80
" }}}
" {{{ Mappings
" Map Y to act like D and C, i.e. to yank until EOL, rather than act as yy
" map Y y$
" Replace selection using c-r
vnoremap <C-r> "hy:%s/<C-r>h//gc<left><left><left>
" Visual shifting (does not exit Visual mode)
vnoremap < <gv
vnoremap > >gv
" Golang bindings
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)
au FileType go nmap <Leader>ds <Plug>(go-def-split)
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
au FileType go nmap <Leader>dt <Plug>(go-def-tab)
au FileType go nmap <Leader>gd <Plug>(go-doc)
au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
au FileType go nmap <Leader>gb <Plug>(go-doc-browser)
au FileType go nmap <Leader>s <Plug>(go-implements)
au FileType go nmap <Leader>i <Plug>(go-info)
au FileType go nmap <Leader>e <Plug>(go-rename)
au FileType go nmap <Leader>f :GoImports<cr>:GoLint<cr>
" Window navigation
map <C-Up> <C-W>k
map <C-Down> <C-W>j
map <C-Left> <C-W>h
map <C-Right> <C-W>l
map <C-S-Up> :resize +10<cr>
map <C-S-Down> :resize -10<cr>
map <C-S-Left> :vertical resize -10<cr>
map <C-S-Right> :vertical resize +10<cr>
" Delete highlight
map <leader><bs> :noh<cr>
" Sometimes I git W instead w
:command WQ wq
:command Wq wq
:command W w
:command Q q
" }}}
" {{{ Autocmds
" Somethimes golang plugin for templates inserts U+00A0 spaces.
autocmd BufWritePost *.tmpl silent! %s/\%xA0/ /g
" }}}
" {{{ Common functions
" Source: http://stackoverflow.com/questions/676600/vim-search-and-replace-selected-text
vnoremap <C-r> "hy:%s/<C-r>h//gc<left><left><left>
" Source: http://stackoverflow.com/questions/11366390/how-to-enumerate-tabs-in-vim
fu! MyTabLabel(n)
let buflist = tabpagebuflist(a:n)
let winnr = tabpagewinnr(a:n)
let string = fnamemodify(bufname(buflist[winnr - 1]), ':t')
return empty(string) ? '[unnamed]' : string
endfu
fu! MyTabLine()
let s = ''
for i in range(tabpagenr('$'))
" select the highlighting
if i + 1 == tabpagenr()
let s .= '%#TabLineSel#'
else
let s .= '%#TabLine#'
endif
" set the tab page number (for mouse clicks)
"let s .= '%' . (i + 1) . 'T'
" display tabnumber (for use with <count>gt, etc)
let s .= ' '. (i+1) . ' '
" the label is made by MyTabLabel()
let s .= ' %{MyTabLabel(' . (i + 1) . ')} '
if i+1 < tabpagenr('$')
let s .= ' |'
endif
endfor
return s
endfu
set tabline=%!MyTabLine()
" }}}
" Two spces on html files
au FileType html set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType js set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType css set expandtab smarttab shiftwidth=2 softtabstop=2
au BufReadPost,BufNewFile *.hbs set filetype=handlebars smartindent expandtab smarttab shiftwidth=2 softtabstop=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment