Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Find and Replace in Vim / Vi - DesignLinux

designlinux 0 Comments

This article describes how to find and replace text in Vim / Vi.

Vim is the most popular command-line text editor. It comes preinstalled on macOS and most Linux distributions. Finding and replacing text in Vim is quick and easy.

Basic Find and Replace #

In Vim, you can find and replace text using the :substitute (:s) command.

To run commands in Vim, you must be in normal mode, the default mode when starting the editor. To go back to normal mode from any other mode, just press the ‘Esc’ key.

The general form of the substitute command is as follows:

:[range]s/{pattern}/{string}/[flags] [count]

The command searches each line in [range] for a {pattern}, and replaces it with a {string}. [count] is a positive integer that multiplies the command.

If no [range] and [count] are given, only the pattern found in the current line is replaced. The current line is the line where the cursor is placed.

For example, to search for the first occurrence of the string ‘foo’ in the current line and replace it with ‘bar’, you would use:

:s/foo/bar/

To replace all occurrences of the search pattern in the current line, add the g flag:

:s/foo/bar/g

If you want to search and replace the pattern in the entire file, use the percentage character % as a range. This character indicates a range from the first to the last line of the file:

:%s/foo/bar/g

If the {string} part is omitted, it is considered as an empty string, and the matched pattern is deleted. The following command deletes all instances of the string ‘foo’ in the current line:

:s/foo//g

Instead of the slash character (/), you can use any other non-alphanumeric single-byte character except as a delimiter. This option is useful when you have the ‘/’ character in the search pattern or the replacement string.

:s|foo|bar|

To confirm each substitution, use the c flag:

:s/foo/bar/gc
replace with bar (y/n/a/q/l/^E/^Y)?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y, and to scroll up, use CTRL+E.

You can also use regular expressions as a search pattern. The command bellow replaces all lines starting with ‘foo’ with ‘Vim is the best’:

:%s/^foo.*/Vim is the best/gc

The ^ (caret) symbol matches the beginning of a line and .* matches any number of any characters.

Case Sensitivity #

By default, the search operation is case sensitive; searching for “FOO” will not match “Foo”.

To ignore case for the search pattern, use the i flag:

:s/Foo/bar/gi

Another way to force ignore case is to append \c after the search pattern. For example, /Linux\c performs ignore case search.

If you changed the default case setting and you want to perform case sensitive search, use the I flag:

:s/foo/bar/gi

Uppercase \C after the pattern also forces case match search.

Search Range #

When no range is specified the substitute command operates only in the current line.

The range can be either one line or a range between two lines. The line specifiers are separated with the , or ; characters. The range can be specified using the absolute line number or special symbols.

For example, to substitute all occurrences of ‘foo’ to ‘bar’ in all lines starting from line 3 to line 10 you would use:

:3,10s/foo/bar/g

The range is inclusive, which means that the first and last lines are included in the range.

The dot . character indicates the current line and $ – the dollar sign the last line. To substitute ‘foo’ in all lines starting from the current line to the last one:

:.,$s/foo/bar/

The line specifier can also be set using the ‘+’ or ‘-’ symbol,followed by a number that is added or subtracted from the preceding line number. If the number after the symbol is omitted, it defaults to 1.

For example to substitute each ‘foo’ with ‘bar’ starting from the current line and the four next lines, type:

:.,+4s/foo/bar/g

Substituting Whole Word #

The substitute command looks for the pattern as a string, not a whole word. If, for example, you were searching for “gnu”, the search find matches where “gnu” is embedded in larger words, such as “cygnus” or “magnum”.

To search for a whole word, type \< to mark the beginning of a word, enter the search pattern, type \> to mark the end of a word:

For example, to search for the word “foo” you would use \<foo\>:

:s/\<foo\>/bar/

Substitute History #

Vim keeps track of all the commands you run in the current session. To browse the history for previous substitute commands, enter :s and use the arrow up/down keys to find a previous substitute operation. To run the command, simply press Enter. You can also edit the command before performing the operation.

Examples #

Comment lines (add # before the line) from 5 to 20:

:5,20s/^/#/

Uncomment lines from 5 to 20, revert the previous changes:

:5,20s/^#//

Replace all instances of ‘apple’, ‘orange’, and ‘mango’ with ‘fruit’:

:%s/apple\|orange\|mango/fruit/g

Remove trailing whitespace at the end of each line:

:%s/\s\+$//e

Conclusion #

Searching and replacing is a powerful feature of Vim, which allows you to make changes to your text quickly.

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

terminal vim

Related

Tags: terminal, vim

How to Set Up High Availability for Resource Manager – Part 6

Prev Post

How to Install Different PHP (5.6, 7.x and 8.0) Versions in Ubuntu

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
  • 0
  • 611
  • 1,055,383

DesignLinux.com © All rights reserved

Go to mobile version