Skip to content

Instantly share code, notes, and snippets.

@benfrain benfrain/init.vim
Last active Nov 12, 2019

Embed
What would you like to do?
neovim config
if &compatible
set nocompatible " Be iMproved
endif
let mapleader=","
" Required:
filetype plugin indent on
" Only set syntax highlighting once!
if !exists("g:syntax_on")
syntax enable
endif
call plug#begin('~/.config/nvim/plugged')
Plug 'leafgarland/typescript-vim'
Plug 'alvan/vim-closetag'
Plug 'jiangmiao/auto-pairs'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'tpope/vim-surround'
Plug 'wellle/targets.vim'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-fugitive'
Plug 'vim-airline/vim-airline'
Plug 'scrooloose/nerdcommenter'
Plug 'arcticicestudio/nord-vim'
Plug 'vim-airline/vim-airline-themes'
Plug 'tommcdo/vim-exchange'
Plug '/usr/local/opt/fzf'
Plug 'junegunn/fzf.vim'
Plug 'prettier/vim-prettier', { 'do': 'yarn install' }
Plug 'ryanoasis/vim-devicons'
call plug#end()
" Te functions handle saving and restoring buffers
"
"
function! MakeSession()
let b:sessiondir = $HOME . "/.config/nvim/sessions" . getcwd()
if (filewritable(b:sessiondir) != 2)
exe 'silent !mkdir -p ' b:sessiondir
redraw!
endif
let b:filename = b:sessiondir . '/session.vim'
exe "mksession! " . b:filename
endfunction
function! LoadSession()
let b:sessiondir = $HOME . "/.config/nvim/sessions" . getcwd()
let b:sessionfile = b:sessiondir . "/session.vim"
if (filereadable(b:sessionfile))
exe 'source ' b:sessionfile
else
echo "No session loaded."
endif
endfunction
" Controls the open/close Vim functions
augroup vimSessions
autocmd!
" Adding automatons for when entering or leaving Vim
au VimEnter * nested :call LoadSession()
au VimLeave * :call MakeSession()
augroup END
augroup markdownSpell
autocmd!
autocmd FileType markdown setlocal spell
autocmd BufNewFile,BufRead *.txt set syntax=markdown
autocmd FileType text setlocal spell
autocmd BufRead,BufNewFile *.md,*.txt setlocal spell
" Stop any default wrapping when in txt or markdown
set textwidth=0
" Tell limelight how to handle dimming – needed for Nord
" When editing a file, always jump to the last known cursor position.
" Don't do it for commit messages, when the position is invalid, or when
" inside an event handler (happens when dropping a file on gvim).
autocmd BufReadPost *
\ if &ft != 'gitcommit' && line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ endif
augroup END
" Use fd instead of find so node_modules doesn't get indexed
let FZF_DEFAULT_COMMAND='fd --type f'
"
" This was the older way I used to preview files in FZF.
" This makes FZF give a 20 line preview
nnoremap <c-p> :FZF --preview=head\ -20\ {}<cr>
"nnoremap <c-p> :FZFg
" Then I happended upon this:
" Files with bat previewer
" https://www.reddit.com/r/vim/comments/9xpb18/file_preview_with_fzf_rg_bat_and_devicons/
" which references this:
nnoremap <silent> <leader>f :call Fzf_dev()<CR>
" ripgrep
if executable('rg')
let $FZF_DEFAULT_COMMAND = 'rg --files --hidden --follow --glob "!.git/*"'
set grepprg=rg\ --vimgrep
command! -bang -nargs=* Find call fzf#vim#grep('rg --column --line-number --no-heading --fixed-strings --ignore-case --hidden --follow --glob "!.git/*" --color "always" '.shellescape(<q-args>).'| tr -d "\017"', 1, <bang>0)
endif
" Files + devicons
function! Fzf_dev()
let l:fzf_files_options = '--preview "bat --theme="OneHalfDark" --style=numbers,changes --color always {2..-1} | head -'.&lines.'"'
function! s:files()
let l:files = split(system($FZF_DEFAULT_COMMAND), '\n')
return s:prepend_icon(l:files)
endfunction
function! s:prepend_icon(candidates)
let l:result = []
for l:candidate in a:candidates
let l:filename = fnamemodify(l:candidate, ':p:t')
let l:icon = WebDevIconsGetFileTypeSymbol(l:filename, isdirectory(l:filename))
call add(l:result, printf('%s %s', l:icon, l:candidate))
endfor
return l:result
endfunction
function! s:edit_file(item)
let l:pos = stridx(a:item, ' ')
let l:file_path = a:item[pos+1:-1]
execute 'silent e' l:file_path
endfunction
call fzf#run({
\ 'source': <sid>files(),
\ 'sink': function('s:edit_file'),
\ 'options': '-m ' . l:fzf_files_options,
\ 'down': '40%' })
endfunction
" Pressing ctrl+b gives me a quick buffer chooser
"nnoremap <c-b> :buffers<cr>
nnoremap <Leader>b :Buffers<CR>
if !&scrolloff
set scrolloff=3 " Show next 3 lines while scrolling.
endif
if !&sidescrolloff
set sidescrolloff=5 " Show next 5 columns while side-scrolling.
endif
augroup numbertoggle
autocmd!
autocmd BufEnter,FocusGained,InsertLeave * set relativenumber
autocmd BufLeave,FocusLost,InsertEnter * set norelativenumber
augroup END
" Prettier - to enable format on save
let g:prettier#autoformat = 0
augroup prettier
autocmd!
autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html PrettierAsync
augroup END
" Airline configuration
let g:airline_theme='nord'
let g:airline#extensions#coc#enabled = 1
" Just show the filename (no path) in the tab
let g:airline#extensions#tabline#fnamemod = ':t'
let g:airline_powerline_fonts = 1
" Don't bother telling me about whitespace
silent! call airline#extensions#whitespace#disable()
" Don't bother telling me about spelling
let g:airline_detect_spell=0
" Coc Vim stuff
" Better display for messages
set cmdheight=2
" You will have bad experience for diagnostic messages when it's default 4000.
set updatetime=300
" don't give |ins-completion-menu| messages.
set shortmess+=c
" always show signcolumns
set signcolumn=yes
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
inoremap <silent><expr> <TAB>
\ pumvisible() ? coc#_select_confirm() :
\ coc#expandableOrJumpable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ coc#refresh()
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
let g:coc_snippet_next = '<tab>'
" Use `[g` and `]g` to navigate diagnostics
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)
"""""""""""""""End Coc Vim Config"""""""""""""""
"" After searching, pressing escape stops the highlight
nnoremap <silent> <esc> :noh<cr><esc>
" Open nvimrc file
nnoremap <Leader>v :vsp $MYVIMRC<CR>
" Source nvimrc file
nnoremap <Leader>sv :source $MYVIMRC<CR>
" Quick new file
nnoremap <Leader>n :enew<CR>
" Easy open devDiary file
nnoremap <Leader>od :edit ~/Desktop/DevDiary/diary.md<CR>
" Easy select all of file
nnoremap <Leader>sa ggvG<c-$>
" Easy show registers
nnoremap <silent> <Leader>zz :reg<CR>
" Make visual yanks pace the cursor back where started
" adding lazyredraw as it should help with the flicker
set lazyredraw
vmap y ygv<Esc>
" Line bubbling
nnoremap <silent> <c-j> :m .+1<CR>==
nnoremap <silent> <c-k> :m .-2<CR>==
inoremap <silent> <c-j> <Esc>:m .+1<CR>==gi
inoremap <silent> <c-k> <Esc>:m .-2<CR>==gi
vnoremap <silent> <c-j> :m '>+1<CR>gv=gv
vnoremap <silent> <c-k> :m '<-2<CR>gv=gv
"Auto close tags
imap ,/ </<C-X><C-O>
" Make coc see CSS as SCSS
let g:coc_filetype_map = {
\ 'css': 'scss',
\ }
if exists('+termguicolors') && ($TERM == "st-256color" || $TERM == "tmux-256color")
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
set termguicolors
endif
" Nord theme config
let g:nord_italic = 1
let g:nord_italic_comments = 1
let g:nord_underline = 1
let g:nord_uniform_status_lines = 1
let g:nord_uniform_diff_background = 1
let g:nord_cursor_line_number_background = 1
colorscheme nord
" Set matching bracket for 1 second
set mat=1
" Cursor line but only current window and not when inserting text
set cursorline
autocmd InsertLeave,WinEnter * set cursorline
autocmd InsertEnter,WinLeave * set nocursorline
" Use spelling for markdown files ‘]s’ to find next, ‘[s’ for previous, 'z=‘ for suggestions when on one.
" Source: http://thejakeharding.com/tutorial/2012/06/13/using-spell-check-in-vim.html
set spelllang=en
set wrap linebreak nolist
set formatoptions=l
set hidden
" Make the gutter for numbers wider by default
set numberwidth=5
set display+=lastline
set clipboard=unnamedplus
set mouse=
set ruler " Show the line and column numbers of the cursor.
set autoread
set noshowmode " Show current mode.
set encoding=utf-8 " Set default encoding to UTF-8
set incsearch " Shows the match while typing
set hlsearch " Highlight found searches
set ignorecase " Search case insensitive...
set smartcase " ... but not when search pattern contains upper case characters
set autoindent
set tabstop=4 shiftwidth=4 expandtab
"set gdefault " use g flag by default on searches
set number relativenumber
" Autocomplete with dictionary words when spell check is on
set complete+=kspell
"Here are things you will need to get this working:
"brew install bat
"brew install ripgrep
"you will also need to create a :CocConfig JSON with the following: "suggest.noselect": false
"You will need a patched font that has devicons in from Nerd font: https://github.com/ryanoasis/nerd-fonts#font-installation
@benfrain

This comment has been minimized.

Copy link
Owner Author

benfrain commented Oct 18, 2019

Added the exchange plugin for swapping sections of text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.