Developer Tools
How-To Guides and Tutorials
Embracing Neovim: Your Daily Code Companion
Bibek Bhattarai
Bibek Bhattarai
May 7, 2024
Embracing Neovim: Your Daily Code Companion

If you believe Vim is an outdated text editor with limited functionality and a high learning curve, you are mistaken, my friend.

Vim is a quick, cross-platform, open-source and adaptable text editor that is exceptionally popular.

Fun fact : I’m writing this blog from Neovim.

Why Vim?

You may be surprised to learn that some developers prefer using a text editor called Vim, despite the fact that it requires typing commands in the terminal. However, there are several good reasons for this preference:

  • Easy to use : Vim is designed for efficient text editing. It allows users to use their hands on the keyboard and avoid switching between typing and using the mouse.
  • Simple commands: Vim’s shortcuts are intuitive, allowing developers to navigate without memorizing complex commands.
  • Make it your own: Vim also allows you to personalize shortcuts as per your requirements, including creating custom shortcuts for different file types.
  • Available everywhere : Vim comes pre-installed on most servers, making it a reliable tool for remote work.

What is Neovim?

Neovim, aka Nvim, is a highly extensible text editor well-suited for terminal-based text editing tasks. It inherits many features from its predecessor, Vim, while also adding enhancements and improving usability.

Installing Neovim

Neovim is available for Windows, MacOS, Linux, and other systems. On Windows OS, it is advisable to use Ubuntu via the Windows Subsystem for Linux (WSL).

Check here for five easy steps in setting up Ubuntu on Windows.

Depending upon your Linux distribution (I use arch btw), you can typically install Neovim using your package manager.

# for Debian-based system 
sudo apt install neovim

# for Arch-based System 
sudo pacman -S neovim

# for macOs using homebrew 
brew install neovim

Basic Text Editing

To open a file with Neovim use the command

nvim <filename>

When navigating the user interface, we consider the following modes:

  • Normal Mode: This is the default mode after launching Neovim.
  • Insert Mode: Use to activate this mode and start typing. Use Esc to go back to normal mode.
  • Visual Mode: Use or left-click anywhere on the screen to activate the visual mode, where users can copy text from the terminal to external applications.
  • Command-line Mode: In Normal Mode, use to enter Command-line mode, where you can enter various commands.

For example – :q!

Vim is my favourite text editor. I’ve been using it for years… I can’t figure out how to exit.

To quit Neovim, use :q

Use :w to save changes made to the file.

To discard changes, use :q! (Forcefully quits without saving). To save and quit, use :wq

Configuration

Even though the default version of neovim ships a good text editor, it lacks some features.

Neovim configuration will be located to config/nvim

Let’s setup the initial file structure with the following commands: Make the nvim config directory.

mkdir -p config/nvim

Create main init.lua file:

touch
init.lua
nvim
init.lua

local set = vim.opt        -- for conciseness

-- line numbers
set.relativenumber = true -- show relative line numbers set.number = true        -- shows absolute line number on cursor line (when relative number is on)

-- tabs & indentation
set.tabstop = 2        -- 2 spaces for tabs (prettier default)
set.shiftwidth = 2        -- 2 spaces for indent width
set.expandtab = true        -- expand tab to spaces set.autoindent = true        -- copy indent from current line when starting new one

-- line wrapping
set.wrap = false        -- disable line wrapping

-- search settings
set.ignorecase = true        -- ignore case when searching set.smartcase = true        -- if you include mixed case in your search, assumes you want case-sensitive

-- cursor line
set.cursorline = true        -- highlight the current cursor line

-- turn on termguicolors for nightfly colorscheme to work set.termguicolors = true
set.background = "dark"        -- colorschemes that can be light or dark will be made dark
set.signcolumn = "yes"        -- show sign column so that text doesn't shift

-- split windows
set.splitright = true        -- split vertical window to the right
set.splitbelow = true        -- split horizontal window to the bottom

When it comes to unlocking the full potential of Neovim with plugins, utilizing a plugin manager is essential. While there are several options available for Neovim, one of the standout choices is lazy.nvim

Inside init.lua paste this:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" if not vim.loop.fs_stat(lazypath) then
vim.fn.system({ "git",
"clone",
"--filter=blob:none", "https://github.com/folke/lazy.nvim.git", "--branch=stable", -- latest stable release lazypath,

})
end
vim.opt.rtp:prepend(lazypath)

Next step is to add lazy.nvim below the code added in the prior step in init.lua :

opts = {} plugins = {
-- plugins link
}
require("lazy").setup(plugins, opts)

As an example, let’s say you want to add the OneDark color scheme to your Neovim setup. You can achieve this by adding the navarasu/onedark.nvim plugin to your configuration.

Here’s how you can do it:
...
plugins = {
"navarasu/onedark.nvim",
}
require("lazy").setup(plugins.opts)

Now that you’ve installed the plugin, you have the freedom to configure it according to your preferences:

require('onedark').setup { style = 'darker' }
require('onedark').load()

Conclusion

In summary, Neovim is a versatile and powerful text editor that caters to the needs of modern developers. Its user-friendly interface, coupled with a rich ecosystem of plugins, ensures a smooth and efficient coding experience.

Whether you’re a beginner or an experienced coder, Neovim provides the tools and customization options necessary to elevate your workflow.

Resources and Credits

Special thanks are extended to the repositories, plugin authors, and the Neovim community for their invaluable contributions, which have made this article achievable.

You can check out my dotfiles on GitHub for a reference on how I’ve customized Neovim to serve as my primary development tool for coding.

linux
neovim
software
technology
vim
C
O
D
S
E
Google App Scripts
Database design
Web development
Mobile application development
Performance optimization
Automation
API Integration
Chatbot
No-code solutions
Web scraping
Google App Scripts
Database design
Web development
Mobile application development
Performance optimization
Automation
API Integration
Chatbot
No-code solutions
Web scraping
Google App Scripts
Database design
Web development
Mobile application development
Performance optimization
Automation
API Integration
Chatbot
No-code solutions
Web scraping