Menu

FEATURE REQUEST: Insert start/end of line

2008-11-19
2012-11-13
  • Mahir B. Asut

    Mahir B. Asut - 2008-11-19

    Hi, if possible please implement this feature; on selected lines, it will be veery helpful to be able to insert some text at the start or at the end of the lines. I hope that it will be also easy to implement. An option under the "Edit" menu is more than enough :) for an example you can check the Lines Manipulation menu of PSPad...

     
    • Mahir B. Asut

      Mahir B. Asut - 2008-11-23

      Thanks all ;)

       
    • Airdrik

      Airdrik - 2008-11-20

      You can do this by opening the replace dialog (ctrl+h), then select the "Regular expression" Search mode option.  Searching for "^" will match the beginning of a line, and "$" will match the end of a line. 
      So to insert "1234" at the beginning of every line, enter Find what: "^" and Replace with "1234" (w/out quotes), then hit replace all.

      There is also a Column Editor in the Edit menu to insert text or incrementing numbers at a given column. 

       
    • Gadrin

      Gadrin - 2008-11-22

      Can't you just record a macro too ?

      Just place the cursor at the start of a line, start record a macro, type in your text and move the cursor to the next line and hit HOME. Stop recording the macro.

      Then play it back.

      To place text at the end of a line: put cursor at beginning of line, start record macro, hit the END key, type in your text, move cursor to next line, hit HOME and end macro recording. Then play it back.

      If you need both, then record a macro that puts text at the start/end of each line.

      >

       
    • Fool4UAnyway

      Fool4UAnyway - 2008-11-22

      Hey, let me make a wise-guy impression!

      1. You can also search for linebreaks or newline characters, if you don't mind putting your text manually at the start of the very first or at the end of the very last line.

      In the Ctrl+H normal Replace dialog:
      - select Extended mode
      - Find: \r\n (or just \r or \n)
      - Replace by: [ text at the end]\r\n[text at the start ]

      In the Ctrl+R advanced Replace dialog:
      - Find: ^M (Press Ctrl+M or Ctrl+Enter)
      - Replace by: [ text at the end]^M[text at the start ]

      Of course, there is no linebreak before the first line or after the last line. You would have to enter your text manually there, or, in case you don't select the very first or last line completely: also select the linebreak before the first and/or after the last line.

      2. The QUICKEST way to insert text at the start of lines

      Select all lines you want to insert text before.

      Hold down the SHIFT key.
      Also press ALT.
      Press CursorRight (or CursorLeft).
      Press CursorLeft (or CursorRight if you just pressed CursorLeft).
      Release all keys.
      Press ALT+C.
      Enter the text to insert before all "selected" lines.

      You won't see any selection anymore, because you have switched to column mode, with the number of selected columns being 0.

      Thank me!

      Read some other posts by Fool4UAnyway to find out more tricks.

       
  • Rodney Beede

    Rodney Beede - 2011-02-06

    So I've written a plug-in that will insert " at the beginning and end of all lines in the current document.  This is faster than having to type in the regex in the Find/Replace dialog.

    You need to (manually) install the NppScripting plug-in by Kremer Eugen (http://www.softwarecanoe.de/data/nppscripting.zip) and copy the lineinserter.js file into the "includes" folder for that plug-in.

    I wanted to make it prompt for the text to insert, but there isn't a good API in the NppScripting plug-in for doing so.  I tried the provided Dialog class, but the IE window wouldn't open with focus on Windows 7.  Any ideas on how to prompt the user for input?

    Downloads:

    http://www.softwarecanoe.de/data/nppscripting.zip
    http://www.rodneybeede.com/downloads/lineinserter.7z

     
  • Dave Brotherstone

    At the risk of sounding cheeky… use the PythonScript plugin, and notepad.prompt('Insert what?', 'Prompt title')

    Then the whole script is just:

    insertText = notepad.prompt('Insert what?')
    if insertText:
        editor.pyreplace('^', insertText)
    

    You could even make it run over either the selection, or if there's no selection the whole document like this:

    insertText = notepad.prompt('Insert what?', 'Add line prefix')
    if insertText:
        selection = editor.getUserLineSelection()
        editor.pyreplace('^', insertText, 0, 0, selection[0], selection[1])
    

    Assign to menu, shortcut and/or toolbar as desired :)

    Dave.

     
  • Rodney Beede

    Rodney Beede - 2011-02-07

    Does PythonScript require that I have python installed as well?

    If not then it would definitely be easier.

    What about doing the end of the line as well?  Including the last line in the file which won' t have a \r\n or \n at the end?