Vim: Search and Replace, Insert new line
Pete
Vim has some really useful commands, especially search and replace ones. Unfortunately I'm always forgetting the finer details of how to use them so here's an example:
CODE:
-
:%s/$/\rthis is a new line
To break down whats happening above:
- : switches Vim into command mode
- % means we're going to apply this command to every line in our file
- s means we're using the substitute command. The format of the command is s/search/replacement, 'search' can be a regular expression
- $ tells vim to search for the end of the line
- \rthis is a new line tells vim to replace with a new line character, and to put the text "this is a new line" on that line.
NOTE: \r is used as the new line character when vim is using DOS file format, to find out which file format you are using type :set ff? You can find out more about Vim file formats here.
#Pete Graham
Posted in regex, regular expressions, vim |
7 Comments »