DEV Community

Cem AKAN
Cem AKAN

Posted on

NeoVim From Scratch

Hi, today i will talk about my favorite text editor NeoVim. I will try to cover the installation, modes, usage, shortcuts, configuration and plugins. Alright, hold tight because we’re starting.

1. What is NeoVim?

captionless image

NeoVim is a modern fork of the terminal-based text editor Vim, retaining Vim’s speed and flexibility while introducing modern enhancements for better performance and extensibility. Primarily used for coding and text manipulation, NeoVim inherits many features from Vim and is interacted with primarily through keyboard commands and shortcuts, minimizing reliance on mouse input.

2. Installation:

NeoVim can be installed in multiple ways on Linux. (It also supports Windows and MacOS, but in this guide, I will only cover Linux.)

The easiest installation option for beginners is the default package manager.

For installation, open your terminal and execute the following command to install NeoVim from your default package manager:

$ sudo apt install neovim     #For Debian, Ubuntu, Linux Mint, etc.
$ sudo dnf install neovim     #For RHEL, Fedora, Alma Linux, etc.
$ sudo pacman -S neovim       #For Arch, Manjaro, EndeavourOS, etc.
Enter fullscreen mode Exit fullscreen mode

After the installation, you can execute the following command to checking the current version of NeoVim.

$ nvim -v
Enter fullscreen mode Exit fullscreen mode

You should see a output like this:

Screenshot from Warp terminal

Congratulations, you have successfully installed NeoVim 🎉

3. Modes and Keyboard Shortcuts:

captionless image

A- Modes:

NeoVim has 13 modes, In this guide, we will only cover the basic 4 modes: Normal, Insert, Visual, Command-line.

a- Normal Mode:

It is NeoVim’s default mode, used for navigation. In normal mode, you can move the cursor, delete, copy, paste, and perform various editing operations using keyboard shortcuts. This mode is ideal for quick navigation and basic file management tasks.For switching your mode to normal mode, you can use the**<ESC>**key.

b- Insert Mode:

Insert mode is NeoVim’s editing mode used for writing and deleting. However, unlike normal mode, you can’t use the cursor because if you try to use it, your mode will switch to visual mode. For switching your mode to insert mode, you can use the **i**,**a** or**<insert>**key.

c- Visual Mode:

Visual mode allows users to select blocks of text for manipulation. It is particularly useful for tasks like copying, cutting, or applying operations to selected text. For entering to visual mode, you can use the ‘ v ’ key in normal mode.

d- Command-Line Mode:

NeoVim also has a command-line mode. This mode used to write NeoVim’s base shortcuts like: save, quit, find, replace etc. Also it used for access embedded terminal and mange your plugins’ settings. For switching your mode to command-line mode, you can use the **:** key. But ‘ : ’ key can not work on insert mode.

B- Keyboard Shortcuts:

captionless image

a- Navigation: (For Normal Mode)

  • h, j, k, l: Move left, down, up, right respectively.
  • w, b: Move forward by word, backward by word.
  • gg: Move to the beginning of the file.
  • G: Move to the end of the file.

b- Editing: (For Normal Mode)

  • o, O = Open a new line below, above the current line and enter insert mode.
  • x =Delete character under the cursor.
  • dd = Delete the current line.
  • yy = Copy the current line.
  • p = Paste after the cursor.

c. Saving and Quitting: (For Command-Line Mode)

captionless image

  • :w = Save the current file.
  • :q = Quit NeoVim.
  • :wq = Save and quit NeoVim.
  • :q! = Quit without saving (force quit).

d- Search and Replace: (For Command-Line Mode)

  • /pattern = Search forward for ‘pattern’.
  • :%s/pattern/replacement = Replace ‘pattern’ with ‘replacement’ on the current line.
  • :%s/pattern/replacement/g = Replace ‘pattern’ with ‘replacement’ globally in the file.

e- Tabs and Windows: (For Normal Mode)

  • Ctrl + w + v = Split the window vertically.
  • Ctrl + w + s = Split the window horizontally.
  • Ctrl + w + Arrow keys = Navigate between split windows.

4.Configuration:

captionless image

Nearly all software has a configuration file, or simply a config file. We can make changes to the software by editing its config file. In this part, we will learn how to edit our NeoVim config file.

NeoVim has three different layers:

  1. Vim API: This is inherited from Vim and uses Vimscript.
  2. Nvim API: Written in C, this API is primarily used for remote plugins and GUIs.
  3. Lua API: Written in Lua, this API allows for powerful and flexible configuration using the Lua scripting language.

Therefore, we can divide NeoVim’s configuration system into two main categories: one for Vimscript and one for Lua scripts.

For basic configurations, you can choose either Lua or Vimscript. However, you should only use one of them to avoid configuration conflicts.

A- Classic Vimscript File Configuration:

To open NeoVim’s config file, open your terminal and execute the following command:

nvim ~/.config/nvim/init.vim
Enter fullscreen mode Exit fullscreen mode

Then, select following settings which you want and add them to your config file.

  • Tab Settings:
set tabstop=4           " Number of spaces that a <Tab> in the file counts for
set shiftwidth=4        " Number of spaces to use for each step of (auto)indent
set expandtab           " Use spaces instead of tabs
Enter fullscreen mode Exit fullscreen mode
  • UI Settings:
syntax on               " Enable syntax highlighting
set cursorline          " Highlight to current cursorline
set number              " Show line numbers
set showmatch           " Highlights matching parentheses, brackets, or braces when the cursor is on one of them.
Enter fullscreen mode Exit fullscreen mode
  • Searching Settings:
