NeoVim Reference Flashcards
What ‘mode’ is vim in when you open it?
vim always opens in ‘normal mode’.
To edit a file press ‘i’ for insert mode.
To exit insert or any mode press ‘esc’ key which takes you back to normal mode.
To enter command mode use ‘:’
To open neovim use the app image currently in the home directory
How to open vim and exit nvim
To open vim use command
> ~/nvim.appimage my_file_name
- this will open the file specified in the editor
to exit
> :q
- will exit editor and return to the command prompt if no unsaved changes were made to the file.
> :q! exit without saving.
To save a file?
To save a new file
> :w my_new_file.txt
- write new file
save changes to a file that already exists
> :w
- write
save changes to a file that already exists and quit
> :wq
- save and quit
Remove saved file
> :!r my_file_name
What is a buffer?
A buffer is a holding area in memory for text. A buffer is not a file until it is saved.
How to add a new buffer?
To add a new buffer, from the current buffer switch to command mode:
> :e my_file_path
- opens a new buffer with specified file
switch between buffers
> :bp
- buffer previous
> :bn
-buffer next
to create a new empty buffer
> :enew
- empty buffer added. Note that the new buffer will be removed automatically if you switch to a another buffer without adding any content.
Add a new buffer in the background without switching to it. In command mode:
> :badd my_file_name.txt
to close a buffer
> :bd
- close the current buffer
Add the contents of one bufferv to another?
put the cursor where you want to insert the content and in command mode
> :r file_name
reads file and insert into current buffer.
Visual mode?
To enter visual mode; from normal mode press ‘v’. This will allow you to highlight text for different operations such as
- coping
in visual mode press ‘y’ for ‘yank’, you will exit visual mode back to normal mode you could perform a paste operation
-paste
Place cursor where you want to paste text, in normal mode and press ‘p’ to ‘put’ text in desired location.
- delete by pressing ‘d’
-sorting lines of text alphabetically
in visual mode highlight lines press “:” brings up a prompt
: ‘<, ‘>sort ui
Split view?
From a open file switch to command mode
> :split my_file_name
- will open file both files in a split horizontal view. To switch between files ‘ctrl ww’
for a vertical split.
> :vsplit my_file_name
- splits view vertically. Note that you can open the same file open twice in split view.
Since ‘ctrl ww’ will close the browser, in command mode use
> :wincmd w
- this will switch windows
Open vim within split-screen mode. From command line.
> vim -o my_file_name.txt my_other_file_name.py
- opens both files in horizontal view
- use -O for vertical split.
How line number to a file
From command mode:
> :set number
- will number each line of the current file.
To remove the numbers
> :set nonumber
Note these changes will not persist. To have your preferences remembered you must have a .vimrc flile in your home directory that will contain your configurations.
Open file at a specific line number?
At the command line
> vim +20 my_file_name.txt
What does ‘SHIFT a’ (A) do from normal mode?
This will append to the current line, note that nvim will also switch to insert mode.
What is a motion?
A motion is a navigation operation such as
w - move cursor to the start of the next word
e - move cursor to the end of the next word
$ - move the cursor end of the line
Motions with a count?
Prefixing a motion with a number performs that motion, that number of times.
3e will move the cursor to the end of the third word forward
0 moves to the start of the line
Delete motion
In normal mode ‘d’ plus a motion will perform a deletion.
dd- delete the entire line and add it to the paste buffer
2dd - delete two lines
dw - will delete a word
d$ - will delete from cursor to the line
d2w - will delete next two words
Undo, Redo
From normal mode:
‘u’ undos last change.
‘SHIFT u’ (U) undos all the changes on a line.
To perform a redo use ‘CTRL r’
Put
In normal mode press ‘p’ to put what’s in the paste buffer after the cursor.
Replace a character?
From normal mode: Place the cursor on the character to be replaced press ‘r’ followed by the character you wish to replace the selected character with.
‘rx’ - will replace selected character with ‘x’
‘c’ is the change operator and therefore can be used with motions to perform specific changes .
- To replace from a character to the end of the word use ‘ce’ , type replacement then hint ‘ESC’ to go back to normal mode.
- To change the rest of a line place the cursor where you wish to start and press ‘c$’
replace the line then hit ‘ESC’.
Ctrl - g
From normal mode pressing ‘CTRL g’ will bring up info about the current file.
- ‘SHIFT g’ moves to the end of the file
- ‘gg’ moves to the start of the file
- line number + ‘SHIFT g’ will cursor to that line number
File search
To search for a word in the file. from normal mode:
‘/word_you_are_searching_for’ + ‘Enter’
- to select each result forward ‘n’
- to select backwards ‘SHIFT n’
-to return to original location ‘CTRL o’ and ‘CTRL i’
Note you can use ‘/ ESC’ to clear the search
Find matching brackets
In normal mode place cursor on bracket
press ‘SHIFT 5’ (%)
Find and replace
Enter command mode
:s/old/new/
- replaces first occurrence in the line
:s/old/new/g
- replace all occurrence in the current line
:%s/old/new/g
- change every occurrence in the file
:%s/old/new/gc
- replace all occurrence in the file with but prompts for change conformation.
How to execute an external command?
Enter command mode
‘:!shell_command’ then ENTER to execute command.
Auto complete suggestions
Press ‘CTRL x TAB’
gives list of suggestions use ENTER to accept.