DEV Community

Cover image for How to use VSCode as your Git editor... only when you call Git from within VSCode
Eryk Napierała
Eryk Napierała

Posted on • Updated on

How to use VSCode as your Git editor... only when you call Git from within VSCode

When I'm using git command in the built-in VSCode terminal, for things like editing commit message or proceeding with interactive rebase, it spawns its own, CLI-based editor. That's regular Git behavior but it doesn't really make sense in this context. I have a full-featured editor already open! Why to open another one? It's one of that little insignificant things that annoy me from quite some time, but I never had a motivation to do anything with them. But finally I did! At least that one.

Running an editor within an editor was a bit awkward thing by itself, but what really pushed me to take the action was broken keyboard shortcuts. VSCode doesn't work well with Nano (this is CLI editor I'm using) running inside the built-in terminal. It intercepts Ctrl+K combination that cuts the selected line in Nano so effectively such a basic feature as copy-paste doesn't work. That really breaks the whole point of interactive rebase as re-ordering commits needs copying and pasting.

Configuring an editor used by Git is easy. git config --global core.editor 'code --wait' should do the trick (I found the hint about a parameter required here). The thing is, I don't really want to get rid of Nano. It works well when I do things in a standalone terminal. So, is it even possible to set this setting conditionally, depending on where you call git commands? To eat a cake and have a cake?

After some digging, I found quite a surprising answer to a question I'd never ask, as I did it once and simlpy memoized the answer, which apparently wasn't full. It turns out, that if there is no editor set in the config, Git searches environment variables: GIT_EDITOR and EDITOR. The later is (at least theoretically) used by other CLI programs too, so I decided to use it.

How to make it work as a charm

  1. Open command palette (Ctrl/Cmd + P)
  2. Type >Open settings JSON and confirm with Enter
  3. Add the following lines to your JSON config, adjusting the operating system name when needed
"terminal.integrated.env.osx": {
  "EDITOR": "code --wait"
}
Enter fullscreen mode Exit fullscreen mode
  1. Open ~/.gitconfig file
  2. Remove the line containing the configuration of the editor (it was editor = nano in my case)
  3. Open ~/.bashrc, ~/.zshrc or any other file configuring your shell
  4. Paste a snippet there (choose whatever editor you've had before in your Git config):
if [[ -z "$EDITOR" ]]; then
    export EDITOR="nano"
fi
Enter fullscreen mode Exit fullscreen mode

Et voilà!

Screen recording showing how VSCode works as a git editor when git rebase -i is called from built-in VSCode terminal

In the Terminal app the same git command launches Nano, as expected.

Screen recording showing how Nano works as a git editor when git rebase -i is called from a Terminal app

Enjoy 😀

Top comments (0)