set ignorecase         " Makes search operations case insensitive.
set hlsearch           " Highlights all occurrences of the search pattern.
set incsearch          " Updates search results incrementally as you type.
Enter fullscreen mode Exit fullscreen mode
  • Other Important Settings:
set mouse=v                " Enable pasting the copied text with mouse' middle-click.
set clipboard=unnamedplus  " For using the system's clipboard.
Enter fullscreen mode Exit fullscreen mode

B- Basic Lua Configuration:

captionless image

( Writer Note: In this guide, I will cover the basic Lua configurations to make it more beginner friendly. In the coming days, I will share an advanced and detailed Lua configuration guide. )

To open NeoVim’s second config file, open your terminal and execute the following command:

nvim .config/nvim/init.lua     
Enter fullscreen mode Exit fullscreen mode

If the file doesn’t exist, this command will create it.

Then, select following settings which you want and add them to your config file.

  • Tab Settings:
vim.opt.tabstop = 4           -- Number of spaces that a <Tab> in the file counts for
vim.opt.shiftwidth = 4        -- Number of spaces to use for each step of (auto)indent
vim.opt.expandtab = true      -- Use spaces instead of tabs
Enter fullscreen mode Exit fullscreen mode
  • UI Settings:
vim.cmd('syntax on')          -- Enable syntax highlighting
vim.opt.cursorline = true     -- Highlight the current cursorline
vim.opt.number = true         -- Show line numbers
vim.opt.showmatch = true      -- Highlight matching parentheses, brackets, or braces when the cursor is on one of them
Enter fullscreen mode Exit fullscreen mode
  • Searching Settings:
vim.opt.ignorecase = true     -- Make search operations case insensitive
vim.opt.hlsearch = true       -- Highlight all occurrences of the search pattern
vim.opt.incsearch = true      -- Update search results incrementally as you type
Enter fullscreen mode Exit fullscreen mode
  • Other Important Settings:
vim.opt.mouse = 'v'                -- Enable pasting the copied text with mouse' middle-click
vim.opt.clipboard = 'unnamedplus'  -- Use the system's clipboard
Enter fullscreen mode Exit fullscreen mode

Saving and Exiting

After editing your configuration file, don’t forget to save your changes. Follow these steps:

  1. Press **<Esc>** to switch from Insert mode to Normal mode.
  2. Type **:**’ to enter command-line mode.
  3. Type **w** and press **<Enter>** to save your edits.

5. Plugins:

Using a plugin manager !?

captionless image

Thanks to plugin managers, we can easily manage our plugins. We can install, update or delete our plugins with single command. There are multiple plugin managers available, but I will talk about vim-plug.

a- Installation:

For installation, open your terminal and execute the following command.

curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vimsu
Enter fullscreen mode Exit fullscreen mode

b- Usage:

After the installation we should add the vim-plug section in to the NeoVim configuration file.

For opening NeoVim configuration file, you should open your terminal and execute the following command.

nvim ~/.config/nvim/init.vim
Enter fullscreen mode Exit fullscreen mode

Press ‘ i ’ or ‘ a ’ key to entering insert mode to edit configuration file.

Vim-plug section has some rules:

  • Begin the section should start with =**call plug#begin()**
  • You can list the plugins with **Plug** commands between call plug sections.
  • End the section should end with = **call plug#end()**

True vim-plug section example:

call plug#begin()
" List your plugins here
Plug 'tpope/vim-sensible'
call plug#end()
Enter fullscreen mode Exit fullscreen mode

After editing the configuration file, don’t forget to save your changes. For saving you can follow these steps:

  1. Press the **<Esc>** key to switch from insert mode to normal mode.
  2. After entering normal mode, press **:** to enter command-line mode.
  3. Type **w** and then press **<Enter>** to save your edits.

c- Vim-plug Commands:

Vim-plug has also some commands for management the plugins easily. We use these commands on command-line mode.

  • **:PlugInstall** =to install the plugins
  • **:PlugUpdate** =to install or update the plugins
  • **:PlugDiff** =to review the changes from the last update
  • **:PlugClean**= to remove plugins no longer in the list

d- Plugins:

I will introduce the most popular plugin, NERDTree. I am only showing one plugin because the installation process is almost the same for all plugins. If you understand the installation of NERDTree, you will be able to download and install other plugins for your NeoVim.

NERDTree:

This plugin is a file system explorer for our editor. With it, you can visually browse directories and open files.

  • Installation:

Firstly we should open the NeoVim config. For opening it, you should open your terminal and execute the following command.

nvim ~/.config/nvim/init.vim
Enter fullscreen mode Exit fullscreen mode

After the opening configuration file, we add following text between the vim-plug sections. Save your changes and restart NeoVim.

Plug 'preservim/nerdtree'
Enter fullscreen mode Exit fullscreen mode

In the end, you enter the command-line mode and write :PlugInstall command to install the new plugin.

  • Usage:

My NeoVim setup

After the installation, NERDTree appear the left side of NeoVim. And now you can easily select your files.

  • Shortcut:

You can change NERDTree’s key mapping from NeoVim’s config file. By default, the shortcut for opening and closing NERDTree is the ‘ CTRL + k ’ combination.

Change the default key mapping to something that suits your preference. For instance, you might prefer using ‘ CTRL + n ’ for opening and closing NERDTree.

nnoremap <C-n> :NERDTreeToggle<CR>   " Opening and closing NERDTree with Ctrl+n
Enter fullscreen mode Exit fullscreen mode

captionless image

Congratulations on learning the basics of NeoVim 🎉 You’ve unlocked a powerful tool that will supercharge your text editing and coding workflow. Remember, “To use NeoVim or not to use NeoVim” is no longer a question you’re already on the right path. Keep exploring, keep coding, and enjoy the journey. See you in the next articles 👋

captionless image

Top comments (0)