Neovim: Get started with Neovim on Mac ver. 11

Last Updated on August 2, 2022 by shibatau

I’m rewriting now.

I. What is Neovim

Here is its vision and goals from Neovim.

Vision

Neovim is a refactor, and sometimes redactor, in the tradition of Vim (which itself derives from Stevie). It is not a rewrite but a continuation and extension of Vim. Many clones and derivatives exist, some very clever—but none are Vim. Neovim is built for users who want the good parts of Vim, and more.

Goals

  • Enable new contributors, remove barriers to entry.
  • Unblock plugin authors.
  • Deliver a first-class Lua/LuaJIT scripting alternative to Vimscript.
  • Target all platforms supported by libuv.
  • Leverage ongoing Vim development.
  • Optimize out of the box, for new users but especially regular users.
  • Deliver consistent cross-platform experience.
  • In matters of taste/ambiguity, favor tradition/compatibility…
  • …but prefer usability if the benefits are extreme.

II. Insstallation

We can learn how to install Neovim on Mac here:

Neovim for MacOS

I have installed all of the plugins suggested above. Here is the Python file opened in Neovim.

III. Shortcuts

red: most frequently used

blue: frequently used

Exiting

Close all files
:qa
Close all files and abandon changes
:ga!
Save

:w
Save and close file
:wq
Close file and abandon changes
:q!

Visual mode

Enter visual mode
v
Enter visual line mode
V
Enter visual block mode (select a rectangular part)

Ctrl+v
Delete selection
d
Replace selection
s
Yank selection

y

Words
Previous / next word
b / w
Previous/next end of word
ge / e
Line

Start of line
0
Start of line after whitespace
^

End of line
$
Move down in a long line
gi
Move up in a long line

gk
Character
Go forward to character c
fc
Go backward to character c

FC
Document
First line
gg
Last line

G
Go to line n
:n
Go to line n
nG
Search
Search pattern
/pattern
Next matching search pattern
n
Previous matched
N
Next whole word under cursor
*
Previous whole word under cursor
#

Tab pages
:tabedit [file] edit file in a new tab
:tabclose close current tab
:tabs list all tabs
:tabfirst go to first tab
:tabn go to next tab
:tabp go to previous tab

Editing

a append

A append from end of line

o next line

O previous line

s delete char and insert

S delete line and insert

C Delete until end of line and insert

R enter replace mode

u undo changes

ctrl+r redo changes

Clipboard

Delete character
x

Delete line (cut)
dd

Yank line (copy)
yy

Paste
p

Paste before
P

Paste from system clipboard
“+p (type three keys)

Paste to system clipboard
“+y (type three keys)

Operators list

d delete
y yanc (copy)

> indent right
< indent left
= autoindent
g~ swap case
gU uppercase
gu lowercase

Text objects
p paragraph
w word
s sentence

The cheatsheet for Vim keys are here:

Vimscript – Surround word under cursor with quotes

IV. Surround words with quotes

You can type quotation marks around words with keys.

You can learn more from the post:

Vimscript – Surround word under cursor with quotes

V. NERDtree

You can learn how to use Nerdtree here:

Open or close the nerdtree windows

:NERDTreeToggle
Open menu
m
Close Menu
Esc

You can learn more:

Intro to Nvim File Explorer Plugin Nerdtree

VI. Buffers, Tabs and Windows

Let’s suppose you have four text files, named sample1.txt, sample2.txt, sample3.txt and sample4.txt in the same directory. You can create the four buffers and three tabs for them as shown in the image below, given you have set Nevim configuration as suggested in the article linked in the II.

1.Open the files with Neovim with the following commands in the terminal:

nvim sample1.txt sample2.txt sample3.txt sample4.txt

2.Run the commands in the Command Mode for creating a new tab:

:tabnew sample2.txt

3.Run the command in the Command Mode for creating a window:

:split sample3.txt

4.Run the command in the Command Mode for creating another tab:

:tabnew sample4.txt

Useful commands

Buffers

:ls Show all the buffers

:buffer filename (you can use Tab key)

:bufferN (N is buffer number)

:bnext To go to next buffer

:bprev To go back to the previous buffer

Tabs

:tabs Show all the tabs

:tabnew file.txt Open file.txt in a new tab

:tabclose Close current tab

:tabnext Go to next tab

:tabprev Go to previous tab

Windows

:split filename Sprit window horizontally

:vsplit filename Split window vertically

:new filename Create new window

<Ctrl-w>+c Close a window

<Ctrl-w>+h/j/k/l Moves the cursor to left/bottom/top/right

You can learn the difference among buffers, tabs and windows and more commands here:

Using buffers, windows, and tabs efficiently in Vim

VII. Python in the terminal

Open Neovim and run the command:

:term

VIII. Plugins

1.nvim-tree.lua

Keybindings

  • <CR> or o on the root folder will cd in the above directory
  • <C-]> will cd in the directory under the cursor
  • <BS> will close current opened directory or parent
  • type a to add a file. Adding a directory requires leaving a leading / at the end of the path.you can add multiple directories by doing foo/bar/baz/f and it will add foo bar and baz directories and f as a file
  • type r to rename a file
  • type <C-r> to rename a file and omit the filename on input
  • type x to add/remove file/directory to cut clipboard
  • type c to add/remove file/directory to copy clipboard
  • type y will copy name to system clipboard
  • type Y will copy relative path to system clipboard
  • type gy will copy absolute path to system clipboard
  • type p to paste from clipboard. Cut clipboard has precedence over copy (will prompt for confirmation)
  • type d to delete a file (will prompt for confirmation)
  • type D to trash a file (configured in setup())
  • type ]c to go to next git item
  • type [c to go to prev git item
  • type - to navigate up to the parent directory of the current file/directory
  • type s to open a file with default system application or a folder with default file manager (if you want to change the command used to do it see :h nvim-tree.setup under system_open)
  • if the file is a directory, <CR> will open the directory otherwise it will open the file in the buffer near the tree
  • if the file is a symlink, <CR> will follow the symlink (if the target is a file)
  • <C-v> will open the file in a vertical split
  • <C-x> will open the file in a horizontal split
  • <C-t> will open the file in a new tab
  • <Tab> will open the file as a preview (keeps the cursor in the tree)
  • I will toggle visibility of hidden folders / files
  • H will toggle visibility of dotfiles (files/folders starting with a .)
  • R will refresh the tree
  • Double left click acts like <CR>
  • Double right click acts like <C-]>
  • W will collapse the whole tree
  • S will prompt the user to enter a path and then expands the tree to match the path

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.