Making Visual Studio Code the default text editor on Git

visual-studio-code-tutorial-git Early on the year when i was working on developing Windows Phone i stumbled upon version control which was a pretty cool way to keep track of codes. Previously when i was taking programming classes i saved all my files onto OneDrive and i still do it. But that’s for simple single file programs, for Windows Phone apps as well as the program for the Freescale Cup, things aren’t that simple. I started off blindly using Git via the built in plugin for IDEs. Back then (a few months back) I only knew how to add, commit and push it to TFS and GitHub. Everything on the IDE was simplified but soon enough i stumbled upon this Udacity course after hearing Google talk about the Android Developers nanodegree on Udacity.

Well, after about 2 weeks, i’m done with the course and I have learnt a lot about version control and how important is it for programming. Of course, I could always just do it the old fashioned way of saving it on a cloud storage service but the experience is not that rich as to tracking other than name changes. I believe that institutions should teach version control alongside the introductory programming paper as it is a core component in the working industry. The course opened my eyes to Git which is not just a plugin which sits in an IDE but a versatile platform which can intelligently track and push/pull updates from a version control manager. The base concept is a lot like learning a new programming language and after O’d learn all the basics, then it’s up to me to bring up the best in the situation.

While working on the course, one of the first things i needed to do was to setup Git on my system and one of the points was to specify a default text editor to launch code files and commit messages. So, I decided to rely on Visual Studio Code but i was not able to find a solution online so that’s why I’m writing this article today. During Microsoft’s BUILD conference this year, they announce Visual Studio Code which is a cross platform code editor, you can read more on that at the tech today. But, what you might not know is that Visual Studio Code is actually based off Github’s open source text editor, Atom. In the rest of the article, i will be focusing on guiding you to make VS Code as your default text editor on Git. This guide is based off Windows but it will also work on Mac too! Feel free to ask me any questions in the comments section if you need some clarification on how things work.

Creating a .bash_profile

If you have not made any custom changes to Git Bash, then you technically don’t have a bash_profile file. You can do so by running the following commands on Git Bash. You will need this file to make launch Visual Studio code with straight from Git Bash.

cd ~/
touch .bash_profile

Making Visual Studio Code the default text editor on Git Bash

Well, the first thing you’ll need to do is to find the directory in which Visual Studio Code is installed on your system. Your directory might vary compared to mine but if you followed the standard installation it should be in the AppData folder of your user account. And depending on when you are following this guide there might be an updated version so just get the directory for the latest version [app-…].

C:\Users\dickw\AppData\Local\Code\app-0.1.1\code.exe
C:/Users/dickw/AppData/Local/Code/app-0.1.1/code.exe

The after that you will need to type in this command into Git Bash with your own directory. Also, do not that the directory needs to utilize a forward slash ‘/’ opposed to a backslash that the windows explorer uses . Follow the directory as the one on top in green.

git config --global core.editor "[program directory]"

OR

git config --global core.editor "'[program directory]' -n -w"

visual-studio-code-directory-windows

Adding a shortcut to open Visual Studio Code from Git Bash

To do this, all you need to do is add a command into your .bash_profile file. If you don’t have that file, follow my guide on top to create one. The first thing to do is to open your .bash_profile file which is in the directory where you initialize git. For me, it was in my user profile but if you can’t find it don’t worry. Because there is a ‘.’ behind of the file name, it means that it’s a hidden file. In order to make it visible type in the following command into Git Bash.

mv .bash_profile bash_profile

This line of code will remove that dot in front of your bash_profile for the time being. However, if you did set Windows Explorer to show hidden files in default, then you can just skip this step. After running that command, you should be able to find your bash_profile file in the directory where you initialized git. From there, right click on the file and open it in a text editor (not a rich text editor like Microsoft Word) like Notepad or preferably Visual Studio Code since you are trying to take advantage of that. git-bash-profile-directory Don’t be alarmed if you see a blank file if you just created the bash_profile file earlier or you have not added any custom changes. Again, you will need to scour for the directory where Visual Studio Code is installed on your computer. Scroll back up if you want to find out how to find the directory where Visual Studio Code is installed. Note: like previously, use the forward slash ‘/’ for the file directory.

C:/Users/dickw/AppData/Local/Code/app-0.1.1/code.exe

After doing that, add this line of code into your bash_profile file.

alias code="C:/Users/dickw/AppData/Local/Code/app-0.1.1/code.exe"

All that’s left now is to save the bash_profile file and re-add the dot in front of the file if you made changes to it earlier

mv bash_profile .bash_profile

git-bash-profile-visual-studio-code After adding that into your .bash_profile file, you will be able to open files from Visual Studio Code in Git Bash by simply typing code before the file and it’s file extension. For example my text file is called cake-recipe and to open it in VS Code, i will type in

code cake-recipe.txt

Finally, to create a text/code file to open in Visual Studio Code from Git Bash, just cd to the directory you want to initialize the file and type in code in front of the file name. For example i want to create a new file called tutorial based on java. So, i’ll run this command

code tutorial.java

Of course, you aren’t restricted to just opening files and creating them. When committing a file in Git, Visual Studio Code will also be the text editor launched if you made it your default text editor. visual-studio-code-in-git-bash

Enabling copy and paste in Git Bash

This is probably something you will eventually want to enable because typing all those long and random commit IDs is not an option. So, to enable the copy and paste commands, all you have to do is enable them in the Git Bash properties. Right click on the Git Bash window and select properties. From there, navigate into the options tab and put a tick onto the enable ctrl key shortcuts option. Click ok and restart Git Bash and you should be able to copy and paste text inside Git Bash. git-bash-properties git-bash-enable-copy-and-paste A little extra tip, you can use the standard keyboard CTRL + C and CTRL + V commands on Git Bash but you can also opt to use the right click your your mouse/trackpad to copy and paste text. Just highlight the text you want to copy on the Git Bash window and right click on it. To paste the text, simply do another right click and the text will be pasted.

UPDATE 20/7/2015 17:04: Code for associating the default text editor modified

UPDATE 21/7/2015 16:15: Added a new command option for referencing the default text editor

UPDATE 7/4/2016 8:47: Updated layout

Advertisements

Drop some words

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s