174

I have a Notepad++ question.

How can I take the below words in Notepad++ (which is on different lines)

Apples
Apricots
Pear
Avocados
Bananas

And turn them into a paragraph with a comma at the end of each one? Like this:

Apples, Apricots, Pear, Avocados, Bananas
343

Open the find and replace dialog (press CTRL+H).

Then select Regular expression in the 'Search Mode' section at the bottom.

In the Find what field enter this: [\r\n]+

In the Replace with:

There is a space after the comma.

This will also replace lines like

Apples

Apricots
Pear

Avocados
Bananas

Where there are empty lines.

If your lines have trailing blank spaces you should remove those first. The simplest way to achieve this is

EDIT -> Blank Operations -> Trim Trailing Space

OR

TextFX -> TextFX Edit -> Trim trailing spaces

Be sure to set the Search Mode to "Regular expression".

7
  • If there are trailing spaces on the line you might for _*[\r\n]+. To deal with the blank lines turned into excess commas you could do a regular expression search for ,_[,_]+ and replace with ,_. Alternatively, the TextFx package has a delete blanks lines command that can be used before adding the commas. (Note change underscores to spaces in these code blocks.) – AdrianHHH Apr 1 '13 at 12:42
  • yes you are very correct. I was just trying to give a minimalistic answer. Maybe I should edit the answer then. – user995502 Apr 1 '13 at 13:00
  • 2
    This doesn't work for me: I see "0 occurrences were replaced." – Iain Samuel McLean Elder Jul 23 '13 at 13:04
  • 3
    @IainElder, Search Mode must be set to "Regular expression" instead of "Normal" or "Extended". – Scott Lawrence Jan 5 '15 at 13:36
  • I had the same result as Iain. – dhochee Feb 22 '16 at 18:35
36

fapDaddy's answer using a macro pointed me in the right direction.

Here's precisely what worked for me.

  1. Place the cursor after the first data item. enter image description here

  2. Click 'Macro > Start Recording' in the menu. enter image description here

  3. Type this sequence: Comma, Space, Delete, End. enter image description here

  4. Click 'Macro > Stop recording' in the menu. enter image description here

  5. Click 'Macro > Run a Macro Multiple Times...' in the menu. enter image description here

  6. Click 'Run until the end of file' and click 'Run'. enter image description here

  7. Remove any trailing characters. enter image description here

  8. Done!

enter image description here

4
  • 5
    This method is slow for thousands of data items. – Iain Samuel McLean Elder Jul 23 '13 at 16:14
  • Great answer. Thanks :) – Peter T. Sep 3 '15 at 21:28
  • Such a great info, will be useful in many similar situations. Thanks! – Moin Apr 26 '17 at 5:00
  • Thanks for sharing the information. It's interesting. – ironman Oct 5 '18 at 14:38
35

Here's what worked for me with a similar list of strings in Notepad++ without any macros or anything else:

  1. Click Edit -> Blank Operations -> EOL to space [All the items should now be in a single line separated by a 'space']

  2. Select any 'space' and do a Replace All (by ',')

3
7

For Notepad++ 5.9

  1. Press Ctrl+H
  2. Select Search mode Extended(\n, \r, \t, \o, \x...)
  3. Enter Find what: \r\n
  4. Enter Replace with: ,
  5. Replace_All should get the required result.
3
  • 3
    This answer repeats the accepted answer and this answer refers to an antique version of Notepad++, version 7.4.x is now available. Welcome to Stack Overflow but please make you answers supply new information or give new insights. Repeating existing answers is not useful. – AdrianHHH Jun 12 '17 at 12:58
  • For newer versions of npp this is the better way to to this. – nurdyguy Jul 18 '19 at 21:02
  • This is the easiest answer. I should not have to go into Regular Expression mode just to replace newlines with commas. N++ actually works correctly whereas other editors interpret "\n" as a carriage return AND line feed, which confused me at first. – BrianVPS Dec 5 '19 at 15:13
3

Place your cursor after Apples, under Macro Tab, select Start Recording. Type the comma(,) character, space( ) character, and press End key, under Macro tab, select Stop Recording.

Ctrl+Shift+P for single playback.

3
  • Thanks for that tip. What if I have 5000++ words? Do I need to run the macro for all of them individually? Is there an easier method? – user2231530 Apr 1 '13 at 10:30
  • If you'll have to perform similar task in the future, you might want to write a script for it. Easy way out is probably just go to the Macro Tab and select run the macro multiple times. It will be done in seconds. – viclim Apr 1 '13 at 12:23
  • +1 Your answer pointed me in the right direction. In my answer I documented exactly what worked for me. – Iain Samuel McLean Elder Jul 23 '13 at 13:39
3

This might sound strange but you can remove next line by copying the whole text and pasting it in firefox search bar, and then re-pasting it in notepad++

step 1

step 2

1
  • 1
    Please improve this answer. Links to images won't be useful. – Drew Dec 3 '16 at 7:27
2

USE Chrome's Search Bar

1-press CTRL F
2-paste the copied text in search bar
3-press CTRL A followed by CTRL C to copy the text again from search
4-paste in Notepad++
5-replace 'space' with ','

1-click for image
2-click for image

1

A regex match with \s+ worked for me:

enter image description here

2
  • BTW: it seems to me that a regex match with $^ should work, but it, instead, matches ^$. – user3673 Jul 17 '19 at 15:06
  • Using the "Extended" option right above the "Regular expression" option (in your screen shot) is actually the easiest way to accomplish this. – nurdyguy Jul 18 '19 at 21:03
0

You can use the command line cc.rnl ', ' of ConyEdit (a plugin) to replace new lines with the contents you want.

Not the answer you're looking for? Browse other questions tagged or ask your own question.