yes i love technology

Vim: Search and Replace, Insert new line

May 3rd, 2007 by 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:
  1. :%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 |

9 Responses

  1. Mzyzek Says:

    Intresting, now i know how to find new line in vim…

  2. Ramnath Says:

    Thanks I used to format my file.Very useful

  3. eregilkibly Says:

    E’ possibile acquistare il Cialis online, in modo sicuro e anonimo, e risparmiare sul suo costo commerciale. Generic Cialis (Tadalafil) e’ un trattamento per gli uomini con disfunzione erettile (Impotence). Generic Cialis aiuta di aumentare circolizazione del In the old days, Viagra was the dominating drug in curing impotence. But nowadays there are new Viagra alternatives appearing on the market. comprare viagra farmacia

  4. eregilkibly Says:

    Potenzmittel Natur. naturliche und rezeptfreie Potenzmittel Potenzmittel aus der Natu
    r sind im Trend. Bei uns konnen Sie eines der wirksamsten Erektion, Impotenz, Erguss: Fragen und Antworten. Endlich hab ich einen Traummann gefunden, der mich liebt - doch er ist impotent! Fur Arzte Fachinfo zur Erektilen Dysfunktion Umfangreiche Fachinfos zu Ursachen und Behandlungsmoglichkeiten. Fach-Login. Fur Patienten online levitra kaufen

  5. Simplicity » Blog Archive » Menyisipkan Baris Baru Pada Vim Editor Says:

    […] http://tech.petegraham.co.uk/2007/05/03/vim-search-and-replace-insert-new-line/ […]

  6. links for 2008-06-30 « The Adventures of Geekgirl Says:

    […] Optimus Pete » Blog Archive » Vim: Search and Replace, Insert new line (tags: code tips vim text edit replace regex regularexpressions) […]

  7. Chris Says:

    Thank goodness for descriptive blog post titles. Sometimes it’s really hard to know what to search for :D

  8. Potenzmittel AuLi Says:

    natürliche Potenzmittel sind definitv die gesündere Alternative zu synthetisch hergestellten Produkten der Pharmaindustrie. Wer also Potenzprobleme hat, sollte lieber erstmal auf Mutter Natur bauen.

    LG - Marcel

  9. Jero Says:

    Thanks you for the tip and the title

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.