Info
This is the second part of the personalized development environment series, every part depends heavily on the previous one
Coc allows us to install vscode extensions and use language servers originially written for vscode
Installing coc
Add the highlighted lines 16 and 17 to nvim/lua/plugins.lua
1-- i use vimplug to manage my plugins
2-- this checks if vimplug is installed, if not install it
3vim.cmd([[
4if empty(glob('~/.config/nvim/autoload/plug.vim'))
5 silent !curl -fLo ~/.config/nvim/autoload/plug.vim --create-dirs
6 \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
7endif
8]])
9
10-- assign variable
11local Plug = vim.fn['plug#']
12
13
14-- in between this we call Plug to specify plugins
15vim.call('plug#begin', '~/.config/nvim/plugged')
16 -- vscode extension provider
17 Plug 'neoclide/coc.nvim'
18
19 -- display buffers and tabs nicely
20 Plug 'akinsho/bufferline.nvim'
21
22
23 -- color theme / sheme
24 Plug('folke/tokyonight.nvim', { branch = 'main' })
25
26
27 -- comment helper
28 Plug 'tpope/vim-commentary'
29
30
31 -- status line
32 Plug 'nvim-lualine/lualine.nvim'
33
34
35 -- file explorer
36 Plug 'nvim-tree/nvim-tree.lua'
37
38
39 -- icons for everything, file explorer, tabs, statusline
40 Plug 'nvim-tree/nvim-web-devicons'
41
42
43 -- automatically add "([ pairs if first one is typed
44 Plug 'jiangmiao/auto-pairs'
45
46
47 -- startup interface
48 Plug 'mhinz/vim-startify'
49
50vim.call('plug#end')
Installing and using coc Extensions
To install extensions, coc exposes the CocInstall
command:
1:CocInstall <extension>
Prettier
Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
Install Prettier:
open neovim and run the following command:
:CocInstall coc-prettier
register a new
:Prettier
command by adding the highlighted lines in yourplugin-options.lua
1-- set colorsheme
2vim.cmd([[colorscheme tokyonight-night]])
3
4-- bufferline config
5require("bufferline").setup{
6 options = {
7 -- only display tabs, hide buffers
8 mode = "tabs",
9
10 -- style for kitty terminal
11 separator_style = "slant",
12
13 -- display coc diagnostics
14 diagnostics = "coc"
15 }
16}
17
18-- nvim tree setup
19require("nvim-tree").setup()
20
21-- lualine setup
22require("lualine").setup{
23 options = {
24 theme = "palenight"
25 }
26}
27
28-- registering :Prettier command
29vim.cmd([[command! -nargs=0 Prettier :CocCommand prettier.forceFormatDocument]])
- reload the config by running
:source %
Coc keybindings
Add the highlighted lines to the keybindings.lua
file:
1-- helper for mapping custom keybindings
2-- source: https://gist.github.com/Jarmos-san/d46605cd3a795513526448f36e0db18e#file-example-keymap-lua
3function map(mode, lhs, rhs, opts)
4 local options = { noremap = true }
5 if opts then
6 options = vim.tbl_extend("force", options, opts)
7 end
8 vim.api.nvim_set_keymap(mode, lhs, rhs, options)
9end
10
11-- toggle the nvim tree sidebar with ctrl+b
12map("n", "<C-b>", ":NvimTreeToggle<CR>", {silent = true})
13
14-- move visual selection down with shift+J
15map("v", "J", ":m '>+1<CR>gv=gv")
16-- move visual selection up with shift+K
17map("v", "K", ":m '<-2<CR>gv=gv")
18
19-- taken from the coc.nvim example config:
20-- https://github.com/neoclide/coc.nvim
21function _G.show_docs()
22 local cw = vim.fn.expand('<cword>')
23 if vim.fn.index({'vim', 'help'}, vim.bo.filetype) >= 0 then
24 vim.api.nvim_command('h ' .. cw)
25 elseif vim.api.nvim_eval('coc#rpc#ready()') then
26 vim.fn.CocActionAsync('doHover')
27 else
28 vim.api.nvim_command('!' .. vim.o.keywordprg .. ' ' .. cw)
29 end
30end
31
32-- autocomplete
33function _G.check_back_space()
34 local col = vim.fn.col('.') - 1
35 return col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') ~= nil
36end
37
38-- view the definition of the currently hovering over element
39map("n", "gd", "<Plug>(coc-definition)", {silent = true})
40-- view a list of the references of the currently hovering over element
41map("n", "gr", "<Plug>(coc-references)", {silent = true})
42-- view documentation for the currently hovering over element
43map("n", "K", "<CMD>lua _G.show_docs()<CR>", {silent = true})
44
45-- use Tab to trigger completion for the currently selected completion
46local opts = {silent = true, noremap = true, expr = true, replace_keycodes = false}
47map("i", "<TAB>", 'coc#pum#visible() ? coc#pum#confirm() : v:lua.check_back_space() ? "<TAB>" : coc#refresh()', opts)
Coc configuration
Info:
To get intellj sense for the config install the
coc-json
:1:CocInstall coc-json
- Read more about the configuration options here.
Coc can be configured via a json file at ~/.config/nvim/coc-settings.json
:
1{
2 // enable inline diagnostics for all lines in the file
3 "diagnostic.checkCurrentLine": true,
4 "diagnostic.virtualTextCurrentLineOnly": false,
5 "diagnostic.virtualText": true,
6
7 // customise hint signs, i dislike the icons
8 "diagnostic.hintSign": "H",
9 "diagnostic.infoSign": "I",
10 "diagnostic.errorSign": "E",
11 "diagnostic.warningSign": "W",
12
13 // make documentation window rounded and bordered
14 "hover.floatConfig": {
15 "border": true,
16 "rounded": true
17 },
18
19 // make suggestions window rounded and bordered
20 "suggest.virtualText": true,
21 "suggest.enableFloat": true,
22 "suggest.floatConfig": {
23 "border": true,
24 "rounded": true
25 }
26}
This config looks like this: