We'll look at some tips related to usage of the Neovim text editor.
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.
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!
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
.
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\!"
_ _ _____ _ _ ___ _
| | | | ____| | | | / _ \| |
| |_| | _| | | | | | | | | |
| _ | |___| |___| |__| |_| |_|
|_| |_|_____|_____|_____\___/(_)
If you daily drive vim/neovim, you will learn a new editor trick every day.