NewNoteFromSelection-VSCode
Creates a new markdown file with selected text and replaces original text with link to new file
-
Install Macros extension
-
Install FileUtils extension
Note: They're pretty useful tools to have, so its not a complete "waste" to install them just for this task.
-
Open your User settings by pressing Ctrl+Shift+P and tpying "Open Settings (JSON)" or locating settings.json at the following paths
- Windows %APPDATA%\Code\User\settings.json
- MacOS $HOME/Library/Application Support/Code/User/settings.json
- Linux $HOME/.config/Code/User/settings.json
-
Add the following code to the bottom of the file:
"macros.list": { //if you are having trouble with the formatting, you can make this delay slower (1000 is 1 second). Just depends on the speed/load of your processor. "updateNote": [ //if you are having trouble with the formatting, you can make this delay slower (1000 is 1 second) { "command": "$delay", "args": { "delay": 500 } }, "workbench.action.files.save", "editor.action.insertLineAfter", "editor.action.insertLineAfter", "editor.action.clipboardPasteAction", "cursorTop", "fileutils.copyFileName", "editor.action.clipboardPasteAction", "deleteLeft", "deleteLeft", "deleteLeft", "cursorHomeSelect", "editor.action.clipboardCopyAction", "cursorTop", {"command": "type", "args": {"text": "# " }}, "workbench.action.files.save", "workbench.action.closeActiveEditor", //"workbench.action.navigateBack", "deleteRight", {"command": "type", "args": {"text": "- [[" }}, "editor.action.clipboardPasteAction", ], "copySelection": [ "editor.action.clipboardCopyAction" ], }, -
Open the user tasks file by entering Ctrl+Shift+P -> "Tasks: Open User Tasks". This file can also be found (or created) at the same user settings folder paths mentioned in step 3.
-
Insert the following code into the file. If you don't have any existing tasks, you can replace the entire code. NOTE: This is set to use Windows Powershell. You might have to modify the code, especially in the "newNote" section, if you are using a different system
{ "version": "2.0.0", "tasks": [ { "label": "newNote", "type": "shell", "command": "echo '' > -encoding UTF8 '${input:fileName}.md' | code '${input:fileName}.md'", "presentation": { "reveal": "silent", "clear": true, }, }, { "label": "copyText", "type": "shell", "command": "${command:macros.copySelection}", "presentation": { "reveal": "silent", "clear": true, }, }, { "label": "newNoteFromSelection", "type": "shell", "command": "${command:macros.updateNote}", "dependsOrder": "sequence", "dependsOn": ["copyText","newNote"] "presentation": { "reveal": "silent", "clear": true, }, } ], "inputs": [ /* { "type": "promptString", "id": "folderName", "description": "Complete my folder name.", "default": "folder" }, */ { "type": "promptString", "id": "fileName", "description": "New file name for selected texte", "default": "new file name", "presentation": { "reveal": "silent", "clear": true, }, } ] } -
Open the user Keybindings file, by either entering Ctrl+Shift+P -> "Preferences: Open Keyboard Shortcuts (JSON) or opening this file from the same directory listed in Step 3.
-
At the bottom, add the following code and customize the keyboard shortcut to your preference.
{ "key": "ctrl+alt+shift+n", // whatever you choose "command": "workbench.action.tasks.runTask", "args": "newNoteFromSelection" }, -
Open the markdown file with the text that you would like to extract.
-
Select text.
-
Use the keyboard shortcut that you just set.
-
Enter a title in the Input Box. If it has multiple words in it, you might consider adding "-" between each word in case it helps with links. However your links and graph node labels will have "-" in them.
This would also be a very good place for someone to improve the code with some sort of Regex find/replace for "-","_", etc...
-
Wait for macro to run. It should take a few seconds.
-
If you are unsatisfied with the results, you can undo it all by pressing Ctrl+Z.
-
If the formatting is all out of order, look in the settings.json file and increase the delay.
