Skip to main content

In many terminal text editors, you use find command as reference in Terminal commands - find.  How about find and replace.  This action depends on the specific text editor you're using in the terminal.  Here are a few common terminal text editors and how you can find and replace strings within them:

Vim

To find: Press / followed by the search term and then Enter.

/

To replace: You can use the substitute command. For example, to replace "old" with "new" globally in the file, you can use :%s/old/new/g. This replaces all occurrences of "old" with "new".

:%s/old/new/g

% This specifies that the search and replace operation will be performed across the entire file. % is a shorthand for specifying the entire file range.
s This is the substitute command in Vim. It tells Vim to search for a pattern and replace it with another pattern.
/old/ This is the search pattern. In this example, old is the string you want to search for and replace.
new This is the replacement string. It specifies what you want to replace the search pattern with.
/g This is a flag that tells Vim to perform a global search and replace, meaning it will replace all occurrences of the search pattern in the file. If you omit the g, Vim will only replace the first occurrence on each line.

 

Nano

Nano doesn't have a built-in find and replace functionality like Vim. However, you can still do a simple search by pressing Ctrl + W and then typing the search term. Nano will highlight the first occurrence. To find the next occurrence, press Ctrl + W again.
Nano also supports a basic search and replace function. Press Ctrl + \, then enter the search term, followed by the replacement term. You can choose to replace individual occurrences or all occurrences.

Emacs

To find: Press Ctrl + S and then type the search term. Press Ctrl + S again to find the next occurrence.
To replace: Press Alt + Shift + 5 (Alt + $ on some keyboards), then type the search term and the replacement term when prompted.
These are just a few examples. Depending on your specific terminal text editor, the key bindings and commands may vary. If you're using a different text editor, please specify, and I can provide instructions for that editor.

Related articles