Understand NeoVim Modes Once and Navigate Them Like a Pro — Here's How

Written by maksimmuravev | Published 2023/02/03
Tech Story Tags: vim | neovim | editor | vim-tips | software-development | software-engineering | neovim-modes | navigate-neovim-modes-like-pro

TLDRNeovim's modes are like a human's daily routine - just when you've got it all figured out, it throws a curveball and unexpectedly throws you into insert mode. It's like trying to brush your teeth while making breakfast and suddenly breaking into a spontaneous interpretive dance - it's not always pretty, but it gets the job done. So yes, Neovim's modes can be overwhelming and confusing for the unprepared. But once you've familiarized yourself with the documentation, you'll be able to navigate Neovim like a pro and never look back.via the TL;DR App

Neovim's modes are like a human's daily routine - just when you've got it all figured out, it throws a curveball and unexpectedly throws you into insert mode. It's like trying to brush your teeth while making breakfast and suddenly breaking into a spontaneous interpretive dance - it's not always pretty, but it gets the job done. So yes, Neovim's modes can be overwhelming and confusing for the unprepared. But once you've familiarized yourself with the documentation, you'll be able to navigate Neovim like a pro and never look back.

So, how many modes does Neovim have?

To ensure that other text editors and IDEs don't even come close to its capabilities, Neovim utilizes a total of four modes:

  1. Normal Mode: This is the "eagle-eye" mode that allows you to quickly navigate through your code, delete, edit, copy, and paste easily.

  2. Insert Mode: This mode is where you write your text or code. It may seem slow and clunky, but getting the job done is necessary.

  3. Visual Mode: Allows you to select text and perform various actions. The possibilities are endless and are covered in depth in the documentation.

  4. Command-Line Mode: This enables you to run commands within the editor, such as searching and replacing text.

In conclusion, Neovim's modes are designed to make the editing process more efficient and streamlined. With a bit of learning and practice, you'll be able to take full advantage of Neovim's capabilities and make your coding experience much more enjoyable.

And with all these modes, you can switch seamlessly from one to another. It's like a charm! As for checking the current mode, it's typically displayed at the bottom-left corner of the editor window.

What are modes for?

Think of it like using different tools for different jobs. For example, we use a wrench to tighten bolts, a hammer to drive nails, and a drill to make holes in the wall. We don't use one tool for everything because it wouldn't be efficient or effective. For example, a drill with a hammer attached to it wouldn't work well for professional builders.

Nvim uses modes to separate the logic of the editor. We're used to text editors being a uniform environment where we type, edit, and highlight without thinking about the different logic and scripts that each action requires. But why should they all be combined?

The advanced editor, Neovim, operates like a professional. It encourages users to use specific tools for specific tasks. Each mode is its tool.

Normal mode

It is the default mode in Neovim. It's used for quick navigation, deletion, text editing, and more. You can always switch to any other mode from Normal mode, and you will almost always return to Normal mode after completing a task.

How to switch?

