Last active
March 22, 2025 17:26
-
-
Save GitMurf/fc4132acd805b9c1413ab84c6bd11576 to your computer and use it in GitHub Desktop.
Move the active line of the active file to a chosen file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%* | |
//v1.6.2: Fix with update to Templater where wasn't removing the selected text/line "on move" | |
//'first' will add to top of file. 'last' will add to bottom of file | |
let firstOrLastLine = 'last'; | |
//Choose a specific line to move to underneath; overrules firstOrLastLine if true | |
const bChooseLine = false; | |
//After moving the line, open the file it was moved to | |
const bOpenFile = false; | |
//"move" = default, "copy" = duplicate, "embed" = move and leave a block ref link, "blockRef" = create block ref and add an embed link in other file | |
const moveMode = "move"; | |
const moveToFile = await tp.system.suggester((item) => item.path, this.app.vault.getMarkdownFiles(), false); | |
if(moveToFile) { | |
let cmEditorAct = this.app.workspace.activeLeaf.view.editor; | |
if(tp.file.selection() == '') { | |
const curLine = cmEditorAct.getCursor().line; | |
cmEditorAct.setSelection({ line: curLine, ch: 0 }, { line: curLine, ch: 9999 }); | |
} | |
let selectedText = tp.file.selection(); | |
tR = selectedText; | |
const curContent = await this.app.vault.read(moveToFile); | |
let newContents; | |
let selectLine; | |
if(moveMode == "blockRef" || moveMode == "embed") { | |
function createBlockHash() { | |
let result = ''; | |
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; | |
var charactersLength = characters.length; | |
for ( var i = 0; i < 7; i++ ) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} | |
let id = createBlockHash(); | |
let blockRef; | |
if(moveMode == "blockRef") { | |
blockRef = `![[${tp.file.title}#^${id}]]`.split("\n").join(""); | |
tR = tp.file.selection() + ` ^${id}`.split("\n").join(""); | |
selectedText = blockRef; | |
} else { | |
blockRef = `![[${moveToFile.basename}#^${id}]]`.split("\n").join(""); | |
tR = blockRef; | |
selectedText = '\n' + tp.file.selection() + ` ^${id}`.split("\n").join("") + '\n'; | |
} | |
navigator.clipboard.writeText(blockRef).then(text => text); | |
} | |
if(bChooseLine) { | |
const arrayEachLine = curContent.split('\n'); | |
let arrayStrings = ['--End of File--', '--Beginning of File--']; | |
let arrayValues = ['last_', 'first_']; | |
arrayEachLine.forEach(eachItem => { | |
if(eachItem != '') { | |
arrayStrings.push(eachItem.slice(0,250)); | |
arrayValues.push(eachItem); | |
} | |
}); | |
selectLine = await tp.system.suggester(arrayStrings, arrayValues, false); | |
if(selectLine == 'last_' || selectLine == 'first_') { | |
if(selectLine == 'first_'){ | |
firstOrLastLine = 'first'; | |
newContents = selectedText + '\n' + curContent; | |
} else { | |
firstOrLastLine = 'last'; | |
newContents = curContent + '\n' + selectedText; | |
} | |
} else { | |
if(curContent.contains('\n' + selectLine + '\n')) { | |
newContents = curContent.replace('\n' + selectLine + '\n', '\n' + selectLine + '\n' + selectedText + '\n'); | |
} else if(curContent.startsWith(selectLine + '\n')) { | |
newContents = curContent.replace(selectLine + '\n', selectLine + '\n' + selectedText + '\n'); | |
} else { | |
firstOrLastLine = 'last'; | |
newContents = curContent + '\n' + selectedText; | |
} | |
} | |
} else { | |
if(firstOrLastLine == 'first'){ | |
newContents = selectedText + '\n' + curContent | |
} | |
else { | |
newContents = curContent + '\n' + selectedText | |
} | |
} | |
if(bChooseLine == false || selectLine) { | |
await this.app.vault.modify(moveToFile, newContents); | |
if(bOpenFile) { | |
await this.app.workspace.openLinkText(moveToFile.basename, moveToFile.path, true); | |
if(bChooseLine == false || selectLine == 'last_' || selectLine == 'first_') { | |
cmEditorAct = this.app.workspace.activeLeaf.view.editor; | |
if(firstOrLastLine == 'first'){ | |
cmEditorAct.setSelection({ line: 0, ch: 0 }, { line: 0, ch: 9999 }); | |
} | |
else { | |
cmEditorAct.setSelection({ line: cmEditorAct.lastLine(), ch: 0 }, { line: cmEditorAct.lastLine(), ch: 9999 }); | |
} | |
} | |
} | |
if(moveMode == "move") { | |
cmEditorAct.replaceSelection(''); | |
tR = ''; | |
} else if(moveMode == "copy") { | |
tR = selectedText; | |
} | |
} | |
} | |
%> |
Choose which line to move to in the selected file. Must set the bChooseLine
boolean to true
.
Change the const moveMode = "move";
to your choice. I recommend making multiple templates with the same code and then just changing this variable for the different "modes" outlined below.
Now can do the following:
- Move multi-lines if you select them before running script
- "Copy" instead of "move"
- Move but leave a block ref embed link behind in original location pointing to new location
- Leave in current location and create block ref and then choose where to copy a block ref embed link to in another file
Awesome. Thanks a lot for sharing it.
I have added a prompt for 3 use cases of move and open the file, copy alone and "copy open the file".
In case any one needs
const moveOrCopy = await tp.system.prompt(" LEAVE EMPTY TO MOVE,0 = MOVE and OPEN,1 = COPY, 2 = COPY AND OPEN");
const moveToFile = await tp.system.suggester((item) => item.path, this.app.vault.getMarkdownFiles(), false);
if(moveOrCopy == 2 || moveOrCopy == 0){
bOpenFile = true;
}
if(moveOrCopy == 2 || moveOrCopy == 1){
moveMode = "copy";
}
Thank you for this.
I made a fix that, in case of first line but the objective MD document has some yaml metadata, it will be inserted just below the yaml metadata:
const yamlRegexp = /^---$/gm;
const yamlVariables = [...curContent.matchAll(yamlRegexp)];
if (yamlVariables.length > 1) {
const position = yamlVariables[1].index+4
newContents = [curContent.slice(0, position), selectedText, '\n', curContent.slice(position)].join("")
} else {
newContents = selectedText + '\n' + curContent;
}
I added it in the two lines where we were doing newContents = selectedText + '\n' + curContent;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo: https://user-images.githubusercontent.com/64155612/116369617-6ed4d980-a7be-11eb-839a-24ed9bd596b0.mp4