-
Notifications
You must be signed in to change notification settings - Fork 2
Neovim
There are many choices for development environments, including fancy integrated development environments (IDEs) like PyCharm, RStudio, Jupyter, VSCode and text editors with graphical user interface (GUI) such as Sublime Text. Why use such a minimal text editor instead? A few important motivations:
- Resource usage: IDEs and GUI-based text editors can use a significant amount of resources (CPU usage and RAM). Even
emacs
, which can be run in a terminal, can have a significant resource footprint when plugins are used. Vim and neovim by contrast, use a very small amount of resources, and importantly, that resource usage does not increase much even with many plugins. Neovim plugins written in Lua are especially efficient because they use just-in-time compilation with LuaJIT, giving them near native performance in speed and resource usage. Minimizing resource usage is especially important in high performance computing environments where resources should be allocated to data processing rather than writing code. - Portability: connecting IDEs and GUI-based editors to remote computing sessions that must often be used for high performance computing can be very challenging. By contrast, neovim can be run inside the remote computing session, meaning you only need a terminal and SSH access to do you work. If you are already using neovim for remote computing, it is much easier to use the same environment for your local text editing and coding as well, and you can easily have the same neovim setup locally on Linux, macOS and Windows (with some limitations).
- Scalability: When working with large amounts of data, IDEs are prone to crashing because they often try to inspect the data loaded into the workspace. Neovim and its plugins have minimal interaction with any workspaces they are running and are not affected by the size of the data loaded into the workspace.
Neovim is a fork of vim and inherits the many useful features that are built into vim. There are many resources on how to learn the basics of vim (LINKS), so this guide will only cover the most basic features.
Vim is a model editor, meaning that the same keys will have different effects depending on which mode you are in. Switching between modes is a critical part of using vim that must be understood to use it effectively. The most important modes to know are (key bindings in parentheses):
- Normal mode (
Esc
) - this mode is where you issue most commands to vim. - Insert mode (
i
/a
/o
/I
/A
/O
) - this mode is where you edit text like in other text editors.i
enters insert mode before the current character,a
enters it after, ando
opens a new line in Insert mode below the current one. In parallel,I
enters insert mode at the beginning of the line,A
inserts at the end of the line, andO
enters Insert mode in a new line above the current one - Visual model (
v
) - this mode allows you to select text character by character - all movement keys described below can be used to move the cursor. - Visual line mode (
V
) - this mode allows you select text line by line (useful for selecting lots of text quickly) - a limited set of movement keys can be used to movement cursor. - Terminal mode (same as Insert) - this mode is specific to neovim and only works in terminal buffers. It is equivalent to Insert mode for text buffers, but you cannot edit text already printed above.
While neovim is not a shell, it has many commands that may not have a key binding assigned, or may be too complex to assign a binding. To enter the command prompt, press :
while in Normal mode. Any shortcut you see here that begins with :
means that you will be entering the command into the command prompt.
Neovim provides some safeguards to prevent you from quitting without saving. If your Neovim session is prematurely terminated without saving, the latest version of your file should be saved in a swap file. This file will automatically be opened the next time you edit the file, and you will be asked if you want to recover the file or edit anyway. You should recover if the modification time of the swap file is newer than the original file, and you should delete the swap file if its modification time is older than the original file. Some important keybindings for saving and quitting:
-
:w
- save your changes. -
:q
- quit vim. You should instinctively use:wq
in most cases to save your changes before quitting, but you may sometimes open a file without changing it, makingw
unnecessary. Even more rarely you may modify a file and not wish to save the changes. In this case, you can use:q!
- use this with caution!
Neovim uses a set of keybindings for moving the cursor that are designed to maximize efficiency and minimize finger travel. An important concept is that your fingers should return to the homekeys (IMAGE) after every movement. This helps to explain the seeming arbitrary set of movement keys.
-
h
,j
,k
,l
- These keys move left, down, up, and right, respectively. They are also the home keys for the right hand! -
^
,$
- Move to the beginning and end of a line, respectively. Remember to useI
orA
instead if you want to enter Insert mode directly! -
Ctrl+f
,Ctrl+b
- These keys move down and up one half page, respectively. They are essentially equivalent to the Page Down and Page Up keys on full sized keyboards. -
w
,W
- These move one word to the right, putting the cursor at the beginning of the next word.W
skips some characters thatw
does not, making movement faster but less precise. -
e
,E
- These move one word to the right, putting the cursor at the end of the next word.E
skips some characters thate
does not, making movement faster but less precise. -
b
,B
- These move one word to the left, putting the cursor at the beginning of the previous word.B
skips some characters thatb
does not, making movement faster but less precise. -
{
,}
- These move to the beginning and end of a code block. Code blocks are language specific but often follow function/class definitions or white space.
Vim provides several keybindings for removing or replacing text. Several of these keybindings are compositional, meaning that you combine the keybinding with a movement key.
-
d
+ movement key - delete from the cursor position to wherever the movement key sends the cursor and stay in Normal mode. For example, to delete to the end of a word, usede
. Use this when you still want to move your cursor after deleting instead of going into insert mode. -
c
+ movement key - delete from the cursor position to wherever the movement key sends the cursor and stay in Normal mode. For example, to delete to the end of a word, usece
. Use this when you want start editing text immediately after deleting. -
x
- delete the current character under the cursor. -
dd
- delete an entire line and stay in Normal mode -
r
- replace the character under the cursor with a new one, and return to Normal mode - You can also select text with Visual or Visual Line mode, and then use
x
,d
orc
to delete it with the same effects as described for those keys above.
Vim provides a very sophisticated system for searching for and replacing text. The main key bindings are:
-
/
- Open the search prompt -
n
,N
- Search for the next and previous match, respectively. Vim uses a syntax similar tosed
for replacements. A simple replacement will take the form ofs/text/replacement/
. Adding ag
to the end will replace all instances of the search query within, and the search query can include regular expressions (LINK). You can either select the text over which to do the replacement with Visual or Visual Line mode, or append a%
to the beginning of the replacement to apply the replacement to the whole file (use this with caution!)
Vim use a series of buffers to store text for copying and cutting text. This guide will only describe basic operations with the local buffer - see the plugins section for more details on how to copy from the system buffer. Important key bindings:
-
y
- copy text selected by Visual or Visual Line mode -
yy
- copy the current line under the cursor while in Normal mode - this saves you from selecting with Visual or Visual Line mode if you only want to copy one line - Deleting text with
d
,c
, orx
as described in the deletion and replacement will automatically place it into the local buffer, making these equivalent to the "cut" commands used in other text editors -
p
/P
- paste the contents of the buffer after or before the cursor, respectively.
Panes are a useful way to split your neovim session, either to view a one large file in more than location simultaneously, or to view different files side by side.
-
:sp
/:vsp
(from Normal mode) - Split your screen horizontally and vertically, respectively. Vertical is usually most useful on wide screen monitors. -
Ctrl
+h
,j
,k
,l
- Move the cursor between panes (see section on Panes below) in the same directions ash
/j
/k
/l
move the cursor in text.