If I have a document of 10 lines and I have a line selected.
How do I invert the selection so that instead, all the other lines are selected?
I searched and searched, could not find anything on this!
Any help would be appreciated.
If I have a document of 10 lines and I have a line selected.
How do I invert the selection so that instead, all the other lines are selected?
I searched and searched, could not find anything on this!
Any help would be appreciated.
Hey Louisa,
What you want is possible (with Javascript).
It seems important to you, so I made a feature…
**
HOW IT WORKS:
RESULT:
**
Here is an example, to show what it does:
1 - ORIGINAL TEXT (say you want to copy everything except “cut line”):
# header
good line
- list
- list
good line
cut line
good line
2 - ACTION: (click on “cut line”, then hit “Control Y”)
3 - RESULT: COPIED TEXT (notice that “cut line” is gone)
# header
good line
- list
- list
good line
good line
**
THE CODE - here is your code:
// copy all lines above and below active line
// hotkey => "control y"
document.onkeydown = function (e) {
if (e.ctrlKey == true && e.key == "y") {
// override default "control y" hotkey (if it exists)
e.preventDefault();
// get text (from lines above and below)
var getInactiveLines = document.querySelectorAll(".cm-line:not(.cm-active)");
// create the new string
var newString = "";
for (var i = 0; i < getInactiveLines.length; i++) {
var lineText = getInactiveLines[i].innerText;
// preserve blank lines
if (lineText === "\n") {newString = newString + "\n";}
// add text to the ultimate string
else { newString = newString + "\n" + lineText;}
}
// remove zerowidthspaces
var finalCopy = newString.replace(/\u200B/g, "");
// copy text to your clipboard
navigator.clipboard.writeText(finalCopy).then(
function () {
console.log("copied to clipboard");
},
function (err) {
console.error("could not copy: ", err);
}
);
}
};
**
And here is how to add that code to Obsidian:
Add the plugin “Javascript Init” [Preferences > Community Plugins > Browse > Javascript Init]
Open Preferences > Javascript Init (in left column)
Now paste that code into the big box. Close Preferences.
In the top menu, click View > Force Reload (this refreshes Obsidian)
DONE - Now “Control Y” will copy every line above and below the active line (the line with a flashing cursor).
**
NOTE: To keep any formatting in your “Control Y” copied text (like headers and lists), use Source Mode in your editor.
**
^ okay - that’s a long post, but it works.
(And you only have to set it up once).
Give it a try
If you have any questions, feel free to ask.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.