4

I have a lot text, and I want to remove the first 4 characters in all lines, then insert a character at the beginning of all lines.

How can I do this using Notepad++?

5 Answers 5

4

Search -> Replace, make sure you have "Search Mode" set to "Regular expression"

Find what: ^....
Replace with: Your_new_character

Replace All

That will only work on lines that have at least 4 characters in them, though.

3

If you press and hold Alt while using the mouse you can select multiple lines at once. So, just press and hold Alt, use your mouse to select the first 4 characters all the way down, hit Backspace, then type the character you want to insert. It'll be applied to all of the lines at once.

2

You can use the record and playback feature.

  1. With your cursor at the beginning of the 1st line, Click on 'Start Recording'
  2. Type Delete, Delete, Delete, Delete, X (or whatever you want), , Home
  3. Click on 'Stop Recording'
  4. Click on 'Run a Macro Multiple Times...'
  5. Select 'Run until the end of file' and click Run
  6. Job done

This answer assumes you have text on all lines, and that they are at least 4 characters long.

Screenshots

enter image description here

Type Delete, Delete, Delete, Delete, X (or whatever you want), , Home

enter image description here

enter image description here

enter image description here

enter image description here

1

Use 'find replace' with regular expression look for "\n...." and replace with "\n" (see picture).

Use with caution i.e. if you run that thing twice it will delete twice

enter image description here

1
  • Probably not a big deal, but \n.... will always miss the first line. You could instead search from the beginning of the line, ie. ^....
    – MrWhite
    Feb 7, 2016 at 0:15
1

The easiest way to do this is to use the regular expression option on the find-and-replace menu.

Replacement Steps

  • Press Ctrl+H to bring up the replace dialog (or select Search > Replace... from the top menu).
  • Select Regular expression under "Search Mode".
  • Use ^.{0,4}(.*)$ as the find-expression.
  • Assume for the sake of example that the character you want to insert is the "#" symbol. Use #$1 as the replace expression. (Replace the "#" with whatever character you need. Note that you may need to use an escape sequence for certain characters).
  • Click replace All.

Explanation

Find-Expression

  • The ^ and $ characters mark the beginning and end of a line. This allows you to search just single lines at a time.
  • The . is a wildcard that matches any single character.
  • The {0,4} indicates the number of times we want to match the preceding expression. In this case, {0,4} means that we are looking for matches of anywhere from 0 to 4 characters (matching is "greedy" so it will try to get four if possible, but will settle for less if there are only, say, two characters available on the line). If you want to exclude blank lines, you could use {1,4} instead. If you wanted to match only lines with at least four characters, just use {4}.
  • (.*) matches the rest of the line. . is again a wildcard (matches any character), and * means to match 0 or more times (all the way to the end of the line, denoted by $). The parentheses (...) mark this as a "group," which allows us to re-use what it matches in the replace-expression.

Replace-Expression

  • The first character of the replace-expression will be the character you want to insert at the beginning of each line. Certain characters (e.g. $) have special functions. If you wanted to insert the literal dollar-sign character, you would need to use \$ instead of just $. See the link below for more information.
  • The $1 inserts the text that matched the first (and only) group (i.e. the sub-expression inside the parentheses) of the find-expression. In this example, this would be the fifth character of the line and onward.
  • Note that you do not need to mark include the beginning-of-line (^) or end-of-line ($) characters in the replace-expression.

The links below has more information about Regular Expressions in Notepad++.

http://docs.notepad-plus-plus.org/index.php/Regular_Expressions

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .