Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Delete Lines in Vim / Vi - DesignLinux

designlinux 0 Comments

Vim or its precursor Vi comes preinstalled on most Linux distributions and macOS. Knowing the basics of Vim is important if you are a system administrator or just a regular Linux user.

Quite often, when working with text files, you’ll need to delete one or more lines.

This article shows how to delete lines in Vim/Vi.

Deleting a Line #

The command to delete a line in Vim is dd.

Below are step-by-step instructions to delete a line:

  1. Press the Esc key to go to normal mode.
  2. Place the cursor on the line you want to delete.
  3. Type dd and hit Enter to remove the line.

Pressing dd multiple times will delete multiple lines.

Deleting Multiple Lines #

To delete multiple lines at once, prepend the dd command with the number of lines to be deleted. For example, to delete five lines you would do the following:

  1. Press the Esc key to go to normal mode.
  2. Place the cursor on the first line you want to delete.
  3. Type 5dd and hit Enter to delete the next five lines.

Delete a range of lines #

The syntax for deleting a range of lines is as follows:

:[start],[end]d

For example, to delete lines starting from 3 to 5 you would do the following:

  1. Press the Esc key to go to normal mode.
  2. Type :3,5d and hit Enter to delete the lines.

You can also use the following characters to specify the range:

  • . (dot) – The current line.
  • $ – The last line.
  • % – All lines.

Here are a few examples:

  • :.,$d – From the current line to the end of the file.
  • :.,1d – From the current line to the beginning of the file.
  • 10,$d – From the 10th line to the end of the file.

Delete All Lines #

To delete all line you can use either the % symbol that represents all lines or the 1,$ range:

  1. Press the Esc key to go to normal mode.
  2. Type %d and hit Enter to delete all the lines.

Deleting Lines Containing a Pattern #

The syntax for deleting multiple lines based on a specific pattern is as follows:

:g/<pattern>/d

The global command (g) tells the delete command (d) to delete all lines containing the <pattern>.

To match the lines not matching the pattern, add an exclamation mark (!) before the pattern:

:g!/<pattern>/d

The pattern can be a literal match or regular expression . Below are some examples:

  • :g/foo/d – Delete all lines containing the string “foo”. It also removes line where “foo” is embedded in larger words, such as “football”.
  • :g!/foo/d – Delete all lines not containing the string “foo”.
  • :g/^#/d – Remove all comments from a Bash script. The pattern ^# means each line beginning with #.
  • :g/^$/d – Remove all blank lines. The pattern ^$ matches all empty lines.
  • :g/^\s*$/d – Remove all blank lines. Unlike the previous command, this also removes the blank lines that have zero or more whitespace characters (\s*).

Conclusion #

We have shown you how to delete lines in Vim.

If you are new to Vim, visit the Open Vim site where you can practice Vim with an interactive tutorial.

Feel free to leave a comment if you have any questions.

terminal vim

Related

Tags: terminal, vim

How to Install TensorFlow on Ubuntu 20.04

Prev Post

How to Install Elasticsearch on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 496
  • 612,675

DesignLinux.com © All rights reserved

Go to mobile version