Samuel Ireson

Native Neovim Tips

Mon Sep 16 2024
4 min read

We'll look at some tips related to usage of the Neovim text editor.

Introduction

Neovim is a terminal based text editor which can be extended via plugins and configuration to create a customised development environment. It’s my editor of choice, and I like it specifically because it’s terminal based, minimal and highly extendable. Coming from VSCode, Neovim is really a breath of fresh air β€” you have complete control over how the editor looks and feels, how the editor interacts with filetypes and environments, and how cool you feel using it.

There’s a bit of a learning curve with using the vi keybindings, but if you fully commit, you will be trying to vim-ify all aspects of your interaction with technology before you know it. If you told me a year ago that I would be trying to configure a terminal based email client, just so I could use vi keybindings, I would have been shocked at how geeky I had become, but it’s a rite of passage. There is no escape.

The internet is overflowing with vim tips and tricks, especially for navigating in the editor, but I hope to outline some lesser known tricks, which I use fairly frequently.

Executing commands :!

One of the major benefits of Neovim for me, as I previously mentioned, is the fact that it is terminal based. This makes interactions between the editor and the OS feel almost seemless. This is heightened by the fact that Neovim has a builtin way to execute single commands and bash scripts from inside the editor.

In normal mode, typing :! will place you in a subset of command mode, where the proceeding commands are interpreted as bash commands, and sent to the terminal. In the strictest sense, the ! character allows you to β€˜filter’ text through an external program. A good example is the following.

Suppose I have a list of fruits each on a separate line,

Orange
Mango
Pineapple
Strawberry
Apple
Blueberry
Grapes
Watermelon
Lime
Raspberry
Blackberry
Cranberry
Banana
Cantaloupe
Dragonfruit
Lychee
Passionfruit
Persimmon
Coconut

and I want to sort these alphabetically. In this case, all I need to do is filter this set of lines through the unix sort command. If I make a visual selection of the lines which I want to sort, then type !sort, and press enter, I will get the following output.

Apple
Banana
Blackberry
Blueberry
Cantaloupe
Coconut
Cranberry
Dragonfruit
Grapes
Lime
Lychee
Mango
Orange
Passionfruit
Persimmon
Pineapple
Raspberry
Strawberry
Watermelon

All nicely sorted!

Repeating actions across buffers :bufdo

Sometimes there are actions which you want to perform across a range of buffers, all at once. Use :bufdo. This will simply take the proceeding command and run it on all buffers. For example, suppose we have the following directory structure,

└── articles
 Β Β  β”œβ”€β”€ conditional-rendering-tex.md
 Β Β  β”œβ”€β”€ indent.log
 Β Β  β”œβ”€β”€ nvim-tips.md
 Β Β  β”œβ”€β”€ some-updates.md
 Β Β  β”œβ”€β”€ tikz-1.md
 Β Β  β”œβ”€β”€ tikz-2.md
 Β Β  β”œβ”€β”€ tikz-3.md
 Β Β  β”œβ”€β”€ tikz-4.md
 Β Β  β”œβ”€β”€ tikz-5.md
 Β Β  β”œβ”€β”€ tikz-6.md
 Β Β  β”œβ”€β”€ using-tailwind.md
 Β Β  └── welcome.md

If we want to replace all occurrences of this with that in every file in articles, then we can open every article into a separate neovim buffer with,

nvim articles/*

Then use the command,

:bufdo %s/this/that/g

Sometimes, I like to do the substitution on the first buffer,

:%s/this/that/g

write and close that buffer (using | to chain commands),

:w | :bd

then access the previous substitution with :<up-arrow> and prepend with bufdo.

Writing output to buffer :r!

Another useful way to interact with the terminal within neovim is to write the output of a shell command into the buffer. I actually used this in the writing of this article! In particular, if I wanted to output the format of some directory, then I could type,

:r !tree 'path-to-file'

which would output

/home/sam/Documents/blog/src/content/articles/
β”œβ”€β”€ conditional-rendering-tex.md
β”œβ”€β”€ indent.log
β”œβ”€β”€ nvim-tips.md
β”œβ”€β”€ some-updates.md
β”œβ”€β”€ tikz-1.md
β”œβ”€β”€ tikz-2.md
β”œβ”€β”€ tikz-3.md
β”œβ”€β”€ tikz-4.md
β”œβ”€β”€ tikz-5.md
β”œβ”€β”€ tikz-6.md
β”œβ”€β”€ using-tailwind.md
└── welcome.md

1 directory, 12 files

There are a lot of other uses of this, for example using cat to see the data file, or using awk 'NR<=10' to see the first 10 lines of a file. Or, another, pretty pointless usage, is writing the output from figlet or some other ASCII terminal art tool.

:r !figlet "HELLO\!"
 _   _ _____ _     _     ___  _ 
| | | | ____| |   | |   / _ \| |
| |_| |  _| | |   | |  | | | | |
|  _  | |___| |___| |__| |_| |_|
|_| |_|_____|_____|_____\___/(_)
                                

Conclusion

If you daily drive vim/neovim, you will learn a new editor trick every day.

Related posts