Open In App

Joining Two Lines in VI Editor

Last Updated : 10 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Vi is a powerful command-line text editor used by programmers, system administrators, and writers alike. They offer a vast array of features, including the ability to manipulate text efficiently. One common task you might encounter when working with Vi is joining two lines together. This article will guide you through the process of joining lines in Vi.

Joining Lines in Vi

Vi provides a straightforward method for joining two lines.

Follow these steps:

  1. Open Your File: Launch Vi and open the file you want to edit.
  2. Position the cursor: Move your cursor to the line that you want to join with the line above or below it.
  3. Join the Lines: Press ‘ Shift + J ‘ (Uppercase J). This key combination merges the current line with the line below it. You can repeat this step to join multiple lines if needed.

Implementation

Example 1:

Create a text file with a name GFG.txt using the following command.

vi GFG.txt


Switch to insert mode by pressing i on keyboard. Once you’re in insert mode, you can start typing your text as you would type in any other text editor. Insert some text with multiple lines into GFG.txt file as shown below.

gfg

Press Esc button to switch to normal mode from insert mode.

To join these two lines, place the cursor on the line that you want to join and press ‘Shift + J‘ (Uppercase J). Now the two lines are merged into a single line as shown below.

gfgafter

Make sure to save your changes by pressing Esc, typing :w, and hitting Enter. To exit Vi, use :q and Enter, or :wq to save and quit in one command.

Example 2:

Now let us try to join multiple lines.

Create a text file with a name Geeks.txt using the command ‘vi Geeks.txt‘.

Switch to insert mode by pressing i on keyboard. Write some text as shown below.

multiline1

Press ESC button to return to normal mode. Place the cursor on the line, which you wanted to join as shown below.

Screenshot-2023-09-29-151058

Here we are joining/appending line2 with line1. Press ‘ Shift+J ‘ to join line1 and line2.

Screenshot-2023-09-29-151132

Keep pressing ‘Shift+J‘ to join multiple lines.

Screenshot-2023-09-29-151208

Screenshot-2023-09-29-152846

Hence we joined multiple lines. To save the changes and quit, press :wq.

Conclusion

In this article we discussed how to join lines in Vi which is a simple yet essential task that can enhance your text editing and formatting abilities. Whether you’re working on code, documents, or any other text-based content, knowing how to join lines efficiently can save you time and help maintain the clarity and readability of your text.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads