Comprehensive Guide to Neovim
Overview
Neovim, a powerful editor often hailed as a third-party extension for Vim, boasts an impressive 70k stars on GitHub. This robust tool makes for an excellent default editor choice. This guide will take you through the installation process and equip you with effective typing techniques.
Installation
Neovim Installation On MacOS
1$ brew install neovim
2
3$ which nvim
4/usr/local/bin/nvim
5
6$ vim ~/.zshrc
7alias vim='nvim'
8alias old_vim='vim'
9
10# replace the normal vim with nvim
11$ which vim
12vim: aliased to nvim
13
14$ vim --version
15NVIM v0.7.2
16Build type: Release
17LuaJIT 2.1.0-beta3
18Compiled by brew@Monterey
19
20Features: +acl +iconv +tui
21See ":help feature-compile"
22
23 system vimrc file: "$VIM/sysinit.vim"
24 fall-back for $VIM: "/usr/local/Cellar/neovim/0.7.2_1/share/nvim"
25
26Run :checkhealth for more info
Essential Components
- Installing a Plugin Manager
If you haven't already, it's crucial to set up a plugin manager for seamless plugin installation and management in Neovim. Some popular options include vim-plug, dein.vim, and packer.nvim. In this guide, we'll demonstrate with vim-plug as the example.
Install vim-plug by following the instructions provided on its GitHub repository: https://github.com/junegunn/vim-plug
1sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
2 https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
- Configuring nvim's Initialization File
Locate the configuration file at ~/.config/nvim/init.vim
. This file plays a pivotal role in configuring Neovim and loading plugins effectively.
- Plugin Installation
After saving your init.vim
file, restart Neovim or run :source ~/.config/nvim/init.vim
to apply the changes. Inside Neovim, input the following command to install the plugins:
1:PlugInstall
This command initiates the download and installation of all configured plugins mentioned in your configuration, simultaneously generating output in your vim console.
Crafting Your Personalized Neovim Setup
Let's delve into the intricacies of configuring each line in your initialization file:
1set paste
2set number
3set relativenumber
- The initial three lines comprise simple settings for Neovim. The
set paste
command activates "paste mode," effectively disabling auto-indentation and other automatic formatting adjustments when pasting text. This prevents unintentional formatting changes when pasting code from external sources. set number
introduces line numbering, revealing line numbers on the left side of the buffer.set relativenumber
displays relative line numbers instead of absolute ones. This approach designates the current line as 0, with other lines denoted by positive or negative offsets relative to the current line. This feature streamlines navigation within the buffer.
1set background=dark
- This line establishes the background color of the editor. While this configuration adopts
dark
mode, you can easily switch to a light-themed background by usinglight
.
1set nocompatible
- This line eliminates compatibility with the old-time vi editor. As Neovim represents an enhanced and extended iteration of Vim, disabling compatibility ensures the availability of Neovim-specific features. You'll always need to include this line in your configuration file.
1set showmatch
2set ignorecase
3set mouse=v
4set hlsearch
5set incsearch
set showmatch
activates the highlighting of matching parentheses, brackets, and similar pairs when the cursor hovers over them.set ignorecase
enforces case-insensitivity during search operations, allowing matches for both lowercase and uppercase instances.set mouse=v
enables mouse support, facilitating navigation, split resizing, and middle-click pasting via the mouse.set hlsearch
highlights search matches in real-time while entering the search query, enhancing visibility of match locations.set incsearch
showcases incremental search outcomes as the search query evolves. The feature highlights partial matches while typing.
1set tabstop=4
2set softtabstop=4
3set expandtab
4set shiftwidth=4
5set autoindent
6set number
set tabstop=4
establishes the count of columns occupied by a tab character, fixed at 4 in this case.set softtabstop=4
recognizes a group of spaces as a tab for indentation purposes, enabling Backspace to function as expected.set expandtab
converts tab characters into spaces, ensuring uniform indentation, particularly when collaborating with others who might have varied tab settings.set shiftwidth=4
designates the number of spaces used for auto-indentation after pressing Enter. This setting governs the cursor's rightward movement during auto-indentation.set autoindent
aligns new lines with the indentation of the previous line, maintaining consistency in indentation levels.
1set wildmode=longest,list
2set cc=80
set wildmode=longest,list
simulates bash-like tab completion behavior. If only one match exists, it's automatically completed. With multiple matches, a list prompts you to choose.set cc=80
introduces a color column at the 80th column, serving as a visual guide to maintain code within a defined width, promoting clean coding practices.
1filetype plugin indent on
2syntax on
3set mouse=a
4set clipboard=unnamedplus
5filetype plugin on
filetype plugin indent on
enables filetype detection, thereby activating corresponding plugins and indent settings based on the file type.syntax on
triggers syntax highlighting, offering color-coded visuals for diverse programming languages and file types.set mouse=a
empowers mouse support, encompassing mouse clicks and scrolling functionality.set clipboard=unnamedplus
directs Neovim to employ the system clipboard (if available) for copy and paste tasks, streamlining interaction with the system clipboard.
1set cursorline
2set ttyfast
set cursorline
accentuates the line where the cursor resides, simplifying tracking of the active line.set ttyfast
optimizes scrolling performance within the terminal, leading to smoother scrolling within terminal-based Neovim.
1" set spell " enabling spell check (may require language package download)
2" set noswapfile " preventing swap file creation
3" set backupdir=~/.cache/vim " Directory for storing backup files.
- Lines commencing with
"
entail commented-out options, currently inactive within the configuration(I suggest not active these three configuration lines). set spell
empowers spell checking functionality, highlighted by identifying misspelled words.set noswapfile
inhibits the generation of swap files. While these files aid in recovering unsaved changes after crashes, deactivation implies potential loss of unsaved edits upon unexpected termination.set backupdir=~/.cache/vim
specifies the directory for storing backup files. These duplicates of original files are generated before saving changes. Although the line mentions~/.cache/vim
, it remains commented out.
1call plug#begin('~/.local/share/nvim/site/plugged')
2 Plug 'dracula/vim'
3 Plug 'gruvbox-community/gruvbox'
4 Plug 'ryanoasis/vim-devicons'
5 Plug 'honza/vim-snippets'
6 Plug 'scrooloose/nerdtree'
7 Plug 'preservim/nerdcommenter'
8 Plug 'mhinz/vim-startify'
9call plug#end()
- These lines leverage
vim-plug
to facilitate plugin management. The segment lies betweencall plug#begin()
andcall plug#end()
. - Each
Plug
line designates a plugin for installation and management. This configuration includes Dracula and Gruvbox themes, Devicons for file icons, Vim Snippets, NERDTree, NerdCommenter, Startify, and more.
1autocmd BufReadPost *
2 \ if line("'\"") > 0 && line("'\"") <= line("$") |
3 \ exe "normal! g`\"" |
4 \ endif
- Remember the last cursor position when opening a file
1if has('persistent_undo')
2 " Set a directory to store undo files
3 let g:undo_dir = $HOME . '/.vim/undodir'
4 if !isdirectory(g:undo_dir)
5 call mkdir(g:undo_dir, 'p')
6 endif
7
8 " Store undo history in the specified directory
9 set undodir=$HOME/.vim/undodir
10
11 " Enable persistent undo
12 set undofile
13endif
- Enable undo history across sessions
1colorscheme gruvbox
- Set the theme to Gruvbox, it's also my favorite vim theme.
1" move line or visually selected block - alt+j/k
2inoremap <A-j> <Esc>:m .+1<CR>==gi
3inoremap <A-k> <Esc>:m .-2<CR>==gi
4vnoremap <A-j> :m '>+1<CR>gv=gv
5vnoremap <A-k> :m '<-2<CR>gv=gv
6
7" move split panes to left/bottom/top/right
8 nnoremap <A-h> <C-W>H
9 nnoremap <A-j> <C-W>J
10 nnoremap <A-k> <C-W>K
11 nnoremap <A-l> <C-W>L
12
13" move between panes to left/bottom/top/right
14 nnoremap <C-h> <C-w>h
15 nnoremap <C-j> <C-w>j
16 nnoremap <C-k> <C-w>k
17 nnoremap <C-l> <C-w>l
18
19" Press i to enter insert mode, and ii to exit insert mode.
20:inoremap ii <Esc>
21:inoremap jk <Esc>
22:inoremap kj <Esc>
23:vnoremap jk <Esc>
24:vnoremap kj <Esc>
- Some key-mapping tips for you, can be used according to personal habits.
A Sneak Peek
Now here is our ultimate Neovim configuration, Take a look at it!
Some Valuable Features I Prefer
- Your history endures even after file closure, enabling convenient use of
Ctrl + R
oru
for undoing changes. - Cursor placement is preserved upon reopening files, proving particularly advantageous when working on substantial projects.
- Pasting maintains its original structure, preventing inadvertent indentation issues and code disruption.
- The chosen theme is Dracula, an impressive Vim theme renowned for its aesthetics and functionality.
Now you've got a gorgeous and up-to-coming enhanced Vim, embrace Productive Coding, and wish You a Wonderful Day.