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.
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.
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.