Editing with Ease

Editing with Ease

If it wasn't clear until now, I'm a tinkerer. If it's software, and there is a way to tweak software, I'm going to try it out. Otherwise, this whole adventure seems mute - I mean, why try something that isn't 'the norm'?

My previous posts established the challenge, and initial setup. There is one more prep I need before starting to focus on my server work flow.

The matter of script editing workflow for this shell environment.

I always make it a point to make editing my environments script easy. In Zsh I have a function that loads all the relevant startup scripts into the editor, and when done refresh the shell. This isn't relevant here, as I chose to use script files instead of preloaded functions (I can say after using it for a bit, I really like this approach).

So, my plan is to focus on a helper script just for editing (what I'm calling) MyScript(s).


Just like MyAliases, MyScripts will be the Noun I'll use to designate those cmdlet / scripts I dump in $MyPSScriptRoot directory.

First I'll establish what accounts for a MyScript:

  • Any .ps1 file in $MyPSScriptRoot - these should be treated like Function scripts (as  in, every script can be called as a function with parameters defined at the top of the script).
    * These are also included in MyAliases.
  • Any .ps1 scripts contained in sub folders of $MyPSScriptRoot
    * These will not be aliased by Reload-MyScripts
  • A special case of the above, is the profile.d directory where it's .ps1 scripts will be loaded by Reload-MyScripts which should also be called in $PROFILE

With the definition established, now let's decide how to initiate an editor.

Laziness is Key

NeoVim has been my editor of choice in the Linux console, and so it shall remain. However, I rarely invoked it directly, even when I typed vi or vim in the Zsh command line, it was usually a function or an alias to a pre-baked call.
This allowed me to have a consistent, comfortable experience.

NeoVim also has syntax highlighters for different languages, PowerShell being one of those. I have not setup those yet, when I do, I'll post on this separately.

Suffice to say, I want to call something that loads the editor exactly the way I like it.


I'm going to preserve this, but creating a Edit-TextFile script, which will accept a list of file names, open them in my editor of choice (defaulting to NeoVim) preferably in a tabbed interface, and offer the option to elevate with the -sudo option.


This script will call Edit-TextFile, but instead of receiving full paths to the scripts it will be able to do the following:

  • Accept a script name (without the extension) and provide auto-completion for those.
  • Accept a new name to start a new script.
  • Migrate an existing function into a MyScript file, and open in an editor.

Most importantly, once edit is done, Reload-MyAliases Reload-MyScripts will be called.

This function will require an additional script named Export-FunctionSource which takes a function name and outputs the source code of the function. This is for the migration of an existing function feature.


Edit all active profile files, or force editor to open all profile files - even the ones that do not yet exists, with (or without) sudo.


Reload-MyAliases is getting a makeover, and from now on will be Reload-MyScripts.
It is still loading MyAliases just like before, only now it also in charge of loading the scripts from the profile.d sub-folder.

It is important to source Reload-MyScripts so that it will load in the shell's scope, and so it calls (actually sources) a new function named Get‑ScopeDepth which tests for this, and if running in a script sub-scope, it will notify that it should be sourced.

This means that if changes to scripts within profile.d are made, it is important to source the edit and reload scripts, like:

. Edit-MyScript profile.d/some_script_loaded_with_profile.ps1


. Reload-MyScripts
The dot is not there by mistake (that's what it means 'to source')

Additional code

Some additional script function

* Source Code

Link to latest version At the time of writing
Edit-MyProfiles commit 06052cfa29
Edit-MyScript commit 06052cfa29
Edit-TextFile commit 06052cfa29
Export-FunctionSource commit 06052cfa29
Get-MyAliases commit 06052cfa29
Get-MyScript commit 06052cfa29
Get-PossibleArguments commit 06052cfa29
Get-Profiles commit 06052cfa29
Get-ScopeDepth commit 06052cfa29
Reload-MyScripts commit 06052cfa29
FunctionName Class commit 06052cfa29
MyScript Class commit 06052cfa29