Skip to content

Instantly share code, notes, and snippets.

@GitMurf
Last active October 10, 2023 11:56
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save GitMurf/fc4132acd805b9c1413ab84c6bd11576 to your computer and use it in GitHub Desktop.
Save GitMurf/fc4132acd805b9c1413ab84c6bd11576 to your computer and use it in GitHub Desktop.
Move the active line of the active file to a chosen file.
<%*
//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;
}
}
}
%>
@GitMurf
Copy link
Author

GitMurf commented Apr 28, 2021

@GitMurf
Copy link
Author

GitMurf commented Apr 29, 2021

Update v1.6: https://gist.github.com/GitMurf/fc4132acd805b9c1413ab84c6bd11576/revisions?diff=unified#diff-239087db550bb609844f862d57fd3856d01b7b86f0bc21154c031d5d2444f338R2

Demo: https://user-images.githubusercontent.com/64155612/116498977-e575e380-a85f-11eb-9333-14b16cf18982.mp4

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

@1nsp1r3rnzt
Copy link

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";
}

@fidel-perez
Copy link

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