Neovim configuration for beginners — Part two

Rio Nyx
Geek Culture
Published in
4 min readJul 9, 2021

--

Plugins are the ones that make the already great editor even greater. And there are lots of plugins available for vim, according to your need, which helps in increasing your productivity.

This is going to be a continuation of my previous article. If you haven’t checked it out, just go through there, it will be worth your time.

We will be configuring some plugins discussed in the earlier article, for easy usage.

Absolutely everyone writes comments in their code, and NerdCommenter is a popular commentation plugin. The configuration that I use for NerdCommenter is

“ Create default mappings
let g:NERDCreateDefaultMappings = 1
“ Add spaces after comment delimiters by default
let g:NERDSpaceDelims = 1
“ Use compact syntax for prettified multi-line comments
let g:NERDCompactSexyComs = 1
“ Align line-wise comment delimiters flush left instead of following code indentation
let g:NERDDefaultAlign = ‘left’
“ Set a language to use its alternate delimiters by default
let g:NERDAltDelims_java = 1
“ Add your own custom formats or override the defaults
let g:NERDCustomDelimiters = { ‘c’: { ‘left’: ‘/**’,’right’: ‘*/’ }}
“ Allow commenting and inverting empty lines (useful when commenting a region)
let g:NERDCommentEmptyLines = 1
“ Enable trimming of trailing whitespace when uncommenting
let g:NERDTrimTrailingWhitespace = 1
“ Enable NERDCommenterToggle to check all selected lines is commented or not
let g:NERDToggleCheckAllLines = 1
“ for motions
nnoremap <silent> <leader>c} V}:call NERDComment(‘x’, ‘toggle’)<CR>
nnoremap <silent> <leader>c{ V{:call NERDComment(‘x’, ‘toggle’)<CR>

The most useful default keybinding of NerdCommenter is <leader><c-space> which toggles comments according to the first selected line.

Another great plugin is vim-startify, which is a customizable start screen for vim. Even though it has MRU list(Most Recently Used), add custom bookmarks is sometimes useful. You can easily add them as

let g:startify_bookmarks = ['~/.bashrc','~/Documents/']

And any file/directory in startify screen can easily be opened by typing the corresponding number.

Now let's define the directory for our snippets(UltSnips being the snippet engine),

let g:UltiSnipsSnippetDirectories=[$HOME.’/.config/nvim/UltiSnips’]

Snippets for specific languages can be placed here, for example for cpp(C++)

save the file as cpp.snippets and content of the file should be of the format

snippet inc "snippet name"
#include <iostream>
$1
endsnippet

Now let’s move on to the completion engine, Coc. Coc itself doesn't have any use, we should extensions for it.

let g:coc_global_extensions = [
\ 'coc-pairs',
\ 'coc-snippets',
\ 'coc-html',
\ 'coc-tsserver',
\ 'coc-css',
\ 'coc-clangd',
\ 'coc-json',
\ 'coc-pyright',
\ 'coc-sh',
\ 'coc-flutter',
\ ]

Then you should run the command :CocInstall to install these extensions.

To have Tab completion similar to that of VSCode,

set signcolumn=yes
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>'

There can be some unwanted trailing spaces in our code. Why should we bother to remove it manually, while vim can take care of it. In order to remove trailing spaces, create a function,

function TrimWhitespace()
let l = line(“.”)
let c = col(“.”)
%s/\s\+$//e
call cursor(l, c)
endfun

You can either run this function an auto command or just bind to a key, say F5. Just use one of the ways to do so.

autocmd BufWritePre * :call TrimWhitespace()
nnoremap <F5> :call TrimWhitespace()<CR>

Now there’s is one more thing, some vim users like to do, ie, remapping the escape key, esp to Caps Lock. But there’s no completely straightforward way to do so in vim itself. There are workarounds but are global changes.

In windows, you can use AutoHotkey, in mac you can change it in iTerm, But in Linux, it is a bit harder. If you are using Xorg as display server there is a way to do that using setxkbmap.

setxkbmap -option caps:swapescape

This switches between Escape and CapsLock keys. To clear all made changes

setxkbmap -option 

So creating a bash function like

nv(){
setxkbmap -option caps:swapescape && nvim $* && setxkbmap -option
}

would do the trick. But remember still this change will be active as far as vim is open. But personally, I don’t like remapping it to any other key. It's a personal preference.

By now, you would have a fair enough setup, to start using vim. But it’s up to one how they want to use it like. Everyone has their own preferences. Pretty much everything can be done in one way or another in vim, so configure it the way you like it.

--

--