Search code examples
regexvimreplace

Adding characters at the start and end of each line in a file


What is the best way to add some characters at the start and end of each line? Can it be done using Vim, or some other way?


Solution

  • In vim, you can do

    :%s/^\(.*\)$/"\1"/
    
    • s/regex/replace/ is vim command for search n replace.
    • % makes it apply throughout the file
    • ^ and $ denotes start and end of the line respectively.
    • (.*) captures everything in between. ( and ) needs to be escaped in vim regexes.
    • \1 puts the captured content between two quotes.