HomeFontsAbout

Neovim configuration

Updated on 2021-11-24

I recently decided to stop using Visual Studio Code and to go back to using Vim, primarily to avoid telemetry and other Microsoft induced nonsense. Neovim seems to be better maintained and feature rich than Vim classic, so that is what I am using. So far I have not really been missing Code and I expect Neovim to work out long term.

Requirements

I have a basic set of requirements that, I expect, is more likely to shrink than grow over time.

I have not yet achieved the last point 🙁, but I will keep working on it!

Lua aliases

These aliases make it easier to write Neovim configs in Lua.

local opt = vim.opt
local g = vim.g
local viml = vim.cmd

Sets

Some essential improvements: enable mouse in all modes, use system clipboard, enabled hidden buffers.

opt.mouse = 'a'
opt.clipboard = "unnamedplus"
opt.hidden = true

Some cosmetic improvements: line numbers, use number column for linting, show eighty character gutter line, keep four lines of vertical context, don't show ins-completion-menu messages, reduce updatetime. Also, disable swap and backup files, as I don't like them.

opt.number = true
opt.signcolumn = "number"
opt.colorcolumn = { 80 }
opt.scrolloff = 4
opt.shortmess = opt.shortmess + 'c'
opt.updatetime = 50
opt.swapfile = false
opt.writebackup = false

Make tabs work as I like them: use spaces over tabs, set all widths to four characters, use smart indent.

opt.expandtab = true
opt.tabstop = 4
opt.softtabstop = 4
opt.shiftwidth = 4
opt.smartindent = true

Enable spell checking for English. For a list of commands check the docs.

opt.spell = true
opt.spelllang = "en_us"

Plugins

I am installing vim plugins using vim-plug. In general, I try to keep the set of plugins as small as possible. I will go over each of these plugins in there own section. For simplicity, I am using a block of Viml to configure vim-plug.

viml [[
call plug#begin('~/.local/share/nvim/plugged')
  Plug 'tjdevries/colorbuddy.vim'
  Plug 'Th3Whit3Wolf/onebuddy'

  Plug 'preservim/nerdtree'

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

  Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()
]]

Leader key

For most of my shortcut mappings I will use a leader key and I set this to be space.

g.mapleader = ' '

Remaps (non-plugin)

The only non-plugin mapping I have is to enable moving lines (and blocks of lines) by holding alt and using arrow keys (can change to j/k).

viml [[
nnoremap <A-Down> :m .+1<CR>==
nnoremap <A-Up>   :m .-2<CR>==
inoremap <A-Down> <Esc>:m .+1<CR>==gi
inoremap <A-Up>   <Esc>:m .-2<CR>==gi
vnoremap <A-Down> :m '>+1<CR>gv=gv
vnoremap <A-Up>   :m '<-2<CR>gv=gv
]]

Color theme

I am not attached to this color scheme, but it is a reasonable light theme, so I am using it for now. I did need to fix the spelling error and coc floating window coloring.

require("colorbuddy").colorscheme("onebuddy", true)
viml [[
highlight SpellBad   guifg=none guisp=#ff0000
highlight SpellLocal guifg=none guisp=#ff0000
highlight SpellCap   guifg=none guisp=#ff0000
highlight SpellRare  guifg=none guisp=#ff0000
highlight CocFloating guibg=#c2c2c2
]]

Nerd tree

For a file explorer I am using NERDTree with the settings below. I have no complaints. I briefly tried using netrw, but did not like it for some reason that I forget now.

g.NERDTreeShowHidden = 1
g.NERDTreeMouseMode  = 2
g.NERDTreeMinimalUI  = 1
viml [[
nnoremap <leader>n <cmd>NERDTreeToggleVCS<cr><cmd>NERDTreeRefreshRoot<cr>
nnoremap <leader>r <cmd>NERDTreeFind<cr><cmd>NERDTreeRefreshRoot<cr>
]]

Telescope

For all my searching needs, I use Telescope and love it! I have created shortcuts for: fuzzy finding files by name, grepping all files in a project, fuzzy finding open buffers, searching the help files, and spelling suggestions. I also set a custom vimgrep_arguments to be able to search hidden files and still ignore the .git folder.

To get this all to work you will need to install fd and ripgrep.

require("telescope").setup({
  defaults = {
    vimgrep_arguments = {
      'rg',
      '--color=never',
      '--no-heading',
      '--with-filename',
      '--line-number',
      '--column',
      '--smart-case',
      '--hidden',
      '--glob=!.git/*'
    },
  },
})
viml [[
nnoremap <leader>p <cmd>lua require('telescope.builtin').git_files()<cr>
nnoremap <leader>g <cmd>lua require('telescope.builtin').live_grep()<cr>
nnoremap <leader>b <cmd>lua require('telescope.builtin').buffers()<cr>
nnoremap <leader>h <cmd>lua require('telescope.builtin').help_tags()<cr>
nnoremap <leader>z <cmd>lua require('telescope.builtin').spell_suggest()<cr>
]]

COC

For programming language support (linting, completion, etc...) I am using the Conquer of Completion Vim plugin with extensions listed below. This extension is what brings Vim closer to an IDE than a simple text editor. I go back and forth on if this is a good thing or a bad thing. Additionally, this is where I expose myself to both telemetry and Microsoft code 😤.

g.coc_global_extensions = {
  'coc-json',
  'coc-html', -- telemetry concern
  'coc-emmet',
  'coc-css',
  'coc-eslint',
  'coc-emoji',
  'coc-prettier',
  'coc-tsserver', -- telemetry concern
  'coc-pyright', -- telemetry concern
  'coc-clangd',
  'coc-markdownlint',
  'coc-sumneko-lua', -- telemetry concern
  'coc-htmlhint',
}
viml [[
  nmap <leader>gd <Plug>(coc-definition)
  nmap <leader>gt <Plug>(coc-type-definition)
  nmap <leader>gi <Plug>(coc-implementation)
  nmap <leader>gr <Plug>(coc-references)
]]

I also have the following in my coc-settings.json to disable Markdown linting.

{
  "markdownlint.onOpen": false,
  "markdownlint.onSave": false,
  "markdownlint.onChange": false
}

Conclusion

This was pretty easy to setup and I am happy with the environment. I will update this page every few months with my current setup.


Copyright © 2020-2021 thestuffido.xyz All Rights Reserved