Skip to content

Instantly share code, notes, and snippets.

@SamuelNorbury
Last active January 26, 2024 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamuelNorbury/5606b28a789f5d5b22794a972dc127ff to your computer and use it in GitHub Desktop.
Save SamuelNorbury/5606b28a789f5d5b22794a972dc127ff to your computer and use it in GitHub Desktop.
" Samuel Norbury's Vimrc
"-----------------------
"
" Plugins using vimplug {{{
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
"
" Automatic light mode / dark mode switcher
"function! ChangeColorScheme(channel, msg)
"let time = trim(a:msg)
"if time ==# "day"
"set background=light
"colorscheme solarized
"else
"set background=dark
"jolorscheme solarized
"endif
"endfunction
"function! Sunshine(timer)
"let job = job_start(["sunshine", "-s", "#Haarlem"], {"out_cb": "ChangeColorScheme"})
"endfunction
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-abolish'
Plug 'tpope/vim-dispatch'
Plug 'rfratto/vim-river'
Plug 'mileszs/ack.vim'
Plug 'ctrlpvim/ctrlp.vim'
Plug 'tpope/vim-surround'
Plug 'machakann/vim-highlightedyank'
Plug 'scrooloose/nerdcommenter'
Plug 'mtscout6/vim-cjsx'
Plug 'kchmck/vim-coffee-script'
Plug 'mxw/vim-jsx'
Plug 'leafgarland/typescript-vim'
Plug 'rust-lang/rust.vim'
Plug 'jvirtanen/vim-hcl'
Plug 'sheerun/vim-polyglot'
Plug 'dense-analysis/ale'
Plug 'luochen1990/rainbow'
call plug#end()
" }}}
"
"
" Misc {{{
"let timer = timer_start(600000, 'Sunshine', {'repeat': -1})
"call Sunshine(timer) " Initial call to setup the theme
set background=light
set encoding=utf-8
syntax on
let g:ale_fix_on_save = 1
let g:ale_completion_enabled = 1
syntax enable
set clipboard=unnamedplus
let $BASH_ENV = "~/.bash_aliases"
set nrformats=
let g:vimwiki_list = [{'path': '~/.wiki/'}]
"let g:rainbow_active = 1
set tabstop=4 expandtab softtabstop=4
set shiftwidth=4
set modelines=0
set completeopt-=preview
filetype indent on
filetype plugin on
set list
set listchars=tab:»\ ,extends:›,precedes:‹,nbsp:·,trail:·
set number relativenumber
set lazyredraw
set showmatch
set undofile undodir=~/.vim/undo undolevels=1000 undoreload=10000
set cc=+1
set nocompatible
set scrolloff=3
set autoindent
set showmode
set showcmd
set hidden
set wildmenu
set wildmode=list:longest
set visualbell
set cursorline
set ttyfast
set ruler
set backspace=indent,eol,start
set laststatus=2
" }}}
"
" Formats the statusline
set statusline=%f " file name
set statusline+=\ [%{strlen(&fenc)?&fenc:'none'}, "file encoding
set statusline+=\ %{&ff}] "file format
set statusline+=\ %y "filetype
set statusline+=%h "help file flag
set statusline+=%m "modified flag
set statusline+=%r "read only flag
set statusline+=\ %= " align left
set statusline+=%l:%c/%L " line X of Y
set statusline+=\ %{FugitiveStatusline()} " git branch
" }}}
"
"
" Searching {{{
set ignorecase " ignore case when searching
set smartcase " except for if we put an uppercase letter in the search
set incsearch " search as characters are entered
set hlsearch " highlight all matches
" }}}
"
" Folding {{{
"=== folding ===
set foldmethod=indent " fold based on indent level
set foldnestmax=10 " max 10 depth
set foldenable " don't fold files by default on open
nnoremap <space> za
set foldlevelstart=10 " start with fold level of 1
" }}}
"
" General remaps {{{
nnoremap j gj
nnoremap k gk
nnoremap Y y$
nnoremap gV `[v`]
nnoremap Q @@
inoremap jj <ESC>
" }}}
"
" Split Shortcuts {{{
nnoremap <c-j> <c-w>j
nnoremap <c-k> <c-w>k
nnoremap <c-l> <c-w>l
nnoremap <c-h> <c-w>h
" }}}
"
" Leader Shortcuts {{{
let mapleader=","
nnoremap <Leader>. :CtrlPTag<cr>
nnoremap <Leader>a :Ack!<Space>
nnoremap <Leader>g :Git blame<CR>
nnoremap <leader>W :%s/\s\+$//<cr>:let @/=''<CR>
"" }}}
"
" CtrlP {{{
set runtimepath^=~/.vim/bundle/ctrlp.vim
let g:ctrlp_match_window = 'bottom,order:ttb,max:20'
let g:ctrlp_switch_buffer = 0
let g:ctrlp_working_path_mode = 0
let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlP'
let g:ctrlp_lazy_update = 200
let g:ctrlp_cache_dir = $HOME . '/.cache/ctrlp'
if executable('ag')
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
endif
" }}}
"
" AutoGroups {{{
augroup configgroup
autocmd!
autocmd VimEnter * highlight clear SignColumn
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
autocmd BufWritePost *.pkr.hcl,*.tf,*.php,*.jsx,*.js,*.py :Dispatch!
autocmd BufEnter *.sh setlocal tabstop=4
autocmd BufEnter *.sh setlocal shiftwidth=4
autocmd BufEnter *.sh setlocal softtabstop=4
autocmd BufEnter *.py setlocal tabstop=4
autocmd BufEnter *.md setlocal ft=markdown
autocmd FileType markdown setlocal spell spelllang=en_gb
autocmd FocusLost * :wa
augroup END
" }}}
let b:dispatch = 'true'
if executable("php")
autocmd FileType php let b:dispatch = '/bin/env bash -c "./vendor/bin/php-cs-fixer fix --config ./tools/PhpCsFixer/.php-cs-fixer.dist.php % &>/dev/null && ./vendor/bin/phpmd % github ./tools/PhpMd/phpmd.xml --suffixes=php"'
autocmd FileType dockerfile let b:dispatch = 'docker run -i hadolint/hadolint < %'
endif
if executable("yarn")
autocmd FileType javascript.jsx,javascript let b:dispatch = '/bin/env bash -c "yarn eslint --fix % &>/dev/null && yarn eslint %"'
endif
if executable("terraform")
autocmd FileType terraform let b:dispatch = 'terraform fmt %'
endif
if executable("packer")
autocmd FileType hcl let b:dispatch = 'packer fmt %'
endif
if executable("black")
autocmd FileType python let b:dispatch = 'black % && pylint %'
endif
" strips trailing whitespace at the end of files. this
" is called on buffer write in the autogroup above.
function! <SID>StripTrailingWhitespaces()
" save last search & cursor position
let _s=@/
let l = line(".")
let c = col(".")
%s/\s\+$//e
let @/=_s
call cursor(l, c)
endfunction
" Backups {{{
set backup
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set backupskip=/tmp/*,/private/tmp/*
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set writebackup
" }}}
"
" Move lines up and down with ctrl {{{
noremap <c-up> :call feedkeys( line('.')==1 ? '' : 'ddkP' )<CR>
noremap <c-down> ddp
" }}}
"
" No more shift for colon! {{{
nnoremap ; :
nnoremap : ;
noremap <silent> <C-S> :w<CR><ESC>
noremap <silent> <C-b> :w !sudo tee %<CR>
vnoremap <silent> <C-S> <C-C>:w<CR><ESC>
inoremap <silent> <C-S> <C-O>:w<CR><ESC>
" }}}
"
" hold onto lines that are being indented {{{
vnoremap < <gv
vnoremap > >gv
" }}}
"
" Triger `autoread` when files changes on disk
" https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
" https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI *
\ if mode() !~ '\v(c|r.?|!|t)' && getcmdwintype() == '' | checktime | endif
" Notification after file change
" https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
autocmd FileChangedShellPost *
\ echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None
"
" Use ag from vim {{{
let g:ackprg = 'ag --silent --vimgrep --smart-case'
cnoreabbrev Ack Ack!
nnoremap # *:AckFromSearch<CR>
nnoremap <Leader># *:AckFromSearch -u<CR>
" }}}
if &term =~ '^screen'
" tmux will send xterm-style keys when its xterm-keys option is on
execute "set <xUp>=\e[1;*A"
execute "set <xDown>=\e[1;*B"
execute "set <xRight>=\e[1;*C"
execute "set <xLeft>=\e[1;*D"
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment