Re-learning Vim (7)

Date: 2023/03/05 (initial publish), 2024/03/04 (last update)

Source: en/note-00040.md

Previous Post Top Next Post

TOC

Nvim configuration retrospective

I have been updating Nvim configuration with AstroNvim as described in Re-learning Vim (5) and Re-learning Vim (6).

I also build the latest Nvim in Building Neovim to be compatible with the latest packages.

Although AstroNvim provided me with great features with simple and consistent customization mechanism, they were still a bit overwhelming and sometimes too much. I needed a simpler baseline Neovim configuration platform which offers most features as opt-in.

As of 2023-12-24, I have migrated to NeoVim (v0.9.5-dev-41+g25bedc925, local deb) with lua based lazyVim (v10.15.1) as my primary editor invoked by nvim (or via its nv alias) with my customized LazyVim starter. See below for how I configured this

This LazyVim is nice but a bit heavy.

I decided to use vi to start Vim or Nvim with a single file configuration with very minimal customization.

Vi as an baseline simple editor

Here, I created a single generic configuration file for Vim and NeoVim ~/.vimrc.

""" Generic baseline Vim and Neovim configuration (~/.vimrc)
"""   - For NeoVim, use "nvim -u ~/.vimrc [filename]"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible                " :h 'cp -- sensible (n)vim mode
syntax on                       " :h :syn-on
filetype plugin indent on       " :h :filetype-overview
set encoding=utf-8              " :h 'enc (default: latin1) -- sensible encoding
""" current vim option value can be verified by :set encoding?
set backspace=indent,eol,start  " :h 'bs (default: nobs) -- sensible BS
set statusline=%<%f%m%r%h%w%=%y[U+%04B]%2l/%2L=%P,%2c%V
set listchars=eol:¶,tab:⇄\ ,extends:↦,precedes:↤,nbsp:␣
set viminfo=!,'100,<5000,s100,h " :h 'vi -- bigger copy buffer etc.
""" Pick "colorscheme" from blue darkblue default delek desert elflord evening
""" habamax industry koehler lunaperche morning murphy pablo peachpuff quiet ron
""" shine slate torte zellner
colorscheme industry
"colorscheme default
set scrolloff=5                 " :h 'scr -- show 5 lines around cursor
set laststatus=2                " :h 'ls (default 1)  k
""" boolian options can be unset by prefixing "no"
"set list                        " :h 'list (default nolist)
set smartcase                   " :h 'scs -- Override the 'ignorecase' option
set autoindent                  " :h 'ai
set smartindent                 " :h 'si
set nowrap                      " :h 'wrap
set nolist                      " :h 'list -- (enable when needed)
set noerrorbells                " :h 'eb
set novisualbell                " :h 'vb
set t_vb=                       " :h 't_vb -- termcap visual bell
set spell                       " :h 'spell
set spelllang=en_us,cjk         " :h 'spl -- english spell, ignore CJK
set clipboard=unnamedplus       " :h 'clipboard -- cut/copy/paste with other app
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Popular mappings
""" Window moves without using CTRL-W which is dangerous in INSERT mode
nnoremap <C-H> <C-W>h
nnoremap <C-J> <C-W>j
nnoremap <C-K> <C-W>k
silent! nnoremap <C-L> <C-W>l
""" nohlsearch with <ESC> (<C-L> is mapped as above)
inoremap <ESC> <CMD>noh<CR><ESC>
nnoremap <ESC> <CMD>noh<CR><ESC>
""" execute macro recorded with <ESC>qq.....<ESC>q with Q
nnoremap Q @q
""" center after jump next
nnoremap n nzz
nnoremap N Nzz
""" fast "jk" to get out of INSERT mode (<ESC>)
inoremap  jk        <ESC>
""" double <ESC> to get out of TERM mode (CTRL-\ CTRL-N)
tnoremap <ESC><ESC> <C-\><C-N>
""" fast "jkjk" to get out of TERM mode (CTRL-\ CTRL-N)
tnoremap jkjk <C-\><C-N>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" nvim default mappings (no need for nvim)
""" copy to EOL (no delete) like D for d
noremap Y y$
""" disable prefix repeats for erases
inoremap <C-U> <C-G>u<C-U>
inoremap <C-W> <C-G>u<C-W>
""" search visual selected string for visual mode
xnoremap * y/\V<C-R>"<CR>
xnoremap # y?\V<C-R>"<CR>
""" repeat last substitute and *KEEP* flags
nnoremap & :&&<CR>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Force to use underline for spell check results
augroup SpellUnderline
  autocmd!
  autocmd ColorScheme * highlight SpellBad term=Underline gui=Undercurl
  autocmd ColorScheme * highlight SpellCap term=Underline gui=Undercurl
  autocmd ColorScheme * highlight SpellLocal term=Underline gui=Undercurl
  autocmd ColorScheme * highlight SpellRare term=Underline gui=Undercurl
augroup END
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" highlight tailing spaces as red color
highlight RedundantSpaces ctermbg=red guibg=red
call matchadd('RedundantSpaces', '\s\+$')
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Use faster 'rg' (ripgrep package) for :grep
if executable("rg")
  " port https://www.vi-improved.org/recommendations/ to rg
  set grepprg=rg\ --no-heading\ --color=never\ --ignore-case\ --column
  set grepformat=%f:%l:%c:%m,%f:%l:%m
endif
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Retain last cursor position :h '"
augroup RetainLastCursorPosition
  autocmd!
  autocmd BufReadPost *
    \ if line("'\"") > 0 && line ("'\"") <= line("$") |
    \   exe "normal! g'\"" |
    \ endif
augroup END
" vim: set sw=2 sts=2 et ft=vim :

Since nvim may not available, I created bash alias for vi in ~/.bashrc as:

# GOOD OLDE VI as baseline editor (minimum resource file)
if [ -r "~/.vimrc" ]; then
  VIMRC="~/.vimrc"
else
  VIMRC="NONE"
fi
if type nvim >/dev/null ; then
  alias nv='nvim'
  alias vi='nvim -u $VIMRC'
  alias v='nvim -u NORC'
  alias sv="SUDO_EDITOR='/usr/bin/nvim -u NORC' /usr/bin/sudoedit"
elif type vim >/dev/null ; then
  alias vi='vim -N -u $VIMRC'
  alias v='vim -N -u NORC'
  alias sv="SUDO_EDITOR='/usr/bin/vim -N -u NORC' /usr/bin/sudoedit"
else
  unalias vi
  unset VIMRC
fi
if [ -n "$VIMRC" ]; then
  export EDITOR='vi'
  export VISUAL='vi'
  alias vimdiff='vi -d'
  alias view='vi -R'
  alias ex='vi -e'
fi

Now that $NVIM_APPNAME environment variable is supported, I should think about using this, soon.

LazyVim

A new baseline Neovim setup system LazyVim is published by the upstream of the new smart delayed package loader 💤lazy.nvim.

Its guide is available at www.lazyvim.org.

LazyVim functions as a nice baseline Neovim configuration system similar to AstroNvim but with less complication. -> So I decided to switch over to this.

Terminal emulator

The current gnome-terminal seems to support true color and undercurls without much issues.

Just to be sure, I also installed kitty and alacritty which are officially supported by www.lazyvim.org.

The colorscheme for them was adjusted to match gnome-terminal to ensure readability for programs such as mc.

Key bindings

NOTE: The comment-out command is gc for LazyVim (it is <SPACE>/ for AstroNvim).

Colorscheme of Lazyvim

The default colorscheme for Lazyvim is tokyonight.

Now Neovim default to vim.go.background = "dark". (as of 0.9.0+)

mason.nvim support

mason.nvim manages binary commands used by null-ls.nvim. It can be started by :Mason (alternatively, <SPACE>cm). To update binary commands to the latest, just press “U”.

