Μετάβαση στο περιεχόμενο

Neovim Usage

Description

Neovim or otherwise Nvim is a text editor that runs on the terminal. The fact that it runs on the terminal, without a user interface, makes it an extremely fast and effective text editor. It falls into the category of Personalized Development Environment (PDE), as opposed to other widespread Integrated Development Environments (IDE) such as VScode, Intellij, etc. It allows the user to configure custom keybinds, the working environment and the plugins it uses. It is based on the text editor Vim, which in turn is based on Vi, but with new features.

Installation

To install Neovim, visit here, where there are installation instructions depending on the operating system you use.

Usage

In Neovim, there are different Modes for editing a file. The most common Modes are Normal mode (You start in this mode when you open Neovim), Insert mode, Visual mode and Command-Line mode.

Normal mode

This mode allows the user to navigate their file, to copy/paste text, to delete parts of text, to undo/redo changes and to execute editing commands in general. The navigation can either be done with the arrow keys, or the Vi/Vim/Neovim way (that is faster), which is by pressing the keys h for left, j for down, k for up and l for right.

Insert mode

This mode allows the user to write/insert text just as other text editors and the user can change into this mode from Normal mode by pressing the i key. To change back into Normal mode, the user can press Esc or Ctrl+C.

Visual mode

This mode allows the user to select and edit parts/blocks of text and the user can change into this mode from Normal mode by pressing v or V. If the user changes into this mode using v, then the character the cursor is selected and with the navigation keys, the user can select words and lines. If the user changes into this mode using V, the line the cursor is on is selected and the user can also select lines by navigating, but Not individual words. To change back into Normal mode, the user can press Esc or Ctrl+C.

Command-line Mode

This mode allows the user to execute editing commands for the file that he is editing. To change into this mode from Normal mode, the user must press : key and then type the command that he wants to execute, followed by pressing Enter. For example, the command :%s/<Old text>/<New text>/g changes every occurrence of <Old text> with <New text>. Additionally, the user can run terminal commands by writing :! <command> (ex. :!ls or :!grep -r "search_term" /path/to/search). To change back into Normal mode, the user can press Esc or Ctrl+C.

Saving and exiting Neovim

To exit Neovim, the user must run the command :q (quit). The user can save files by running :w (write), while saving a file and exiting can be achieved by running the command :wq (write quit). If the user tries to exit without saving the changes, the command :q will fail. In case the user wants to exit without saving, he will have to use the command :q! (force quit) that force exits Neovim.

Useful keybinds

Some of the most basic and useful keybinds that the user can use while in Normal mode or Visual mode are the following:

  • d+d : Deletes the line on which the cursor is located and stores it into the default Register.
  • y+y : Copies the line on which the cursor is located into the default Register.
  • /<keyword> : Searches for the pattern <keyword> in the file.
  • y : Copies the selected test into the default register.
  • p : Pastes the content of the default register.
  • d : Deletes the selected text and saves it into the default register.
  • "+++y : Copies the selected text into clipboard (also known as '+' register).
  • "+++p : Pastes the selected text from clipboard (also known as '+' register).
  • u : Reverts the latest change in the file (undo).
  • Ctrl+r : Restores the latest reverted change in the file (redo).
  • Shift+z+Shift+z : Saves and closes Neovim.
  • g+g : Moves the cursor to the start of the file.
  • Shift+g : Moves the cursor to the end of the file.

For more information on the default keybinds, visit here

Configure Neovim

To configure Neovim, the user must go to where the configuration (config file) file of Neovim is located (usually ~/.config/nvim in Linux. If the file doesn't exist. it will have to be created). Afterwards. the user needs to create a file named init.lua inside the nvim folder.

The file init.lua runs every time Neovim opens, so we can store our settings for Neovim in it.

The recommended way of configuring Neovim is by splitting the file init.lua into multiple, smaller files (ex. a file containing the keybinds remap.lua, a file containing the plugins plugins.lua, etc). The original init.lua requires the smaller files, so that the configuration is easier to manage.

If the user does not want to create their own config file from scratch, there are pre-built configurations like kickstarter.nvim (that has its own file for configuring Neovim and is a good starting point for someone to start), LazyVim (that is almost a feature complete IDE with a lot of plugins and configured keybinds), etc. For a detailed guide about configuring Neovim, visit here.

Setting Up Options

In Neovim, the user can configure a Neovim option by adding the following line into the init.lua.
set {option}
Example code for displaying lines in Neovim:
set number
For more information about the available options, visit here.

Setting Up Keybinds

Keybinds allow users to bind a keyboard shortcut for quick execution of a command or function.

To add a new keybind, the user must go to the init.lua file and add the following line:
vim.keymap.set(MODE, KEYBIND, COMMAND, OPTIONS)
MODE is the mode that the keybind will be for (ex. n for Normal mode, i for Insert mode).
KEYBIND is the key combination that needs to be pressed to run the the COMMAND.
COMMAND is the action that Neovim will run when the keybind activates.
OPTIONS are the options that will be set for the keybind.

Keybind example:
vim.keymap.set ('n', '<leader> s', ':%s/<Old text>/<New text>/g', {silent = true})
(The option {silent = true} will make the keybind NOT print the COMMAND when the keybind activates).

Configuring Plugins

Install a Plugin manager

In order to configure/install plugins, the user must first install a plugin manager, like lazy.nvim. For a more detailed guide about how to install lazy.nvim, visit here.

Install a Plugin

The user can install a plugin with lazy.nvim by adding the following code into init.lua:

    require("Lazy").setup({

        "Creator1/plugin1",

        "Creator2/plugin2",

})

Creator1/Creator2 are the creators of the plugins. plugin1/plugin2 are the names of the plugins. For example, to add the plugin Comment.nvim, the user needs to add the line numToStr/Comment.nvim into require("Lazy").setup({}), where numToStr is the name of the plugin creator and Comment.nvim is the name of the plugin.