As a software developer who prefers Neovim for text editing, my work often includes editing text that is stored externally, for example, in a web-based low-code tool. Here I present two little tricks that have been game-changers for me in these situations. Although I’m discussing Neovim here, both tips also apply to Vim purists. The following video displays the complete workflow with the following steps:
The command pbpaste | jq
before and after editing displays the clipboard contents to demonstrate the changes made to the clipboard data.
I’ll discuss the need for editing text without saving later in this post. But now, let’s start with the first tip!
When a file is opened with Neovim, the chances are Neovim can determine the file type based on the filename extension or the shebang line.
However, when spinning up nvim
without providing a filename, Neovim doesn’t have any of that information available, resulting in filetype
not being set.
And when that is the case, there’s no syntax highlighting or other language features that would otherwise help understand code structure and identify errors at a glance.
However, it’s easy to set the file type manually with Vim command :set filetype=foobar
, where foobar
should be replaced with the actual file type, such as json
.
Here’s a short video displaying the whole process of opening nvim
from the shell, pasting some JSON data, and setting the file type:
For scripts starting with a shebang line such as !#/usr/bin/env python3
, you can even use :filetype detect
to let Neovim automatically determine the file type.
For other snippets, it’s usually best to define the file type manually.
Here are some common filetype
values for quick reference:
bash
css
csv
html
javascript
javascriptreact
json
jsonc
lua
python
sql
typescript
zsh
To list all of the supported file types, type :setfiletype
, space, and Ctrl + d, as suggested by EvergreenTree.
In the first part, we pasted JSON data from the system clipboard into Neovim for editing.
Suppose we make some edits and want to copy the modified JSON back into the system clipboard.
You could go to the first line of the file with gg, declare the buffer +
(system clipboard) for the next action with "+, and finally copy everything from the current location to the end of the file with yG.
I find it too complicated for regular use, and the cursor location will be lost during the copy.
Thus, I’ve added this line to my vimrc
file:
nnoremap <leader>ya msggVG"+yg`s:delmark s<CR>:echo 'Copied buffer content to +'<CR>
Now I can yank all text inside of a buffer with a simple <leader>ya, just like this:
I have set my <leader> key to be , with the following line in my vimrc
:
let mapleader = ","
If you are re-using the same Neovim process while working with external text, this simple Vim command can be used to replace the current buffer content:
ggcG
Then, paste your new content as you usually would.
Why do I believe these tips might be valuable for someone?
As a software developer, my job consists of designing and building software with a wide variety of tools. Sometimes, these tools are available as software libraries and command-line tools, which allows me to use the tools I know and love. Other times, the tools require writing code in text boxes on some web interface, which is fine for small pieces of code. However, more often than not, the code in the box grows in size. When I need to get back to such not-so-small-anymore piece of code to make a change, I often find it easier to copy-paste it into Neovim, make the changes, copy it back to the clipboard, and paste it back to the browser, just as I’ve shown in this post’s example.
Tools like low-code platforms aim to make building software more straightforward, not to provide an excellent text editing experience. Luckily, we have other tools that are specialized just for that purpose! For me, the most natural text editing tool is Neovim. Even if you prefer something else, I hope this blog post encourages you to use the tools that best suit you, instead of struggling with tiny text boxes in a web app.
The point of this post is not to bash low-code platforms. I have seen them accelerate and hinder development, often during the same project.
That being said, I do see the value they can bring to the table, but the benefits and caveats of low-code are a topic for another time.
Even when working with “pro-code” tools, using Neovim without a file makes sense in some scenarios.
For instance, I’ve found myself working on an SQL query, modifying it in Neovim, pressing ,ya, and pasting the query into an open psql
terminal on a remote server, without ever saving the file locally.
As an alternative solution, a Firefox add-on called Firenvim replaces <textarea>
elements on all web pages with Neovim instances.
While this seems fantastic, I have not yet dared to test it.
Although I love Vim keybindings for editing text, I don’t want to depend on Vim in order to edit any text at all.
That being said, I’ve accidentally closed countless tabs with Ctrl + w during text input while trying to delete the previous word.
Maybe I’ll give Firenvim a chance one day, but for now, I will save myself from the setup hassle.
I can always open Neovim in a terminal instead of embedding it into the browser.
Although simple and perhaps even obvious, these tips have made editing of externally stored texts more enjoyable. I find yanking the entire buffer to the system clipboard with a simple keybind especially powerful, since it makes transporting text between different contexts a lot faster, enabling a development cycle where Neovim can be started whenever the text-editing needs grow bigger than fixing a single value within a few lines of code. It’s best to use tools that you work most efficiently with!
My Neovim configuration is available among other configuration files on Github: samporapeli/dotfiles.
vimrc
(at the time of writing this blog post): .vim/vimrc @ c55b6c6The main
branch always contains the most up-to-date version.
Do you similarly use Neovim for clipboard-based editing, or do you have some tips to share? Feel free to contact me; details can be found below this post!
I'll announce new posts in the following channels:
See my blog's front page to read my other posts.
You can reach me on Mastodon: @sampo@hachyderm.io. I'd love to hear from you!