Tovi Jaeschke

Links:

My Neovim Configuration

I love vim. I think its a brilliant editor, that makes editing code (and plain text) much more fluid than any other editor (and you don't need 10 fingers on each hand, looking at you Emacs!).

For this post, I'm going to share my vimrc, or more accurately my init.vim, and break down why I have it setup in the way that I do.


TLDR: My dotfiles can be found here

Setup and Plugins

  
let mapleader =","

if ! filereadable(expand('~/.config/nvim/autoload/plug.vim'))
	echo "Downloading junegunn/vim-plug to manage plugins..."
	silent !mkdir -p ~/.config/nvim/autoload/
	silent !curl "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" > ~/.config/nvim/autoload/plug.vim
	autocmd VimEnter * PlugInstall
endif

call plug#begin('~/.config/nvim/plugged')

  Plug 'neoclide/coc.nvim', {'branch': 'release'}

  Plug 'nvim-lua/popup.nvim'
  Plug 'nvim-lua/plenary.nvim'
  Plug 'nvim-telescope/telescope.nvim'
  Plug 'nvim-telescope/telescope-fzy-native.nvim'

  Plug 'ap/vim-css-color'

  Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
  Plug 'darrikonn/vim-gofmt', { 'do': ':GoUpdateBinaries' }

call plug#end()

  

The first section is just setting my mapleader to the key that I like. I see the space key getting used quite a bit by other people, which I don't particularly like because it feels like vim is a bit sluggish when typing, so I prefer the "," key. Next I'm just making sure that "plug" is up to date.

I generally find people using a lot of plugins, and I don't really think thats necessary. I find a good auto-completer, navigation helper, and some stuff to format the languages you work in often to be sufficient.

Sane defaults

  
" Some basics:
  set go=a
  set mouse=a
  set nohlsearch
  set clipboard+=unnamedplus
  set incsearch
  set ignorecase
  set smartcase
  set linebreak

  set noswapfile
  set nobackup
  set undodir=~/.config/nvim/undodir
  set undofile

	nnoremap c "_c
	set nocompatible
	filetype plugin on
	syntax on
	set encoding=utf-8
	set number relativenumber

" Tab 2 spaces
	filetype plugin indent on
  set tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab

" Disables automatic commenting on newline:
	autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o

" Remember last cursor position
	autocmd BufReadPost * if @% !~# '\.git[\/\\]COMMIT_EDITMSG$' && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif

  

These are fairly self explanatory. The most notable of the above is probably the set clipboard+=unnamedplus, which sets the default register to the system clipboard, and set nohlsearch, which turns off the highlighting when you search for something.

CoC

  
" coc
  inoremap <silent><expr> <TAB>
        \ pumvisible() ? "\<C-n>" :
        \ <SID>check_back_space() ? "\" :
        \ coc#refresh()
  inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

  function! s:check_back_space() abort
    let col = col('.') - 1
    return !col || getline('.')[col - 1]  =~# '\s'
  endfunction

  nmap <leader>gd <Plug>(coc-definition)
  nmap <leader>gr <Plug>(coc-references)
  nmap <leader>rr <Plug>(coc-rename)
  nnoremap <leader>prw :CocSearch <C-R>=expand("<cword>")<CR><CR>

  inoremap <expr> <C-j> pumvisible() ? "\<Down>" : "\<C-j>"
  inoremap <expr> <C-k> pumvisible() ? "\<Up>" : "\<C-k>"
  

This lets me tab through auto completion results, or use Ctrl-j and k to move up and down. It also binds keys to the most useful coc commands, such as jumping to definitions and references, and renaming something.

Telescope

  
" Telescope remaps
  lua require("telescope")

  nnoremap <leader>gs :lua require('telescope.builtin').grep_string({ search = vim.fn.input("Grep For > ")})<CR>
  nnoremap <C-q> :lua require('telescope.builtin').git_files()<CR>
  nnoremap <C-a> :lua require('telescope.builtin').find_files()<CR>

  nnoremap <leader>fb <cmd>lua require('telescope.builtin').buffers()<cr>
  

Telescope is excellent for quickly navigating around a codebase. As you can see, I've bound keys to grep for strings, search for files, and reopen buffers easily. The lua require("telescope") initialises telescope with the setup function, with some keybindings for navigating the modal.

Remaps

  
" Shortcutting split navigation, saving a keypress:
	nnoremap <C-h> <C-w>h
	nnoremap <C-j> <C-w>j
	nnoremap <C-k> <C-w>k
	nnoremap <C-l> <C-w>l

" Replace all is aliased to S.
	nnoremap <c-s> :%s//g<Left><Left>

" For chrome extension development
  nnoremap <c-h> :set syntax=

" Navigating with guides
	inoremap <leader><leader> <Esc>/<++><Enter>"_c4l
	vnoremap <leader><leader> <Esc>/<++><Enter>"_c4l
	map <leader><leader> <Esc>/<++><Enter>"_c4l

" Copy entire document
  noremap <leader>y <Esc>ggyG<C-o>
  map <leader>y <Esc>ggyG<C-o>

" Set gohtml template files to html syntax
  autocmd BufNewFile,BufRead *.gohtml set syntax=html
  

Here, I just map some of my most used commands to some quick key presses.