Geertjan's Blog

  • August 19, 2011

Paste a Multiline Literal with Line Wrap On

Geertjan Wielenga
Product Manager

The new line wrap (in Tools/Options/Editor/Formatting) in NetBeans IDE 7 is great but not so great when you paste a multiline literal:

Better would be if pasting a multiline literal would immediately generate this, i.e., below line wrap is switched on (hence the line wrap arrows at the end of the long lines), while the paste is aware of the multiline nature of the text in the clipboard:

Here's how:

@ActionID(category = "Edit",
id = "org.netbeans.multinline.PasteMultilineAction")
@ActionRegistration(displayName = "#CTL_PasteMultilineAction")
    @ActionReference(path = "Menu/Edit", position = 1350),
    @ActionReference(path = "Editors/text/x-java/Popup", position = 4050),
@ActionReference(path = "Shortcuts", name = "DA-V")
@Messages("CTL_PasteMultilineAction=Paste Mu&ltiline")
public final class PasteMultilineAction implements ActionListener {
    private final EditorCookie context;
    public PasteMultilineAction(EditorCookie context) {
        this.context = context;
    public void actionPerformed(ActionEvent ev) {
        String result = null;
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(null);
        boolean hasTransferableText = (contents != null)
                && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
        if (hasTransferableText) {
            try {
                result = (String) contents.getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException ex) {
            } catch (IOException ex) {
        result = result.replace("\\","\\\\");
    result = result.replace("\"","\\\"");
    result = result.replace("\n","\\n\" +\n\t\"");
        try {
            StyledDocument doc = context.openDocument();
            JTextComponent jtc = EditorRegistry.lastFocusedComponent();
            int dot = jtc.getCaret().getDot();
            doc.insertString(dot, result, null);
        } catch (BadLocationException ex) {
        } catch (IOException ex) {

Also handy for SQL scripts. Here's one:

(TEMP_F - 32) * 5 /9,
RAIN_I * 0.3937

Compare this standard paste result, in a "line wrap on" scenario:

...to this one, via my action above:

Above, you now have a nice place to start working with your query, instead of a whole bunch of error marks in the editor that you need to deal with before integrating the query into your code.

Join the discussion

Comments ( 7 )
  • guest Friday, August 19, 2011

    You asked about more complex netbeans examples. Well; the code above is nice and all but for a newer user to netbeans... where does it go?

  • Geertjan Friday, August 19, 2011
  • asigurare Friday, August 19, 2011

    It seems simple enough when you give us the code just to use it. Anyway thanks for the time spent for this, it is helpful to me.

  • Jesse Glick Friday, August 19, 2011

    Does this have anything to do with line wrap? I think the same would apply without line wrap.

    Anyway this should really be a bug report for the Java editor, I think - pasting a string containing newlines should insert "+" escapes when the context of the paste can be determined to be a string literal according to the lexer.

  • Geertjan Friday, August 19, 2011
  • guest Wednesday, March 7, 2012

    great - voted

  • guest Monday, March 25, 2013

    can't enable it :( Line Wrap is On ...but nothing is happening :(

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.