Things Vim is fecking great at #912983772: Joining text


I love Vim. Have I said that before? I want to share another command that I use every single day. It’s extremely simple and, as is often the case with simple tools, very powerful.

J for Joins.

What it does

Simply, if I have two or more lines of text I can use J to join them together on one line. Let’s take some example text (note cursor is on the H):

Here is some
example text, spread over a
few lines.

I can join this in a few ways:
1. Execute 3J (Join 3 lines)
2. My preferred way, especially if dealing with a lot of lines, is to visually select the text first and then executing V2jJ (Visually select this line [V], go down 2 lines [2j] and Join [J]). V}J would also work a treat in this case and is essentially one less key press.
3. Do one of the above with gJ e.g. 3gJ. This actually changes the command to remove spaces which is useful occasionally. The example text would end up looking like this: Here is someexample text, spread over afew lines. That probably isn’t what you want in this case but I’m sure it’s obvious how automatically removing spaces might be useful.

Why it’s good

Like all great Vim commands, it’s mnemonic so you’ll not need to look it up once you’ve used it once or twice. Its real strength is apparent when refactoring. Combined with a good IDE (IntelliJ + IdeaVIM) and the other staple Vim commands, you’ll find that manipulating text becomes a pleasure rather than a chore.

Leave a Reply

Your email address will not be published. Required fields are marked *