Basically, this helps us to install requested <packagename> package into ~/.local/share/nvim/mason/packages/<packagename>/ and make its executable <progname> program accessible from Neovim in ~/.local/share/nvim/mason/bin/<progname>. So executable programs from Neovim plugins are not the one on the main system.

As I see my situation with :checkhelth mason.:

mason: require("mason.health").check()
========================================================================
## mason.nvim report
  - OK: neovim version >= 0.7.0
  - OK: **Go**: `go version go1.19.6 linux/amd64`
  - OK: **cargo**: `cargo 1.65.0`
  - OK: **luarocks**: `/usr/bin/luarocks 3.8.0`
  - OK: **Ruby**: `ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux-gnu]`
  - OK: **RubyGem**: `3.3.15`
  - WARNING: **Composer**: not available
  - WARNING: **PHP**: not available
  - OK: **npm**: `9.2.0`
  - OK: **node**: `v18.13.0`
  - OK: **python3**: `Python 3.11.2`
  - OK: **pip3**: `pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)`
  - WARNING: **javac**: not available
  - OK: **java**: `openjdk version "17.0.6" 2023-01-17`
  - WARNING: **julia**: not available
  - OK: **wget**: `GNU Wget 1.21.3 built on linux-gnu.`
  - OK: **curl**: `curl 7.88.1 (x86_64-pc-linux-gnu) libcurl/7.88.1 OpenSSL/3.0.8 zlib/1.2.13 brotli/1.0.9 zstd/1.5.4 libidn2/2.3.3 libpsl/0.21.2 (+libidn2/2.3.3) libssh2/1.10.0 nghttp2/1.52.0 librtmp/2.3`
  - OK: **gzip**: `gzip 1.12`
  - OK: **tar**: `tar (GNU tar) 1.34`
  - WARNING: **pwsh**: not available
  - OK: **bash**: `GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu)`
  - OK: **sh**: `Ok`
  - OK: GitHub API rate limit. Used: 0. Remaining: 60. Limit: 60. Reset: Tue 07 Mar 2023 01:45:13 AM JST.

I ended up to install following Debian packages as required system programs.

See more :h mason.nvim

In order to make mason.nvim generated programs accessible from shell prompt, I have modifies ~/.profile to include the following:

# set PATH so it includes mason.nvim's private bin if it exists (Neovim/mason.nvim)
if [ -d "$HOME/.local/share/nvim/mason/bin/" ]; then
    PATH="$HOME/.local/share/nvim/mason/bin:$PATH"
fi

I also added ~/.editorconfig to cope with sources using editorconfig.

LazyVim customization

I created folked LazyVim starter with my changes in osamu branch to keep backup of my local configuration. I intend to git push -f. It now contains following changes.

Update .gitigonore

I excluded lazy-lock.json from git repo.

Opening Gzip files

Since Debian ships most documentation files as Gzipped, I enabled “gzip” plugin for view purpose.

Extra features as opt-in features

I made following extra features disabled as started. These can be enabled via key bindings.

I also made <leader>uu as NOP to avoid accidental undo u.

Column and Line number and, Unicode codepoint in statusline

Using ~/.config/nvim/lua/plugin/ui.lua, I replaced clock display in statusline with Column and Line number and, Unicode codepoint as 1: 1 [+U20] (Unreadable chars are OK if hack font is used).

Vimdoc help files

I add access to many hidden help files available under LOCAL ADDITIONS: by running vimdoc-lazy.

Extra plugins

A few extra plugin supports are introduced by adding configuration files in ~/.config/nvim/lua/plugin/ of my folked LazyVim starter in osamu branch.

Treesitter and LSP

It is a bit confusing what is all about Treesitter and LSP. Reddit gave me good intro: Treesitter vs LSP. Differences ans overlap.

The nvim-treesitter playground plugin is now integrated into Neovim v0.9, so :InspectTree works without installing this plugin.

Previous Post Top Next Post