How to Delete all lines in VI / VIM text editor at once

In this tutorial, we learn the shortcuts or commands to delete all lines from a  file in the VIM or VI text editor on Linux instead of pressing the Del key again and again.

VIM or VI is a popular text editor available in almost all Linux operating systems by default. We can use it to edit files using the command line interface. This text editor is light in weight, however for beginners it is quite complicated. But once you have familiarized yourself with the different operating modes, you can no longer do without the helpful services of the lean, but almost arbitrarily expandable editor.

VIM is a further development of the text editor Vi, hence the name as an abbreviation for “VIMproved”. There are editors that are initially much easier to use, e.g. Pico or Nano. However, VIM has some advantages:

  • On any POSIX-compliant system, at least the ancestor of the VIM, the Vi, is present as an editor (with Vi having much fewer functions)
  • Navigable without cursor keys (can help with keyboard problems in remote sessions)
  • Super-fast usability
  • Browsing through directories
  • Almost infinitely expandable and configurable (settings, macros, mappings, plugins …)

How to delete all file lines in VI  or VIM editor?

  • Open the command terminal on your Linux system whether it is Ubuntu, Debian, or Redhat.
  • Now go to the file from where you want to remove all line and open it using VIM or VI editor.
  • For example, Let’s say we have a file name demo then the command to open it, vi demo or vim demo
  • After opening the file in the text editor, press the ESC key on your keyboard.
  • Now, press gg. Pressing the G key two times will take you to the first line of the file.
  • Once you are at the first line press dG and hit the Enter key.
  • This will automatically delete all the lines from the first to the last one in your VIM or VI editor.

delete all file lines in VI or VIM editor

 

Deleting a Single Line

If you want to delete a single line  from a file in VIM or VI instead of all lines then here are the steps to follow:

  • Open your File in VIM or VI editor
  • To get the editor mode, press the Esc key from your keyboard
  • After that using the arrow keys go to the line that you want to delete.
  • Press the d key two times means dd and hit the Enter key.
  • This will delete the current line.
  • Pressing the same dd you can delete multiple lines.

Deleting a Single Line in Linux text editor

 

Deleting a select number of multiple lines

A selected number of lines means you want to delete the next 8 lines from your current cursor position. The number can be 3 or 2 or whatever you want. In this way, you can delete multiple lines on the VIM editor but only if you want.

For example, you want to delete 8 lines simultaneously:

  • Open the file in this text editor
  • Press the Esc to get the Editor mode.
  • After that move the cursor to the line from where you want to delete the next 8 lines including it.
  • Then type or press the 8 and d keys two times means- you have to press 8dd
  • After that hit the Enter key. This will delete the eight lines altogether.

Delete multiple lines in VIM

 

Delete a specific range of lines

Above we have learned how to delete multiple numbers of lines at once, now let’s suppose you want to delete a few lines between a specific range of lines. For example, let’s you want to delete all the starting from 4 and 8. This means you want to tell the VIM editor to leave the first, second, and third lines from the start of the file and the lines available after 8th one.

  • For this the syntax is :[start],[end]d
  • Hence, open your file in the VIM editor
  • Press the Esc button on the keyboard
  • Type colon and range of the lines along with d. For example to delete lines 4 to 8 only the command will be :4,8d

 

Deleting  specific pattern

  • Even we can delete only lines that contain a specific type of pattern or text.
  • For example, you want to delete all lines containing a text – port
  • The syntax for this will be :g/<text>/d
  • For example- delete all lines containing port even the lines containing port in some other word such as portable:
  • Press Esc and then type the below command in VIM.
:g/port/d

Alternatively, if you want to delete all lines except the ones containing a letter or string port. Then just add the exclamatory sign.

:g!/port/d

 

Command to empty any file in Linux

Those who don’t want to use VIM or any other file editor such as Nano to empty the file content in Linux can use the /dev/null command.

cat /dev/null > file-name

For example, if your file name is demo then to remove all lines from it without opening it in any editor, the command will be:

cat /dev/null > demo

In this way using the above two methods we can empty a file on a Linux system either using VIM or without opening it. Command to empty any file in Linux

 

Other Articles:

How to Create a file in Ubuntu Linux using command & GUI
Linux: Delete files & folders using the command line terminal
List Open or closed Ports in UFW Firewall on Ubuntu

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.