Terminal: Exploring Vim after 20 years of writing code
As a middle-aged CEO and developer, I’ve found myself increasingly occupied with running the company and maintaining a general project overview lately. I needed something new to explore. Therefore, after more than 20 years of coding in regular text editors and IDEs, I decided to explore Vim.
Why? There was no particular reason; I was just a little curious and needed a challenge. I did like the idea that you spend more time editing than writing, so there should be a specific mode for that.
This text is not a general introduction to Vim - there are many available on the Internet - but I have written down a few notes from that endeavor.
The whole thing did not start with Vim. At first, I took a look at Helix. Helix is an editor that functions similarly to Vim and Neovim. It’s fast, written in Rust, and requires almost zero configuration. I liked it a lot and spent some time in it. It took a bit of getting used to working in different modes, but I could see the appeal.
Helix is still a very niche product, and after reading and reviewing tutorials, I switched to Neovim mainly because of the extensive amount of material and plugins available. Neovim is an extension of Vim, so learning this provides a solid foundation for Vim motions as well. I also liked learning Lua, the programming language used to configure Neovim.
Many people suggest starting nvim
, pressing the <ESC>
key, typing :Tutor
, and then <ENTER>
if you need help with the basics.
Another great place to start is to go all-in on the kickstart.nvim project. You can also read the project’s config file, which is full of comments and ideas, directly on GitHub and pick and choose the things you like.
Both approaches are good options and might suit you, but I needed to see what was doable, so I started watching some "set up Neovim from scratch" videos on YouTube to get into it.
Have I switched to Neovim exclusively? No, I have not. Have I gained insights into what’s out there? Yes, I have. Currently, I’m in the middle, but I can see myself using an editor with Vim key bindings soon.
Key bindings
The most interesting aspect of Vim and Vim-based editors is learning how to work in different modes.
Normal mode
You start in normal mode, which is your base, and all interactions begin here. Move around files and select what to edit.
Navigation
-
Left, down, up, right
h
j
k
l
-
Start of line
0
/ End of line$
-
f @
- move cursor to next occurrance of @, i.e.,f _
move to next _ -
t @
- move cursor to character before next @.
-
Next word
w
/ WORDW
-
Previous word
b
/ WORDB
-
End of next word
e
/ WORDE
-
Previous blank line
{
/ Next blank line}
-
Previous sentence
(
/ Next sentence)
-
Move cursor to first line of document
gg
-
Move cursor to last line of document
G
-
Move cursor to line
#
-#G
-
Go
#
lines down -#j
/ Go#
lines up -#k
-
Move up a screen
Ctrl u
-
Move down a screen
Ctrl d
-
Go to correct indentation level
S
Editing text
Edit
a
- addi
- insert
-
A
- start editing at the beginning of the line -
I
- start editing at the end of the line -
.
- redo the last command
ciw
- Change Inner Word
Folding code
zR
- Open all
zM
- Collapse allzo
- OpenzO
- Open all from the current locationzc
- ClosezC
- Close all from the current locationza
- Toggle current foldzi
- Toggle code folding
Quit vim
ZZ
Auto-complete
Ctrl P/N
Selection
- Select current word -
*
Files and buffers
An open file lives in a buffer. You can display multiple buffers at the same time, and you can have the same file open in many buffers.
Buffers
:ls
- List buffers:bn
- Next buffer:bp
- Previous buffer:bw
- Close buffer:bd#
- Close buffer #
Splits
:sp
- split horizontally:vs
- split verticallyZQ
- close split
Macros
Record interactions and reuse them with macros.
Recordings
- Record:
q a
(keystrokes)q
- Play:
@ a
- Play multiple (15) times:
15@a
Visual mode
Working in visual mode allows you to select text and view a visual representation of what you have selected.
Select a paragraph
Vap
- select around the current paragraphVip
- select inside the current paragraph
Select block
v%
- select from the current position to the next matching character
Select until next instance
v/foo
- select from your current position to the next instance of foo, andn
expands to the next instance.
Select Word
viw
andviW
Copy a word and replace another word
- To copy a word under the cursor, press
bvey
- To replace the word under the cursor, press
bvep
Insert mode
Ctrl o
- temporarily enter Normal mode.Ctrl w
- delete the word to the left of the cursor.Ctrl u
- delete from the cursor to the beginning of the line.
Third-party plugins
Before committing to third-party plugins and extensions, it is wise to explore the basic commands in your applications. It is also always a good idea to keep dependencies to a minimum.
That said, there are a few plugins that significantly enhance the user experience in Neovim, and if they have a good reputation and solid support, I’m not opposed to using them.
The foundation
From what I gathered during my research, it appears that lazy.nvim is a reliable way to manage plugins.
Here is a list of the ones who function as a foundation for my setup:
- Package manager for downloading plugins (lazy.nvim)
- Download LSPs, formatters, and linters (mason.nvim)
- Formatting code (conform.nvim)
- Linting code (nvim-lint)
- Syntax highlighting and code completion with LSPs
- nvim-lspconfig - LSP interaction
- blink.cmp - code completion with support for LSPs and external sources
- nvim-treesitter - syntax highlighting
- nvim-tresitter-textobjects - working with text objects
The following ones are outstanding:
telescope.nvim
Telescope is an extendable fuzzy finder over lists for Neovim. It is "built on the latest awesome features from Neovim core," which I guess must be good. It is centered around modularity, allowing for easy customization.
vim.keymap.set("n", "<leader><leader>", builtin.oldfiles, {})
vim.keymap.set("n", "<leader>ff", builtin.find_files, {})
vim.keymap.set("n", "<leader>fg", builtin.live_grep, {})
vim.keymap.set("n", "<leader>fb", builtin.buffers, {})
vim.keymap.set("n", "<leader>fr", builtin.registers, {})
oil.nvim
If you like to step away from the file tree view on the left-hand side of the code and use a more native-feeling solution, you want to try oil.nvim.
Hitting the - key displays a list of the current directory, and you can use your Vim skills to add, delete, and rename files.
Using g
will display other options if you have Which Key installed.
Currently, this is my preferred method for quickly and easily performing file-related tasks within Neovim.
nvim-surround
Surround text with a character or a tag.
add
withys{motion}{char}
delete
withds{char}
change
withcs{target}{replacement}
ysiw)
– add "(" and ")" around word
ys$t
- adds a tag around the text from the cursor position to the end of the line
ds"
- delete " around text
dst
- delete tag around text
cs'"
- change quotes from '
to "
// Comment.nvim
Comment your code in style.
-
NORMAL mode
gcc
- Toggles the current line using a linewise commentgbc
- Toggles the current line using a blockwise comment
-
VISUAL mode
gc
- Toggles the region using a linewise commentgb
- Toggles the region using a blockwise comment
Putting a smile on your face
These are the others I use at the moment;
-
Interface/UX
- α alpha-nvim - start up Neovim with style
- lualine.nvim - easy to configure Neovim status line
- colorizer.lua - display your color codes in color
- :GuessIndent - fast indentation style detection written in Lua
- Noice (Nice, Noise, Notice) - completely replaces the UI for messages, cmdline, and the popup menu.
-
Navigation
- Neovim-Tmux Navigation - navigate between tmux and Neovim
-
Git
- fugitive.vim - working with Git inside Neovim (haven’t had time to explore, but the name alone puts it on the list)
- gitsigns.nvim - deep buffer integration for Git
Links
References and videos
-
"Neovim from scratch" by Mr Jakob
- Part 1: lazy.nvim, kanagawa.nvim, oil.nvim, Statusline & Essential Settings
- Part 2: Supercharged Code Navigation via Treesitter, Textobjects & which-key
- Part 3: Insanely fast Neovim Navigation with fzf ❤️ lua
- Part 4: Code Intelligence in Neovim - The LSP Setup that grants you IDE Powers
- Part 5: Perfect Code Formatting with conform.nvim
- Part 6: blink-cmp: Lightning-Fast Autocompletion Built in Rust
-
How I Setup Neovim To Make It AMAZING in 2024: The Ultimate Guide by Josean Martinez
-
How To Setup Linting And Formatting In Neovim To Replace null-ls by Josean Martinez
-
The Only Video You Need to Get Started with Neovim by TJ DeVries
-
telescope.nvim introduction by TJ DeVries
-
Code Formatting made easy by TJ DeVries
-
0 to LSP : Neovim RC From Scratch by ThePrimeagen