Select the pasted text immediately after paste?

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

After pasting a long text, I want this text to be selected, so that i can make holistic operation on it, such as make them bold or put them in block quotes(if the text has multiple breaklines).

But at present the cursor will be at the end after pasting, forcing me to drag my mouse and select the text.

Things I have tried

search “Select the pasted text” on google and found nothing for Obsidian.

Thanks for help.

1 Like

Interesting idea! It would probably make a good plugin idea.


When you say “forcing the mouse”, I just wanted to point out that there are hotkeys to select words and lines at a time, depending on your OS. It doesn’t address your idea, but it might help sometimes. I love avoiding the mouse when possible.

Some Vim masters might know how to do this in Vim mode. But that would only help Vim mode users.

Conceptually this could be done like the following:

  • Get the active editor, and store in activeEditor
  • currentPos = activeEditor.getCursor() to save current position in that editor
  • Use something like navigator.clipboard.readText() to get the clipboard content
  • Insert the text into the editor, could possibly be done using activeEditor.replaceRange()
  • Get the new position of the cursor,
  • And use both position to set the new activeEditor.setSelection()

That’s one potential way to do this… Now what remains is to actually code it altogether…

1 Like

Ok, lets start with a disclaimer that I don’t know how robust or safe this code is, but it seems to do the trick. Install the User Plugins plugin, and after enabling it go to the settings of this plugin and insert the code below in a Snippets box, and hit the > icon to the left of the box to “load” the command:

plugin.addCommand({
    name: 'Paste and select',
    id: "paste-and-select",
    callback: async () => {
    // RESTART INDENTATION LEVELS -- Needs }}) at end 

const activeFile = app.workspace.getActiveFile()  
const activeView = app.workspace.activeLeaf.view
if ( ! activeView ) {
  console.warn("No active view!")
} else {

  const editor = activeView.editor
  const currPos = editor.getCursor()
  const currOffset = editor.posToOffset(currPos)

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

  editor.replaceRange(clipboard, currPos)
  console.log(currPos, endPos)
  editor.setSelection(currPos, endPos)
}

}});

Now you should have a command in your command palette called: User Plugins: Paste and select which you could assign to a hotkey of your choice. I chose cmd + ctrl + V (just because it fits with a theme of mine for hotkeys), and after copying some text into the clipboard, executing this hotkey surely pasted and selected this text.

I’ve not tried, and not going to try, this with multiple selections or stuff like that. So take it as it is, and see if it serves your purpose.

You could surely add this command through other means, but I found this to be a simple way to add a custom command to a vault.