howtouselinux

2 ways to add new line with Vi

Table of Contents

If you’re a programmer, you’ve probably heard of Vi and Vim. But did you know that they’re more than just text editors? In fact, they’re two of the most powerful tools in your programming arsenal.

Vi and Vim are both modal editors, which means that they can be used in multiple ways depending on the mode you’re in. In normal mode, for example, you can use Vi or Vim to edit text files. But in command mode, you can use them to run commands or macros.

This makes Vi and Vim incredibly versatile tools, which is why so many programmers rely on them. In this post, we’ll take a look at 2 ways to add a new line with VI/Vim.

Procedure to add a new line with Vi

  • Open the terminal.
  • Navigate to the directory where the file is located.
  • Type vi filename and press Enter. Replace filename with the actual name of the file you want to edit.
  • Move the cursor to the right line
  • Press the “o” key. It will insert a new line below the current line. or Press the “O” key. It will insert a new line above the current line.
  • Type the new line of text you want to add.
  • Press Esc to exit insert mode.
  • Type :wq and press Enter to save the file and exit Vi.

 

add a new line with Vi

To add a new line below the current line in Vi, press the “o” key. This will insert a new line below the current line.

To add a new line above the current line in Vi, press the “O” key. This will insert a new line above the current line and put you in insert mode at that position.

After adding some text,  you can switch to command mode by pressing the Esc key.  Press : (colon) to open the prompt bar in the bottom left corner of the window. Type wq after the colon and hit Enter. This will save the changes and exit.

These are just two of the many ways you can use Vi and Vim to edit text files.

What is the difference between Vi and Vim?

Vi is a text editor that was originally created for the Unix operating system. Vim is an enhanced version of Vi that includes additional features.

How to save a file in Vi Vim?

To save a file in Vi or Vim, press the “:w” key. This will write (or save) the file.

How to quit Vi Vim?

To quit Vi or Vim, press the “:q” key. This will exit the editor. If you have unsaved changes, you will be prompted to save them.

Tips to use Vi Vim

1. Learn the different modes.

Vi and Vim can be used in multiple ways, depending on the mode you’re in. In normal mode, for example, you can use them to edit text files. But in command mode, you can use them to run commands or macros. This makes them incredibly versatile tools.

2. Practice using Vi and Vim in different situations.

The more you practice using Vi and Vim, the better you’ll become at using them. Try using them to edit code files, text documents, and more.

3. Use plugins to enhance your Vi or Vim experience.

There are a number of plugins available for Vi and Vim that can enhance your editing experience. Check out the Vim plugin repository to find plugins that interest you.

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.