Vim Join lines
Vim join lines allows you to join the words, content, code, sentences, or any type of text distributed in multiple lines based on a common attribute. For example, you want to combine all lines that contain the word ‘line’. See the example below, it has five lines, and three out of five lines contain the word ‘line’.
The criteria to join the lines is, combine those lines which have the word ‘line’ in them. The vim join lines feature allows you to combine all the segments of a particular content using the common attribute and create a single linear element representing a meaningful text according to the required criteria.
Features of Vim Join Lines
The features of vim join lines are as follow:
- Vim join line can join lines with a string separator.
- Vim join lines can join the lines without trimming the trailing and leading whitespaces.
- Vim join lines can join the lines in reverse order.
- Vim join lines not join joins the line but also keep those joined lines.
- Vim join lines can join the line with the combination of any option given above.
Syntax of Vim Join Lines
The Join line is more capable than the built-in :join command as it has more powerful join line features. The built-in join plugin provides only one command, which is :join. However, with join lines features, you can have multiple options. Let us start with :J or shift+j, which is available for :join.
The format for the vim join lines is as follow:
Note: all the arguments should be separated with a space.
All the arguments are given in the :join command syntax are optional, which means if nothing is provided, the default value will be used. However, the lineRange refers to the current line where your cursor is, following the vim range definition. lineSeparator is the separator value that is used to separate the line after joining them together; it can be any string and should be quoted by ‘’ or “” , depending on whether the escaped special character is required or not. lineCount is the count of the number of lines that need to be joined; it could be a negative number, and that way, the join will join the line backward. There are two types of flags for vim join lines: ‘r’ and ‘k’, ‘r’ is used when the lines need to be joined in reverse order, and ‘k’ is used to keep the joined line after joining. The ‘r’ and ‘k’ flags can be used together with ‘rk’ or ‘kr’, though the order does not matter; both are valid.
How to Join Lines in Vim?
Combining or joining the two or multiple lines in vim is very simple. The following are the required steps needed to join lines in the vim text editor.
- Get to the vim command line mode.
- Go to the end of the line, after which you want to join the next line
- Place the cursor to the end of the line and press the shift+j key to append the next line to it
The upper-case j of Shift+j is used to join the lines in vim editor. When you press shift+j, it will append the following line at the end of the line that your cursor is currently at. For example, you have the following data with you:
And you want to join these nine lines into three lines, so first, you put your cursor at the end of the first line (next to a) and press shift+j. This will join a and b like this:
By repeating the same process at each line, you will get the desired result which is given below:
VIM Join Line Without Space
Sometimes while editing some text, there is a possibility that you end up with a series of multiple short lines, which are quite hard to scan. So, you want them to be merged without spaces. Here is how you can join lines in vim without spaces:
- Go to the vim command-line mode.
- Place your cursor on the line where you want your next line to be merged.
- Enter gJ: to join the lines without spaces.
Vim Join Lines with Count
Suppose you have 100 lines, and you want to join all of them in just one line; what will you do? Go to each line and press Ctrl+J at each line? That would be a tedious task, right?
Here is a simple solution, use the numeric count argument with J and join as many lines as you want with just one command. The numeric count argument joins that number of the consecutive line. So, for example, :50J will join the 50 consecutive lines in just one line. Similarly :1000J will join the 1000 consecutive lines together.
The vim command line is case-sensitive, so make sure you type exactly what you intended. For example, you are mistakenly typing J instead of j. You may have unintentionally hit the CAPS LOCK key, and now all commands are interpreted differently. Ensure that the CAPS LOCK is turned off if you intend to use it, and press ESC to ensure that you are in the command line mode.
This article was about the vim join lines feature. Here we have learned the syntax, usage, and features of vim join lines. Moreover, we have looked at the step-by-step process of using the vim join lines.