notepad++ - Add a string to every line of a selection

23
2014-04
  • Muleskinner

    To prepend a string to every line of a selection, I usually use the "increase line indent" (which add tab to every line) and then run a search for tab / replace with my string.

    Anyone know how I append a string to every line of a selection?

  • Answers
  • Simon

    Note: I misread the question initially, so I've posted this as a comment instead as it's still potentially useful but not an answer

    One method that doesn't involve find/replace (but only works if you want to do it on every line, including blank lines in your block), is this:

    • Move your cursor to the start of the first line
    • Hold down Alt + Shift and use the cursor down key to extend the selection to the end of the block

    This allows you to type on every line simultaneously

  • phihag

    Replace the regular expression $ with the string you want.

  • kek
    1. Choose Search -> Find
    2. Select the Replace tab
    3. Choose Regular expression as Search Mode in the bottom left
    4. Enter as Find what: $
    5. Enter as Replace with: the string you want to have at the end of the lines
    6. Check In selection
    7. Click Find Next or Replace All

    This site is nice for testing regular expressions and has a quick reference:

    http://rubular.com/

  • user103102

    Alternatively,

    1. Choose Search -> Replace
    2. Choose Extended for Search Mode
    3. Find what: \n
    4. Replace with: Whatever\n

    Note that if you leave off the \n in the Replace with, all your lines will be pasted together (which is probably not what you want).

    I usually leave Notepad++ in Extended mode because it allows me to optionally paste lines to gether (by removing \n) or split them (by inserting \n), and doesn't get confused by special characters like *+() etc. (Which I am often searching for in C++ code.)

  • user205987

    This can be done using notepad++. Its explained here step by step. http://dotnet-programming-solutions.blogspot.ae/2012/11/how-to-add-text-to-beginning-or-end-of.html Basically its a regular expression match and replace. It is so simple as find->replace all.


  • Related Question

    Notepad++ find and replace string with a new-line
  • p.campbell

    Consider the scenario where you have a specific string that you want to find-and-replace. You want to replace it with a new string that contains a newline command.

    abc123 xyz456-blah

    fsafd23 xyz456-green

    89hjkf23 xyz456-red

    afdsa23 xyz456-yellow

    abaac123 xyz456-orange

    In the scenario above, I'd like to find " xyz" and replace the space with a carriage return/newline.

    The results would look like:

    abc123

    xyz456-blah

    fsafd23

    xyz456-green

    89hjkf23

    xyz456-red

    etc...

    Question: How would you most easily achieve this using Notepad++? Are there any other tools that you'd suggest to easily perform this command?


  • Related Answers
  • stephenwade

    Notepad++ will do just fine.

    Search string:

     xyz
    Note the space in front of xyz.

    Replace string:

    \r\nxyz

    You will also need to set the "Search Mode" to "Extended" so that Notepad++ honors escape codes.


    Some background: "\r\n" is the escape code for carriage-return, the standard for new lines in Windows. Unix-style systems use simply \n (newline). Most IDEs, Notepad++ included, will understand both styles and portray them each with new lines, but core Windows utilities do not understand \n as being equivalent to \r\n, so the latter is usually the most appropriate if the file is intended to be used in Windows environments.

  • th3dude

    In Notepad++, it's very easy...

    • Find: xyz
    • Replace with: \n


    • Search Mode: Extended (\n, \t, etc)

    The trick is to set the search mode.

  • tj111

    In the "Replace" dialog, make sure that under Search Mode you have "Extended" selected. Then type in the find box enter "xyz" and replace with "\n".

  • Macha

    I cheat a bit when S&Ring characters that I can't type directly into the text fields (e.g. tabs and newlines). Find somewhere in the document which already has that character, then copy it, and paste it into the replace field.

    So in your example, start at the very start of one line, click, drag to the very end of the preceeding line, copy that, and paste it into the Replace dialog.

  • dlux

    For Notepad++, change the search mode to Extended then in the Replace with field use \n.