Introduction
If you are reading this tutorial, you probably need a good programming software for your server. There are many of them, but this particular one stands out — NeoVim. More specifically: NeoVim with kickstart.nvim config. Unlike the classic "nano", NeoVim has many possibilities. First of all: syntax highlighting, LSP support, fast code writing based on shortcuts, fuzzy find and much more! IMO: best code editor (but quite challenging in the beginning). In the next parts of this tutorial you will find information about installation, using LSP for everyday programming or manage plugins.
Step 1 - Install NeoVim
To install NeoVim, we should type these commands in the terminal, depending on your operating system.
Install NeoVim:
-
Debian / Ubuntu
sudo add-apt-repository ppa:neovim-ppa/unstable -y sudo apt update sudo apt install make gcc ripgrep unzip git neovim
-
Fedora
dnf copr enable agriffis/neovim-nightly dnf install -y neovim python3-neovim ripgrep unzip git
-
CentOS 8 / RHEL 8
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm yum-config-manager --add-repo=https://copr.fedorainfracloud.org/coprs/carlwgeorge/ripgrep/repo/epel-7/carlwgeorge-ripgrep-epel-7.repo yum install -y neovim python3-neovim ripgrep unzip git
Install KickStart.nvim config:
The files are saved in
~/.config/nvim
.
git clone https://github.com/nvim-lua/kickstart.nvim.git "${XDG_CONFIG_HOME:-$HOME/.config}"/nvim
Now, after downloading/installation, we can run NeoVim with the nvim
command! Run nvim --version
to check if nvim was installed successfully. We should wait a while until all updates and operations are performed. After that, our editor is ready to use. To start editing any file just type nvim filename.sh
. To exit NeoVim, write :q
in main window (we will discuss commands and shortcuts later).
Step 2 - NeoVim as sudo editor
To set NeoVim as the main system editor of the sudoedit command, edit the bashrc file:
sudo nano ~/.bashrc
Then at the end of the file add:
export EDITOR="nvim"
Save the file by pressing CTRL
+x
➔ y
➔ "return" key. For our edit to work, we still need to run the command:
source ~/.bashrc
From now on, we can edit system files in NeoVim with the sudoedit command.
Step 3 - Modes of editor
In NeoVim, we have 4 file-editing modes. They can be visualized as a "birthday cake".
- NORMAL - This are candles and the name of the birthday boy. In this mode, we can quickly navigate the file, easily copy/delete/paste text.
- INSERT - This is the main biscuit, the core of our cake. Moving to this mode opens up the possibility for us to edit the code.
- VISUAL - This is a sharp knife for cutting our cake. In this mode, we can select the text word by word, line by line, block by block, and perform various operations on it.
- COMMAND - This is our fancy moving stand with a ruler on which the cake is placed. Here we enter commands directly into NeoVim, e.g. searching or replacing text, saving a file.
Keys | Action |
---|---|
nvim filename.sh | Open file in Normal mode |
esc ➔ i | Switch to Insert mode |
esc ➔ v | Switch to Visual mode |
esc ➔ : | Switch to Command mode |
esc | Switch back to Normal mode |
Normal mode
Normal mode is our default mode. We will use it for browsing, copying, deleting, and other operations. We will spend most of our time in this mode. From "Normal", we can switch to any other mode - but after finishing our task - we always return to it.
In the beginning - train yourself to come back to Normal mode automatically after any action done (this is usually done with the ESC
key). This is a really good practice that will speed up your code work.
-
How to "walk" in this mode?
Moving the cursor is done using certain shortcuts
{motions}
. The most basic of these are listed below.Keys What does it do? k or UP arrow Cursor goes up j or DOWN arrow Cursor goes down l or RIGHT arrow Cursor goes right h or LEFT arrow Cursor goes left CTRL+u Scroll half page up CTRL+d Scroll half page down w Cursor moves to beginning of next word b Cursor moves to the previous word e Cursor moves to the end of next word } Cursor moves one block of code down { Cursor moves one block of code up zz Cursor line goes to middle screen (center screen) One more important thing! If we type a number (for example 9 or 24) before moving
{motion}
the cursor, that's how many times the action will be performed. For example: 3w will move the cursor forward by three words.
-
Simple operations on text
As in any code editor, we can copy and paste text. We can also easily delete a line. Below are the basic key bindings for these tasks.
Keys What does it do? yy Copy (yank) current line Y Copy (yank) the characters from the cursor to the end of the current line dd Delete current line D Delete the characters from the cursor to the end of the current line p / P Paste yanked text after / before cursor In addition, we can use
y
➔{motion}
to copy a specific fragment. For example,y
➔e
will copy the fragment from the cursor to the end of the word.
Insert mode
In Insert Mode we can write code and… mainly this mode is only just for that. Unfortunately, most of NeoVim's shortcuts won't work in this mode - so it's always a good idea to go back to Normal Mode for shortcuts.
Such separation of these modes makes sense because programmers mainly navigate files. If walking through files is efficient, it saves a lot of time.
When we switch to Insert mode, our cursor turns from a thick rectangle into a classic line for entering text. In addition, the inscription in the lower left corner will also change to "Insert."
-
How to start typing in NeoVim?
Normal Mode has a set of key bindings to do an action and enter to Insert Mode. After that we can type our code.
This means, when you run the keys below in Normal mode, NeoVim will automatically switch from Normal mode to Insert Mode and execute the action mentioned in "What does it do?".
Remember that you can always use
esc
to switch back to Normal mode.Keys What does it do? o / O Create a new and empty line below / above the cursor i / a Start typing before / after cursor I / A Start typing at beginning / end of current line s ➔ any character Change the current character to any character we press and start typing S Clear current line and start typing C Remove the characters from the cursor to the end of the current line and start typing When we are in Insert mode, we can switch into a special mode that allows us to run one single Normal mode shortcut. This way we won't have to leave Insert mode to paste text, for example.
To do this, press the combination
CTRL
+o
. Now we can run one of the Normal mode shortcuts, e.g. text pastingp
, screen centeringzz
, line copyingyy
, etc. After the action, we are automatically back in Insert mode.
Visual Mode
Visual mode could also be called "text selection mode". With it, we can select any text using the previously learned {motions}
(the ones mentioned in Normal mode). In Normal mode:
- Press
V
to switch to Visual mode with text selection by lines and use UP arrow or DOWN arrow to select more lines. - Press
v
to switch to Visual mode with text selection by single letters and use the Normal mode{motions}
to select more characters/words.
So, if we want to select a word, we press v
to enter Visual Mode. Now we press e
to select first word. To select next word we press e
again. Of course we can mix motion keys. We can press e
➔ w
➔ 5w
➔ LEFT arrow ➔ }
.
After text selection we can do certain actions:
Keys | What does it do? |
---|---|
y | Copy (yank) selected text |
d | Delete selected text |
c | Delete selected text and enter Insert mode |
Command Mode
The last mode is Command. Here we will send commands to NeoVim to save and open a file, search or replace text. From this mode we can also look at e.g. plugins, environment variables, etc.
-
Open / Close / Save
This is a set of the most basic commands for manipulating files.
Command What does it do? :q Close current file (works only if your file is saved) :q! Close current file and discard changes :qa Close every file and exit NeoVim :qa! Close every file without saving and exit NeoVim :w Save current file :wq Save and close current file :wqa Save all files and close NeoVim :e filename Open a file with name filename :saveas filename Save contents as filename and continue editing
-
Buffers
We will look at one scenario: we opened several files at once. We could have called the command
nvim filename1 filename2
or:e filename
several times. How does it work, where are these files? They are loaded into separate buffers. In order to view them, we need to use these commands:Command What does it do? :ls List of open buffers (with numbers) :b{NUMBER} Switch to buffer number X (e.g. :b4 - buffer no. 4) :bn Switch to the next buffer (next file) :bN or :bp Switch to the previous buffer (previous file) :bd Discard current buffer (only if saved) :bd! Discard current buffer without saving
-
Tabs work differently!
We can also use the classic tabs in NeoVim. This way, we will see the names of open files in the bar at the top. As before - there are corresponding commands for this.
Command What does it do? :tabnew or :tabnew filename Create new tab or load file to new tab :tabc Close current tab :tabn or gt Switch to the next tab :tabN or gt Switch to the previous tab :tablast Switch to the last tab :tabfirst Switch to the first tab
-
Search & Replace
Searching is done a little differently as typing all the commands, but it seems right to put it in this section. These are the only actions that will not start with a
:
. To begin searching in Normal mode, we type our{text}
after/
or?
and hit the "Return" key.Command What does it do? /{text} Search {text} forwards ?{text} Search {text} backwards n / N Move to the next / previous result In addition, when we are in Normal mode we can search for the text that we have under the cursor. When we press
#
, we will start looking for the selected text forward. When we press*
, we will start looking for the selected text backwards.To search and replace text we use a single command. This is how it goes:
:%s/pattern/replacment/g
After hitting the "Return" key, we will have the option to agree to replace the text from "pattern" to "replacement". With the
y
key we will agree to the highlighted replacement. With then
key we will refuse the highlighted replacement.
Step 4 - LSP
Time to discuss the most important (in my opinion) functionality of NeoVim. LSP - ie: Language Server Protocol. When LSP entered the programming world, it was a breakthrough event. Today, LSP is the "standard" - almost every programmer is using it on daily basis. Thanks to it, we constantly have functions such as code completion, intelligent syntax highlighting, code error marking, etc at our disposal. Everything without leaving your editor.
In the Kickstart.nvim configuration, we will use the Mason plugin to manage LSPs. It makes installing/managing new servers trivial.
-
Installing LSP
I will describe the installation as a list of steps that need to be done. It will just be easier that way.
- Run
nvim
in your terminal - Enter
:Mason
command - Press
CTRL
+f
and search for your language (for example: Bash) and hit the "Return" key. - After that, we have updated app list to download. Each entry is for a selected programming language.
- Use arrows to select LSP (for Bash name of package is: bash-language-server) and hit
i
key. - The chosen LSP installs now. When it is finished, we will be able to use all the features.
- Run
-
Using LSP in editing files
To use LSP, just open the file that is associated with the installed server (for bash it will be filename.sh). Now - just start coding. If everything went well, we will see a pop-up window with code completion. With the UP arrow and DOWN arrow we can choose which hint to accept. With the
CTRL
+y
keys we accept the highlighted hint. To scroll a pop-up with content from LSP we can use the combination:CTRL
+f
to scroll up /CTRL
+b
to scroll down. From now on we know how to write code faster and more efficiently :)
Step 5 - Useful shortcuts
Below is a description of three more “shortcuts” that are very useful in everyday work.
-
Code line bookmarks
Example situation: we have a project that has 5,000 lines of code. We are working on two functions that are at the bottom of both file. In order to not scroll through the whole project each time, we can add specific line numbers to the bookmarks. To do this, we first press
m
and then the key we want to bind with that line (e.g.a
). We can have as many such bookmarks as there are keys - which is quite a lot. I personally use about eight on a daily basis.To jump to a bookmark, press
'
and then the key we previously assigned (e.g.a
). After this combination, NeoVim will take us to the marked line of code.I would also add that NeoVim does this in a smart way - it does not remember the line number but the content that is there. Thanks to this, bookmarks still work regardless of how many lines we added before and after the bookmark.
-
Repeating with a DOT
The second very useful thing is the ability to repeat the last action on the designated places of other lines.
For example: we want to delete 5 words in several places. To do this, we move the cursor to the right place and type the combination
d
➔5
➔e
. This deletes five words from the beginning of the cursor.Now we want to repeat this action in several other places. We don't have to type this combination every time. It is enough that the cursor is at the beginning of the target place and we press
.
. When we press the dot, the indicated 5 words will be deleted.
-
Copy/mark/delete the current word
It is a simple set of key combinations to perform the above actions on a specific word. Of course, the cursor must be on that word.
Command Action d ➔ i ➔ w Delete the word y ➔ i ➔ w Copy the word v ➔ i ➔ w Select the word c ➔ i ➔ w Delete the word and start editing
Step 6 - Plugins and Lazy
-
Lazy plugin manager
In this configuration, our plugin manager is Lazy. To enter the module itself, we need to type
:Lazy
and press enter. In this window, we can update plugins, look at what plugins are installed or check if they are still compatible with the current version of NeoVim.The commands that are visible at the top of Lazy have a key in parentheses to trigger the action. To start updating plugins, we need to press
U
. To check compatibility, we pressC
(remember to use upper case, each action is capitalized!). To enter the logs, we pressL
, and so on.In fact, this is all the functionality of Lazy. It is a very simple and effective plugin manager.
-
Installing plugins
To add new plugins to NeoVim, we need to edit the
init.lua
file. This file is located in the directory:~/.config/nvim/init.lua
. Then we find the line (it is located near the 227th line):require('lazy').setup({
Under this line we will add more plugins. The easiest way is to add a plugin that someone wrote and shares on GitHub. As an example, we will use a plugin that can be found at github.com/tpope/vim-sleuth. In order for Lazy to find it, we only need to provide the GitHub user and the repo. In our case it will be:
tpope/vim-sleuth
. Now we still need to add the lua syntax. Our final entry will look like this:'tpope/vim-sleuth',
Each plugin needs to be added in a new line with apostrophes and a comma. Of course, after editing, we save the file. Next time we launch NeoVim, the Lazy window will pop up and the plugin will install itself.
Step 7 - Fuzzy Find
-
Searching for files in a directory
NeoVim allows us to search for files from the directory in which it is currently running. It also searches subdirectories. All we need to do is fire up Fuzzy File Find. To do this we need to press "space" ➔
s
➔f
. Then we already type the name of the file we are interested in - and finally press "return". This way we can edit more files in a very fast pace.
-
Searching for text in open files
To use Fuzzy Find, which will search for text in all open files, press "space" ➔
s
➔/
. Now after typing the search string, select the highlighted document and press "return". We will then be taken to a specific line in that particular file.
Conclusion
As you can see, NeoVim is a great tool for everyday programming work. After memorizing all those rules, modes, commands, etc. we can work really fast. As I wrote at the beginning — it is not too easy to start. Nevertheless, it is the training that makes the master. Good luck in your future coding with NeoVim :)