vim: join lines without producing a space
• 265 words • 2 min • updated
Here’s a common issue I have when blogging, when inserting a markdown link with
vim. The edit progression is as follows, wherein | represents the cursor:
First, blank:
|Then, type out the hyperlink label and prepare to insert the link:
[example](|
)Now go to another buffer (window) and copy the link (with y + motion).
Go back to the original buffer and paste it with p:
[example](
|https://example.com/
)Press kJ:
[example](| https://example.com/
)Press J again:
[example](| https://example.com/)J is for “join lines” (:help J).
Do you see the issue above? The first J inserts a space, the second one does
not. This inconsistency is frustrating.
This vi stack exchange question seeks to address it:
The standard J command for joining lines replaces the newline character(s) with a space. It’s useful when editing ’literature’ but can be troublesome if I, say, edit a hex dump by hand if I forget to remove the superfluous space.
Is there a quick & easy method to join two lines without producing a space between them?
The standard recommendation is to use gJ to do so. From :help gJ:
Join [count] lines, with a minimum of two lines. Don’t insert or remove any spaces.
But it’s harder to memorize than simply J. Plus, it’s an extra keystroke. I’d
rather unconditionally use J.
It turns out it’s possible to do so with a simple config setting, added to
~/.vimrc:
nnoremap J gJWhen repeating the original link insertion sequence, it yields the following result, as desired (no spaces anywhere!):
[example](|https://example.com/)