To transition to Normal mode, you can use the following commands, depending on your keyboard layout: <ESC>, CTRL+[, or CTRL+C, Caps Lock or even jk combination, which is very convenient because your index and middle fingers are already on those keys!

I like to think of Normal mode as "eagle 🦅 mode". This mode lets you see the whole picture, like an eagle soaring high above. When you need to type text, it's like the eagle diving down to grab the mouse, typing the text, and then flying back up.

It's important not to jump around like a pigeon, as it's slow, and you can't see anything. So instead, train yourself to return to Normal mode after any action automatically. This mode allows you to work quickly, accurately, and efficiently, enabling you to perform all the magic Neovim offers.

Insert Mode

Insert mode is simply for typing in text.

  • All editing and manipulation are done in Normal mode.
  • This includes deleting, copying, and pasting. Sure, you can use the backspace key to remove characters in Insert mode, but for more complex editing, it's best to switch to Normal mode.
  • Even navigating through text is more difficult in Insert mode, making it more efficient to switch to Normal mode for these tasks.

This separation is because most programmers spend more time navigating, reading, and editing code than writing new code. That's why we have a separate mode for writing code. Think of it like a typewriter - you can type but can't easily edit or delete your work. Insert mode is like that - you switch to it, type, and then switch back to Normal mode for editing.

When you enter Insert mode, the cursor changes from a square to a line. The mode in the bottom left corner will also change to Insert. As a result, many of Nvim's "magic" commands will become unavailable, and new powers specific to Insert mode will become available.

How to switch?

As we're flying over the landscape in Normal Mode, we switch to Insert mode if we need to add to the code. These are the commands to use (with different variations separated by a slash).

  • i / a To begin typing before or after the cursor, use the insert or append command. It's easy to remember because 'i' stands for insert, and 'a' stands for append.
  • I / A To start input at the beginning or end of the current line, use the 'I' (uppercase) or 'A' (uppercase) command. It's similar to the previous option but more powerful.
  • o / O To start typing on a new line below or above the cursor, use the 'o' or 'O' command. 'o' for the new line under the cursor, 'O' for the new line above the cursor.
  • s The command "4s" will delete the specified number of characters, in this case, 4, and switch to Insert mode.
  • S Entirely clear the line and switch to Insert mode.
  • r The command "r" replaces the character under the cursor. Just press 'r' to type the desired character to replace it. You will remain in Normal Mode. This is useful when you only need to correct one character but still want to stay in Normal Mode.

Command Line

How to switch?

To get to this mode, we have to enter the symbol :. Then, a colon will appear on the left bottom, after which we enter our command.

I haven't been familiar with Nvim for very long, so I haven't learned all of the command line functionality. But I'll tell you about two main areas where I started using the command line.

Saving and exiting from file

  • :w Save (aka write)
  • :wq Save and leave (quit) out of it, also :x can get the same effect
  • :q Exit (you will be asked if a file is not saved):q! exit forcibly without saving
  • :new <file> Creates a new file
  • :edit <file> Opens file immediately in Nvim for editing.

Searching

One thing to note is that in Nvim, searching is done using the / or ? instead of : symbol.

  • /{pattern} - Search for the specified pattern to the right of the cursor. This supports regular expressions.
  • ?{pattern} - Search for the specified pattern to the left of the cursor. This also supports regular expressions.
  • Enter - Jump to the first match of the pattern.
  • n - Go to the next occurrence of the pattern.
  • N - Go to the previous occurrence of the pattern.
  • :nohlsearch - Remove the persistent highlighting of the search pattern.

Visual Mode

Visual mode is excellent for tasks that require precision and accuracy. For example, we are already familiar with the 4s command, which deletes four characters and enters Insert mode. But what if we want to delete around 12 characters? Counting each character while staring at the screen can be tedious and may lead to mistakes.

Nvim offers many tools for more precise operations, and Visual mode is one of them. This mode allows you to easily select and manipulate text with greater accuracy, making tasks that would otherwise be tedious and error-prone much simpler and more efficient.

How to switch?

To enter selection mode, press the v key. Once in this mode, you can use the arrow keys to select text character by character. You can copy the selected text to your clipboard by pressing y or delete it by pressing d. Almost all actions that can be done in other modes can also be done in the Visual mode.

But did you know that the Visual mode is divided into three sub-modes? These include:

  • Single character selection

  • Line-by-line selection

  • And block selection (which is excellent for working with tables) by using Ctrl+V

Give it a try and experiment with each of these sub-modes using the arrow keys. You'll quickly see the difference between them.


Here's a little Nvim trick for you. While in Normal mode, place your cursor anywhere within a paragraph and type vip. This command stands for "Visual mode - Inner Paragraph" and allows you to select and work with text at a higher level of abstraction.

This is one of the most potent aspects of Nvim. You can accomplish various tasks by combining multiple commands and modes with motion commands. We'll delve deeper into navigation and motion commands in the next part of our discussion.


Written by maksimmuravev | DevOps Engineer.
Published by HackerNoon on 2023/02/03