How to Paste Multi-Line Text as A Single Line

Ever copied from a PDF or resource, only for the paste to be…

A paragraph split into
several lines like this?

A couple of plugins in conjunction can fix this:

  1. The Text Format Plugin apparently solves a majority of this with its “Merge Broken Paragraph” command (Paste → Select Text → Run Command w/ Hotkey).
  2. If you want an even better solution (just Paste), the post “Select the pasted text immediately after paste?” has a slightly-broken solution. Paste their solution, and after the line editor.replaceRange(clipboard, currPos), add `const endPos = editor.offsetToPos(editor.posToOffset(currPos) + clipboard.length). Then, create a QuickAdd macro to 1.) Paste and Select → 2.) Run Command and bind it to a hotkey.

which one? (url or author pls)

Should be this one : GitHub - Benature/obsidian-text-format: Format seleted text in Obsdidian.md

1 Like

Just realized I can reply to my own posts, and post links and whatnot where the Forum wouldn’t let me before. Here’s the rest of the information:

Plugins:

Code:

plugin.addCommand({
    name: 'Paste and Select',
    id: "paste-and-select",
    callback: async () => {
        const activeFile = app.workspace.getActiveFile();  
        const activeView = app.workspace.activeLeaf.view;

        if (!activeView) {
            console.warn("No active view!");
            return;
        }

        const editor = activeView.editor;
        const currPos = editor.getCursor();

        const clipboard = await navigator.clipboard.readText();
        if (!clipboard) {
            window.alert("No text in clipboard");
            return;
        }

        editor.replaceRange(clipboard, currPos);
        const endPos = editor.offsetToPos(editor.posToOffset(currPos) + clipboard.length);
        console.log(currPos, endPos);
        editor.setSelection(currPos, endPos);
    }
});

Setup:

  1. Paste the code into the User Plugins’ Snippets field, then press the > symbol on the left.
  2. QuickAdd: Create Macro “Single-Line Paste”
  3. Steps within the macro: 1.) User Plugins: Paste and Select, 2.) Wait for 10ms, 3.) Text Format: Merge broken paragraph(s) in selection
  4. Bind the Macro sequence to a hotkey.