Vim, seach and replace. With regular expressions.
Vim, ohh, Vim. Text editor that is used from terminal, an improved version of the Vi text editor, present on all UNIX systems. It has a high learning curve, but when have a good control, it can increase productivity. In this post, I will explain some basic operations for "Find and Replace".
Vim has different working modes, the following are commented: "command", "insert" and “command-line”:
- Command mode:
Default mode or otherwise known as normal. You can always return to this mode by pressing two times the "Escape" key.
- Insert mode:
Used to edit the text. Accessed from the command mode by pressing the "i" key.
- Command-line mode:
Accessed from the command mode by pressing the ":" key and then the command. Mode where "Search and replace" actions will be executed:
- Find the occurrences of the string "foo" and replace them with "bar", throughout the text:
Find the occurrences of the string "foo" and replace them with "bar", only in the current line:
Regular expressions can also be used.
For example, by solving the following problem:
Assuming the following text is available:
Want to transform to:
The steps to do this, relatively quickly are:
Solved the problem.
You can also see the solution of the problem in video:
Surely exists a regular expression that solve this with a single order,
but it's a basic introduction and solved in a simple way.
If you have the regular expression that solves it,
I invite you to share it on our Twitter account @tocappcom