Skip to content

Instantly share code, notes, and snippets.

@felix-d
Last active March 11, 2018 18:54
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 felix-d/c5defd471c99617343d6a01aef5b8b34 to your computer and use it in GitHub Desktop.
Save felix-d/c5defd471c99617343d6a01aef5b8b34 to your computer and use it in GitHub Desktop.
Nvim conf
" vim: set sw=2 ft=vim ts=2 sts=2 et:
"""""""""""""""""""""""""""""""
" => BUNDLES
"""""""""""""""""""""""""""""""
call plug#begin('~/.vim/plugged')
Plug 'MarcWeber/vim-addon-mw-utils'
Plug 'tomtom/tlib_vim'
" Utils
Plug 'tpope/vim-surround'
Plug 'sheerun/vim-polyglot'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-unimpaired'
Plug 'tpope/vim-rhubarb'
Plug 'airblade/vim-rooter'
Plug 'christoomey/vim-tmux-navigator'
Plug 'jiangmiao/auto-pairs'
Plug 'vim-scripts/matchit.zip'
Plug 'gcmt/wildfire.vim'
Plug 'vim-scripts/DeleteTrailingWhitespace'
Plug 'tpope/vim-vinegar'
Plug 'AndrewRadev/splitjoin.vim'
Plug 'janko-m/vim-test'
Plug 'neomake/neomake'
Plug 'djoshea/vim-autoread'
" SCSS
Plug 'cakebaker/scss-syntax.vim'
" Autocomplete
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
Plug 'Shougo/neosnippet'
Plug 'Shougo/neosnippet-snippets'
Plug 'honza/vim-snippets'
" Navigation
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
" Python
Plug 'vim-scripts/indentpython.vim'
Plug 'vim-scripts/pythoncomplete'
" Rust
Plug 'rust-lang/rust.vim'
" Elixir
Plug 'elixir-lang/vim-elixir'
" UI
Plug 'joshdick/onedark.vim'
Plug 'flazz/vim-colorschemes'
Plug 'edkolev/tmuxline.vim'
Plug 'chriskempson/base16-vim'
Plug 'itchyny/lightline.vim'
" Yaml
Plug 'chase/vim-ansible-yaml'
" Markdown
Plug 'godlygeek/tabular'
Plug 'plasticboy/vim-markdown'
" Json
Plug 'elzr/vim-json'
" Javascript
Plug 'pangloss/vim-javascript'
Plug 'jelera/vim-javascript-syntax'
" HTML
Plug 'mattn/emmet-vim'
Plug 'alvan/vim-closetag'
" CSS
Plug 'hail2u/vim-css3-syntax'
" Misc
Plug 'mattn/gist-vim'
Plug 'tpope/vim-dispatch'
Plug 'benmills/vimux'
Plug 'christoomey/vim-tmux-runner'
" Ruby
Plug 'vim-ruby/vim-ruby'
Plug 'tpope/vim-endwise'
" Go
Plug 'fatih/vim-go'
call plug#end()
"""""""""""""""""""""""""""""""
" => GENERAL
"""""""""""""""""""""""""""""""
let mapleader=' ' " Just like spacemacs
scriptencoding utf-8
filetype plugin indent on
set shellpipe=2>&1\|\ tee\
set tags=./tags,tags;$HOME
set nofoldenable " No folds.
set mouse= " Automatically enable mouse usage
set mousehide " Hide the mouse cursor while typing
set virtualedit=onemore " Allow for cursor beyond last character
set iskeyword-=. " '.' is an end of word designator
set iskeyword-=# " '#' is an end of word designator
set iskeyword-=- " '-' is an end of word designator
set scrolljump=10
set linespace=0 " No extra spaces between rows
set re=1 " Force old regex engine for faster ruby syntax highlighting.
set number " Line numbers on
set showmatch " Show matching brackets/parenthesis
set nocursorline " No cursor line. It slows down terminal vim.
set nocursorcolumn " No cursor column
set norelativenumber " No relative number
set winminheight=0 " Windows can be 0 line high
set ignorecase " Case insensitive search
set smartcase " Case sensitive when uc present
set nolist " We don't want to see $ at the end of each line.
set nospell " No need for spell checking, its annoying.
set wildmode=list:longest,full " Command <Tab> completion, list matches, then longest common part, then all.
set scrolloff=1 " Minimum lines to keep above and below cursor
set nowrap " Do not wrap long lines
set shiftwidth=4 " Use indents of 4 spaces
set expandtab " Tabs are spaces, not tabs
set tabstop=4 " An indentation every four columns
set softtabstop=4 " Let backspace delete indent
set nojoinspaces " Prevents inserting two spaces after punctuation on a join (J)
set splitright " Puts new vsplit windows to the right of the current
set splitbelow " Puts new split windows to the bottom of the current
set nobackup " Why backups if git everywhere
set nowb " No need to make a backup before overwriting
set noswapfile " Swap files are annoying
set noerrorbells
set novisualbell
set tm=500
set clipboard=unnamed
set hidden
" Edit vim config (edit config)
nnoremap <leader>ec :vsplit $MYVIMRC<cr>
" ignore compiled files
set wildignore=*.o,*~,*.pyc
if has("win16") || has("win32")
set wildignore+=.git\*,.hg\*,.svn\*
else
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.ds_store
endif
" Instead of reverting the cursor to the last position in the buffer, we
" set it to the first line when editing a git commit message
au FileType gitcommit au! BufEnter COMMIT_EDITMSG call setpos('.', [0, 1, 1, 0])
" Return to last edit position when opening files (You want this!)
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"""""""""""""""""""""""""""""""
" => LANG
"""""""""""""""""""""""""""""""
autocmd FileType rust,html,ruby,yml,javascript,css,scss,vue,vue.html.javascript.css setlocal expandtab sw=2 sts=2 ts=2
autocmd FileType yml setlocal filetype=ansible
autocmd FileType markdown setlocal textwidth=80
autocmd FileType ex setlocal filetype=elixir
let g:closetag_filenames = "*.html,*.xhtml,*.phtml,*.vue,*.html.eex"
let g:out_of_explorer=0
"""""""""""""""""""""""""""""""
" => MISC KEY BINDINGS
"""""""""""""""""""""""""""""""
map 0 ^
nnoremap <leader>R :call CWDSearchAndReplace()<CR>
vnoremap <leader>R :call CWDSearchAndReplaceVisual()<CR>
nnoremap <leader>r :call FileSearchAndReplace()<CR>
vnoremap <leader>r :call FileSearchAndReplaceVisual()<CR>
nnoremap <leader>s :call SearchProject()<CR>
vnoremap <leader>s :call SearchProjectVisual()<CR>
"Changes the cwd to the current file directory
cmap cwd lcd %:p:h<CR>
" Because jk is overrated
inoremap df <Esc>
" Go to last buffer that exists. Just like spacemacs
noremap <Space><Tab> :b#<CR>
" Remove trailing white spaces on all buffers
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
" Pastetoggle (sane indentation on pastes)
set pastetoggle=<F12>
" Wrapped lines goes down/up to next row, rather than next line in file.
noremap j gj
noremap k gk
" Yank from the cursor to the end of the line, to be consistent with C and D.
nnoremap Y y$
" Toggle search highlight
nmap <silent> <leader>/ :nohlsearch<CR>
" Visual shifting (does not exit Visual mode)
vnoremap < <gv
vnoremap > >gv
" Allow using the repeat operator with a visual selection (!)
" http://stackoverflow.com/a/8064607/127816
vnoremap . :normal .<CR>
" Don't copy the contents of an overwritten selection.
vnoremap p "_dP"
" For when you forget to sudo.. Really Write the file.
cmap w!! w !sudo tee % >/dev/null
" Adjust viewports to the same size
map <Leader>= <C-w>=
" New file in same directory
map <Leader>fn :e <C-R>=expand("%:p:h") . "/" <CR>
" Change S to s for vim surround
xmap s <Plug>VSurround
" Tabs and buffers
map <Leader>tn :tabnew<cr>
map <Leader>to :tabonly<cr>
map <Leader>tc :tabclose<cr>
map <Leader>tm :tabmove
map <Leader>bn :bn<cr>
map <Leader>bp :bp<cr>
" Let 'tl' toggle between this and the last accessed tab
let g:lasttab = 1
nmap <Leader>tl :exe "tabn ".g:lasttab<CR>
au TabLeave * let g:lasttab = tabpagenr()
" Open new tab in same directory with nerd tree
map <leader>te :tabedit <c-r>=expand("%:p:h")<cr><cr>
" Scratch buffers
map <leader>xr :e ~/scratch.rb<cr>
map <leader>xs :e ~/scratch<cr>
map <leader>xm :e ~/scratch.md<cr>
" Add current file path to clipboard
nmap <leader>fp :let @+ = expand("%")<cr>
" Visual mode pressing * or # searches for the current selection
" Super useful! From an idea by Michael Naumann
vnoremap <silent> * :<C-u>call VisualSelection('', '')<CR>/<C-R>=@/<CR><CR>
vnoremap <silent> # :<C-u>call VisualSelection('', '')<CR>?<C-R>=@/<CR><CR>
"""""""""""""""""""""""""""""""
" => AUTOCOMPLETION
"""""""""""""""""""""""""""""""
let g:deoplete#enable_at_startup = 1
" These two lines conflict with the default digraph mapping of <C-K>
imap jk <Plug>(neosnippet_expand_or_jump)
" <TAB>: completion.
inoremap <expr><TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<TAB>"
" Set omnifunc functions
if has("autocmd") && exists("+omnifunc")
autocmd Filetype *
\if &omnifunc == "" |
\setlocal omnifunc=syntaxcomplete#Complete |
\endif
endif
autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS
autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags
autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS
autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags
autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
autocmd FileType ruby setlocal omnifunc=rubycomplete#Complete
" Close pum and new line if CR
inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
return (pumvisible() ? "\<C-y>" : "" ) . "\<CR>"
endfunction
" Close pum when going insert mode
au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
"""""""""""""""""""""""""""""""
" => AUTOCLOSE TAGS
"""""""""""""""""""""""""""""""
" Make it so AutoCloseTag works for xml and xhtml files as well
au FileType xhtml,xml ru ftplugin/html/autoclosetag.vim
nmap <Leader>ac <Plug>ToggleAutoCloseMappings
" call neomake#configure#automake('rw', 1000)
"""""""""""""""""""""""""""""""
" => PYTHON
"""""""""""""""""""""""""""""""
let python_highlight_all = 1
au FileType python setlocal expandtab sw=4 ts=4 sts=4
au FileType python syn keyword pythonDecorator True None False self cls
"""""""""""""""""""""""""""""""
" => RUBY
"""""""""""""""""""""""""""""""
au FileType ruby setlocal expandtab sw=2 ts=2 sts=2 textwidth=120
" let test#ruby#minitest#executable = 'echo rake > /dev/null 2>&1 && source ~/runtest.sh'
let test#strategy = "vtr"
" let g:neomake_ruby_enabled_makers = ["rubocop"]
" Set the rubocop executable after vim rooter has changed the directory with
" BufEnter
" au BufWinEnter *.rb :let b:neomake_ruby_rubocop_exe = system('PATH=$(pwd)/bin:$PATH && which rubocop | tr -d "\n"')
"""""""""""""""""""""""""""""""
" => Javascript
"""""""""""""""""""""""""""""""
au FileType javascript hi link jsFuncArgs GruvboxPurple
au FileType javascript syn match jsDecorator '@[a-zA-Z_][0-9a-zA-Z_$]*'
au FileType javascript hi link jsDecorator Function
"""""""""""""""""""""""""""""""
" => JSON
"""""""""""""""""""""""""""""""
nmap <leader>jt <Esc>:%!python -m json.tool<CR><Esc>:set filetype=json<CR>
let g:vim_json_syntax_conceal = 0
au FileType json setlocal expandtab sw=2 ts=2 sts=2
"""""""""""""""""""""""""""""""
" => Lua
"""""""""""""""""""""""""""""""
au FileType lua setlocal expandtab sw=2 ts=2 sts=2
"""""""""""""""""""""""""""""""
" => FZF
"""""""""""""""""""""""""""""""
" let rg_ignored_dirs = ['node_modules']
" let $FZF_DEFAULT_COMMAND = 'rg --column --line-number --no-heading --color=always '
" for dir_name in rg_ignored_dirs
" let $FZF_DEFAULT_COMMAND = $FZF_DEFAULT_COMMAND . ' --ignore-dir ' . dir_name
" endfor
"
command! -bang -nargs=* Rg
\ call fzf#vim#grep(
\ 'rg --column --line-number --no-heading --color=always '.shellescape(<q-args>), 1,
\ <bang>0 ? fzf#vim#with_preview('up:60%')
\ : fzf#vim#with_preview('right:50%:hidden', '?'),
\ <bang>0)
nnoremap <leader>fr :History<cr>
nnoremap <leader>ff :Files<cr>
nnoremap <leader>bb :Buffers<cr>
"""""""""""""""""""""""""""""""
" => FUGITIVE
"""""""""""""""""""""""""""""""
nnoremap <silent> <leader>gs :Gstatus<CR>
nnoremap <silent> <leader>gd :Gdiff<CR>
nnoremap <silent> <leader>gc :Gcommit<CR>
nnoremap <silent> <leader>gb :Gblame<CR>
nnoremap <silent> <leader>gl :Glog<CR>
nnoremap <silent> <leader>gp :Git push<CR>
nnoremap <silent> <leader>gr :Gread<CR>
nnoremap <silent> <leader>gw :Gwrite<CR>
nnoremap <silent> <leader>ge :Gedit<CR>
nnoremap <silent> <leader>gi :Git add -p %<CR>
"""""""""""""""""""""""""""""""
" => WILDFIRE
"""""""""""""""""""""""""""""""
let g:wildfire_objects = {
\ "*" : ["i'", 'i"', "i)", "i]", "i}", "ip"],
\ "html,xml" : ["at"],
\ }
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Turn persistent undo on
" means that you can undo even when you close a buffer/VIM
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
try
set undodir=~/.vim/temp_dirs/undodir
set undofile
catch
endtry
"""""""""""""""""""""""""""""""
" => UI
"""""""""""""""""""""""""""""""
let $NVIM_TUI_ENABLE_TRUE_COLOR=1
set termguicolors
let g:onedark_termcolors=16
let g:onedark_terminal_italics=1
colorscheme onedark
highlight Comment cterm=italic
let g:lightline = {
\ 'colorscheme': 'onedark',
\ }
"""""""""""""""""""""""""""""""
" => Functions
"""""""""""""""""""""""""""""""
" Add text to the command line.
" Don't ask me why it's like this, it's just is.
function! CmdLine(str)
exe "menu Foo.Bar :" . a:str
emenu Foo.Bar
unmenu Foo
endfunction
" Search and replace the visual selection in the file
" if the visual selection is made on a single line else
" it promps the user for a selection and replace inside
" the selection.
function! FileSearchAndReplaceVisual() range
call inputsave()
let visual_select = VisualSelection()
let newlines = matchstr(visual_select, '\n')
if empty(newlines)
let l:pattern = Escape(visual_select)
let replacement = input("Replace \"" . l:pattern . "\" with: ")
execute ":%s/" . l:pattern . "/" . replacement . "/gc"
else
let wordToReplace = input("Replace: ")
let replacement = input("Replace \"" . wordToReplace. "\" with: ")
execute a:firstline . "," . a:lastline . 's/' . wordToReplace . '/' . replacement . '/gc'
endif
call inputrestore()
endfunction
" Returns the word under cursor.
function! GetWordUnderCursor()
let isOnKeyword = matchstr(getline('.'), '\%'.col('.').'c.') =~# '\k'
if isOnKeyword
return expand("<cword>")
else
return ""
endif
endfunction
" Returns the visual selection
function! VisualSelection()
try
let a_save = @a
silent normal! gv"ay
return @a
finally
let @a = a_save
endtry
endfunction
" Use Rg with word under cursor
function! SearchProject()
call CmdLine("Rg " . GetWordUnderCursor())
endfunction
" Maps Rg result to quickfix list
function! s:rg_to_qf(line)
let parts = split(a:line, ':')
echo parts
return {'filename': parts[0], 'lnum': parts[1], 'col': parts[2],
\ 'text': join(parts[3:], ':')}
endfunction
" Wrapper over rg_to_qf
function! RgToQF(query)
call setqflist(map(systemlist('rg --column '.a:query), 's:rg_to_qf(v:val)'))
endfunction
function! Escape(toescape)
return escape(a:toescape, '\\/.*$^~[]<>{}')
endfunction
" Search the project for the visual selection
function! SearchProjectVisual()
call CmdLine("Rg " . Escape(VisualSelection()))
endfunction
" Search and replace the visual selection in the project
function! CWDSearchAndReplaceVisual()
call inputsave()
let visual_select = Escape(VisualSelection())
let replacement = input("Replace \"" . visual_select . "\" with: ")
if visual_select != ''
call RgToQF(visual_select)
exec "cdo s/" . visual_select . "/" . replacement ."/gc"
endif
call inputrestore()
endfunction
" Search and replace the word under cursor in the CWD
function! CWDSearchAndReplace()
call inputsave()
let wordToReplace = input("Replace : ", GetWordUnderCursor())
let replacement = input("Replace \"" . wordToReplace . "\" with: ")
call RgToQF(wordToReplace)
exec "cdo s/" . wordToReplace . "/" . replacement ."/gc"
call inputrestore()
endfunction
" Search and replace the word under cursor in the current buffer
function! FileSearchAndReplace()
call inputsave()
let wordToReplace = input("Replace: ", GetWordUnderCursor())
let replacement = input("Replace \"" . wordToReplace . "\" with: ")
execute ":%s/" . wordToReplace . "/" . replacement . "/gc"
call inputrestore()
endfunction
fun! <SID>StripTrailingWhitespaces()
let l = line(".")
let c = col(".")
%s/\s\+$//e
call cursor(l, c)
endfun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment