Text Editor inside PowerShell

Paul Masek
PowerShell Explained
4 min readMar 15, 2017

If you’ve ever groaned about having to leave PowerShell just to edit a file, I have good news for you.

I come from a background of having a text editor at my fingertips while doing work in the command-line. If I needed to edit a file, I would simply type “vi filename”, and I would edit the file within Vim (text editor included with most Linux systems), then exit Vim, and be right back at the command-line without skipping a beat. Normally, in order to edit a file while using PowerShell, you need to interrupt your flow by running something like “notepad filename”, which takes you back to the land of GUI’s and mice (oh the horror!), you edit your file, then go back to the command-line land of PowerShell.

Granted this problem that I have an easy solution to, is probably not a huge issue for many people. But, I think there is something to be said for streamlining your workflow, even if it’s a small adjustment (they add up). Being a systems administrator, I’m always looking for tricks and tools to automate and streamline my repetitive processes.

Without further ado, I’ll jump straight to the solution. This does require having “ Bash on Windows” installed. This is also referred to as “Bash on Ubuntu on Windows”. Essentially Bash on Windows allows you to run a Linux environment, including the Bash shell, on Windows. Read more about it here: https://msdn.microsoft.com/en-us/commandline/wsl/about. This site also includes installation instructions.

Now from PowerShell, all you have to do is run:

bash -c “vi filename.txt”

If this is your first time running Vim, you will definitely want to check out the resources at the end of this article before diving in. I’ll suffice it to say that inserting text and even exiting the program require learning. Don’t let these things discourage you from trying though, you’ll be rewarded handsomely in efficiency, if you stick with it.

If you would just like to edit a file without any learning curve, then Nano, which is another command-line text editor, may be a good choice for you. If so, run:

bash -c “nano filename.txt”

Something which may trip you up with Nano, is that the “^” in the keyboard shortcuts at the bottom of the screen is the “Ctrl” key. So “^O” is “Ctrl+o”. Also “^O” is labeled as “WriteOut”, this is the same as Save.

After you edit the file you’re working on, simply save it and and exit, and you’ll be right back to Powershell. You don’t need to run “bash” to go to Linux, then use Vim or Nano, then “exit” to go back to PowerShell.

This command works with both new files that you want to create and editing existing ones. For new files, it will create the file in whatever directory you are in when you run the command. For existing files, you have to use the Linux file path convention, which would look like this:

bash -c “vi /mnt/c/Users/username/Documents/filename.txt”

This is truly all there is to it. It’s so simple that it may even seem like it doesn’t warrant a blog post. I believe it does warrant a post though, especially for the Windows PowerShell users who have little or no Linux experience. Hopefully it opens up a new world of command-line text editors that some didn’t even know existed. For some, like myself, who have used command-line text editors, it will show them how they can be easily harnessed from PowerShell.

Going Further:

If you want to go one step further in efficiency, then Google “PowerShell profile”. Once you figure out what and where the powershell profile files are and which one you should edit, then add the following function to it and you’ll be one step further in your road to efficiency:

#Below function updated 6/22/2018 from EUNJIN LEE’s comment to allow for use of tab completion ($File = $File -replace “\\”, “/”)

#Below function was updated 3/27/2019 from Brendon Thiede’s comment regarding handling spaces in filenames (-replace “ “, “\ “)

function vi ($File){
$File = $File -replace “\\”, “/” -replace “ “, “\ “
bash -c “vi $File”
}

Now every time you want to edit a file you can just run:

vi filename

The same function and command will work with Nano as well.

Great starting point to learn Vim, this is an interactive tutorial: http://www.openvim.com/tutorial.html

Vim has many keyboard shortcuts, print one of these out and put it next to your keyboard: http://michael.peopleofhonoronly.com/vim/

Nice writeup on Vim, which also includes some good resources: http://hackaday.com/2016/08/08/editor-wars-the-revenge-of-vim/

Nano has a much shallower learning curve than Vim, hence only one link: https://www.howtogeek.com/howto/42980/the-beginners-guide-to-nano-the-linux-command-line-text-editor/

--

--

Paul Masek
PowerShell Explained

IT Polyglot (Windows Systems Engineer / Windows SysAdmin / Linux SysAdmin / PowerShell Enthusiast) who dislikes repetitive tasks and loves automation.