Skip to content

Instantly share code, notes, and snippets.

@fsmv
Last active June 25, 2023 04:36
Show Gist options
  • Save fsmv/436f62cd76c0a7f339f67617bf5a68f0 to your computer and use it in GitHub Desktop.
Save fsmv/436f62cd76c0a7f339f67617bf5a68f0 to your computer and use it in GitHub Desktop.
A simpler .vimrc with better default settings for programming and with golang autoformatting and automatic listchars
" Optional plugin manager see: https://github.com/junegunn/vim-plug
" Do this first so we can overwrite anything it does after
"call plug#begin('~/.vim/plugged')
" Automatically source .local.vimrc files in directories you open
"Plug 'thinca/vim-localrc'
"call plug#end()
set title " Set terminal title
set mouse=a " Turn on mouse support
set undolevels=1000
set noswapfile " Don't make those backup files all over the filesystem
filetype plugin on " Turn on detecting filetypes and loading extra config
syntax on " Turn on syntax highlighting
set number " Display line numbers
set nowrap " Don't visually wrap lines when they're off the screen
colorscheme ir_black " Download from https://github.com/twerth/ir_black
" Preferred indentation settings
set expandtab
set tabstop=2
set shiftwidth=2
" Set the line width and display it
set textwidth=80 " Wrap comments etc at 80 chars
set formatoptions-=t " Turn off auto-wrapping of text, but leave comment wrapping on
set colorcolumn=+1 " Display the 80 chars margin, in darkgrey
hi ColorColumn ctermbg=darkgrey
" Turn on auto indent settings that are language syntax aware
filetype indent on " Load the indent configuration by filetype
set autoindent
set smartindent
set cindent
" Make Q rewrap lines instead of doing the annoying Ex mode that you have to use
" :visual to get out of
noremap Q gq
" Ctrl+N for :make in normal mode, which is just an alias for 'j' by default
nmap <C-n> :make<CR>
" Display tab characters as "|---" and trailing spaces as ".",
" but only display tabs if expandtab is set (you're using spaces not tabs).
"
" This way you can use spaces in some languages and tabs in other languages and
" have the listchars automatically set correctly.
function SetListchars(option_type)
if &expandtab
if a:option_type=="local"
setlocal listchars=tab:\|-,trail:.,extends:#,nbsp:.
else
set listchars=tab:\|-,trail:.,extends:#,nbsp:.
endif
else
if a:option_type=="local"
setlocal listchars=tab:\ \ ,trail:.,extends:#,nbsp:.
else
set listchars=tab:\ \ ,trail:.,extends:#,nbsp:.
endif
endif
endfunction
set list " Turn on displaying listchars
" When the expandtab setting changes automatically update the listchars setting
autocmd OptionSet expandtab call SetListchars(v:option_type)
" Also update the listchars when you open a file so tha modeline settings update
" the listchars setting automatically too
autocmd BufWinEnter * call SetListchars("local")
" Highlight matching braces plus angle brackets
set showmatch
set matchpairs+=<:>
" Don't send any terminal bells
set noerrorbells
set novisualbell
set belloff=all
" Thin line window seprators with utf8 characters
set encoding=utf8
set fileencoding=utf8
set fillchars=vert:│,stl:─,stlnc:─
hi VertSplit term=NONE cterm=NONE gui=NONE ctermbg=black ctermfg=grey
hi StatusLine term=NONE cterm=NONE gui=NONE ctermbg=black ctermfg=green
hi StatusLineNC term=NONE cterm=NONE gui=NONE ctermbg=black ctermfg=grey
" Golang autoformatting and set up :make and tabs indenting
" Install goimports with `go install golang.org/x/tools/cmd/goimports@latest` for automaticly managed import sections
" This setup uses the go vim plugins that used to come with the standard go installation package
" Copy paste https://go.googlesource.com/go/+/c4f5421/misc/vim/ftplugin/go/fmt.vim into ~/.vim/ftplugin/go/fmt.vim
let g:gofmt_command = "goimports"
autocmd FileType go setlocal makeprg=go\ build noexpandtab
autocmd BufWrite *.go if exists(":Fmt") | :Fmt
autocmd BufEnter *.go if !exists(":Fmt") | echo "Install the plugin (see vimrc comments) to get go autoformatting."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment