All Articles

Setup Visual Studio code as the default editor for Kubectl and OpenShift CLI

When working with kubectl or the OpenShift CLI oc there are times where you just need to edit a resource using the command oc edit <resource> or kubectl edit <resource>. Apply the changes and let the magic of your cluster work to apply them.

By default both of these commands will use an already installed application as the editor, for Linux/MacOS users that would be vi and for Windows it would be notepad.exe.

Don’t get me wrong, I have nothing against vi (I am a regular user of vim myself) on the contrary I really like for quick a simple edits that I need to do but I also like the easiness of Visual Studio Code and I’m a firm believer of using the right tool for the right job.

Editing fairly large yaml files in my opinion can become an easy task using Visual Studio Code but how do I tell these CLIs to use it? I did spend a few days exporting the YAML, editing and importing it again before asking myself this question.

The answer is simple, add the following environment variable to your path (in my case adding it to ~/.zshrc)

export KUBE_EDITOR=“code -w”

For this to work you will need to have Visual Studio Code Installed and have added it to your PATH. Here is a quick guide for Mac. Windows or Linux? No worries, the command should already be there when you install it.

And that’s it. Now every time you use oc edit or kubectl edit a VS Code windows will open, save that file and once you close it the changes will be applied.

*For the curious people and to save you a couple of google searches, the -w in the command is basically telling VS Code to wait. The help menu on the CLI (code —help) reads:

-w —wait Wait for the files to be closed before returning.

Extra tip. Add comments to your .zshrc changes, they may see obvious now but you never know. Here is mine:

Cesar’s KUBE_EDITOR setting