Replies: 1 comment 1 reply
|
Apologies this went unanswered for so long. You're right that the built-in editor commands (Select active line, Copy, Paste) are line-scoped, so they can't extend to a multiline selection. A small user script in a Macro closes the gap: it duplicates the current selection below itself, re-selects the copy, and runs whatever command you point it at. Save this as a user script in your vault, e.g. module.exports = async (params) => {
const { app, obsidian } = params;
const view = app.workspace.getActiveViewOfType(obsidian.MarkdownView);
const editor = view?.editor ?? app.workspace.activeEditor?.editor;
if (!editor || !editor.somethingSelected()) {
new obsidian.Notice("Select some text first.");
return;
}
const selection = editor.getSelection();
const to = editor.getCursor("to");
// Insert a blank line, then the duplicated block, right after the selection.
editor.replaceRange("\n\n" + selection, to);
// Re-select the duplicate (it starts two lines below where the selection ended).
const start = { line: to.line + 2, ch: 0 };
const selLines = selection.split("\n");
const end =
selLines.length === 1
? { line: start.line, ch: selection.length }
: {
line: start.line + selLines.length - 1,
ch: selLines[selLines.length - 1].length,
};
editor.setSelection(start, end);
// Run whatever command you want on the re-selected block:
app.commands.executeCommandById("editor:toggle-bold");
};Then create a Macro choice, add this script as its command, and toggle the Macro's own command on (the lightning bolt) so you can bind it to a hotkey. To target a different command, swap the id in the last line. To find a command's id, open the developer console (Ctrl/Cmd+Shift+I), type Selecting |
Uh oh!
There was an error while loading. Please reload this page.
Probably easy, I just don't know how. I want to be able to select some text and then:
Basically I want to paste a selection below itself and then modify it. Specifically, something like:
I have made progress on doing 2,3, and 4. For example, I have tried just creating a macro that pastes, selecting the current line, and then runs the command. This works on single line text, but not on multiline text, which is what I need.
The transition between 1 and 2 (i.e. copying, then moving to end of selection, adding paragraphs, and then running 2) is also something I would like to be able to do.
Does anyone know how to do this? Is this possible?
All reactions