Dyota's blog

PowerShell: Taking notes

Note-taking

I use PowerShell every day for small, regular things, like writing notes. A lot of time, while I'm doing an activity, or while I'm in a meeting, I need a notepad open so that I can jot down notes while things are happening.

These days I do this in the shell. Why? Because I have the shell open much of the time anyway (I have it eighth on my Windows taskbar, so I only need to hit Win + 8 to open it) and I've made it into a frictionless experience to get in an out quickly.

Friction?

The type of friction I'm talking about is: opening Notepad; saving it in a desired location; giving it a meaningful name; and "multitasking" with it.

Friction #1: opening notepad

I use a text editor in the shell. I use NeoVim, and I have a configuration that I'm comfortable with. To make a new note, I call my own custom function, nwt, that prompts me for a filename, and opens that text file in NeoVim for me.

Why is it called nwt? In Windows, if you're in a folder, with your cursor not focussed on a file, if you hit the combo Shift+10, W, T, it will create a new text file for you. In my head, nwt is an analogous acronym for that shortcut.

Friction #2: saving it in a location

The function nwt always creates the note file in the same location that I set. I don't have to think about where to put it.

Friction #3: giving it a meaningful name

The function automatically starts the filename with the current date in the format yyyy-mm-dd, and appends the filename that I specify. The file format is .md (Markdown).

Friction #4: "multitasking" with it

My notepad desktop application of choice is Notepad++. Much of the time, I need to have my notes page and a working scratch pad open in Notepad++ open at the same time.

Using a text editor in the shell relieves NPP from having to pull double duty.

But what if I need to exit the notes text editor and do something else in the shell? This comes up often too. But that is why I have the script "remember" what the last working file was, and another function backin opens up that file in NeoVim.

Full code

function nwt($title) {
    if ($title.length -eq 0) {
        Write-Host "You must have a title."
        break
    }
    $dir = $documents
    $date = Get-Date -Format "yyyy-MM-dd"
    $filename = "$date $title.md"
    New-Item -ItemType "File" "$dir\$filename"

    # this allow backin to work
    Set-Content -Path "$dir\lastnote.txt" -Value "$dir\$filename"

    nvim  "$dir\$filename"
}
function backin {
    $lastNote = (Get-Content "$documents\lastnote.txt")
    nvim $lastNote
}