DEV Community

Amin
Amin

Posted on

Modal editing — Neovim From Scratch

This episode is part of a serie called Neovim From Scratch where I show you the basics of how to use Neovim (and Vim) when editing text.

Editing files is at the heart of Neovim.

However, contrary to traditional text editors, Neovim does not allow inputing text by default inside of a file.

If you tried it, you might have activated some keybinds that were supposed to do something other than appending a character to a file.

This is because Neovim is a modal text editor.

Modal means that it has modes, that you can activate through a serie of keybinds and/or commands.

The two most important modes in Neovim are the NORMAL & INSERT mode.

As you probably guessed, the INSERT mode is used for inserting text.

And the NORMAL mode is used for normaling text... Wait, what?

Actually, the NORMAL mode is mostly used for two things.

The first one, we already saw, was to issue commands. Those commands were edit, write & quit.

Actually, without knowing it, we already saw a third mode, which was the COMMAND mode.

When you are in NORMAL mode and you press the colon key (:), you actually quit the NORMAL mode and enter the COMMAND mode.

In COMMAND mode, you can use a wide variety of commands that you can use in order to call specific routines inside of Neovim, like opening a new buffer, writing to the file system or quiting the editor.

But what does the NORMAL command do besides allowing us to use the COMMAND mode?

Actually, this is where you may have hit a rock. By trying to type something into your text editor, you might have guessed that it does not work immediately, maybe you got lucky and hit the i key and you have been able to append some characters to the buffer.

Actually, if you open a file and try to use the i key, you'll see that you will be able to append some text to it. Let's try to test that quickly.

nvim
Enter fullscreen mode Exit fullscreen mode
i
Enter fullscreen mode Exit fullscreen mode

Now, you can write anything that you want. It does not matter.

Once this has been done, try to issue a command and see that it does not work as expected.

If you try to literraly type the :write test.txt command or :quit, you'll see that it appends those characters to the buffer.

That is no good!

This is actually normal behavior of Neovim, and what you have done after pressing the i key in NORMAL mode is changing to INSERT mode.

In insert mode, almost everything will append characters to the buffer. You might even use the arrow keys to move inside of the buffer (we will see how we can do it better using idiomatics keybinds in normal mode later).

There is one key that do not append anything in INSERT mode, but rather changes the mode from INSERT to NORMAL which is the escape key.

Hitting the escape key will help you go back into NORMAL mode.

Try appending some characters to this buffer by using the i key to change from NORMAL mode to INSERT mode. Once this has been done, use the escape key in order to go back into normal mode.

Using what we saw in the last serie. Try to save the buffer into a file named insert.txt and try quitting and opening the file again inside Neovim to check that it has been correctly saved.

The commands used for writing and quitting are write & quit.

You should have seen in the lower-left corner of the Neovim screen that there is actually an indication about the current mode you are at.

Except for when you are in NORMAL mode, you should see the mode indicated there when changing into COMMAND & INSERT modes.

Lastly, we will see another mode that might be useful later when we will work on advanced keybinds which is the VISUAL LINE mode.

Try opening Neovim with a new buffer. This might be an existing file, or a new file, it does not matter.

$ nvim
Enter fullscreen mode Exit fullscreen mode

Good to know: if you don't provide an argument for the nvim command, you are actually using a buffer that has no file attached to it. This can be useful for testing things out.

Once this is done, try appending many lines to this buffer. Remember to use the i key when in NORMAL mode in order to append characters to this buffer.

Once this has been done, go back to NORMAL mode using the escape key.

Now that this has been done, you can use the V keybind.

Important thing to note for now and for future articles, the casing is important. v is different than V since the latter is in uppercase, whereas the first has been written using a lowercase letter.

If you want to issue this keybind in Neovim, you should use the SHIFT key in you keyboard.

If you managed to do that, you should now see that you are in VISUAL LINE mode. In this mode, you can select entire lines of text, an many more at once.

Actually, if you tried to use the lowercase version of V, which is v, you should see that you entered the VISUAL mode.

The difference between the VISUAL & VISUAL LINE mode is that in VISUAL mode, you select characters of text, whereas in VISUAL LINE, you select entire lines at once.

If you need to go back to NORMAL mode, you can use the escape key.

That's it for this article! We now know a little bit more about modes in Neovim, and we can actually go between modes inside our editor.

In the next article, we will talk a bit more about the different commands that exists inside the NORMAL mode, and how we can actually move inside the editor without having to use the arrow keys or the mouse.

See you!

Top comments (0)