Open Visual Studio solution from Git Bash

tools

Open Visual Studio solution from Git Bash

Update (2018/03/10)

I’ve been using Visual Studio Preview recently and my original solution no longer works with my workflow. Instead I’ve created another function and change from using the magic start command to the devenv.exe that comes with Visual Studio. Below is what I now have in my .bashrc.

vs() {
  FILES_LIST="$(ls *.sln 2>/dev/null)"
  for file in $FILES_LIST; do
    start /c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2017/Professional/Common7/IDE/devenv $file
  done
}

vsp() {
  FILES_LIST="$(ls *.sln 2>/dev/null)"
  for file in $FILES_LIST; do
    start /c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/Preview/Professional/Common7/IDE/devenv $file
  done
}

For those not great at shell scripting (like me), the start at the beginning of the line calling devenv runs the application in the background so my shell returns the prompt to me to continue working while I have Visual Studio open. I can also run either vs or vsp (for the Preview version) on my .sln files.

Original Post

I’ve been working from the command line a lot more since I started using Git. I have also recently started creating aliases for both Git ( .gitconfig) and Git Bash ( .bashrc). One alias I had been thinking about adding recently was the ability to open a Visual Studio .sln file from the Git BASH. Yes, I could always type explorer . to open up windows explorer and then double click the .sln file. But that seems to be working against my new love of using the command line. I did some digging on stackoverflow.com for shell scripts that might match what I’m trying to do. My shell scripting skills are near 0 but I can usually interpret one if I read it. Here is what I came up with

// Previously added aliases for BASH
alias npp='notepad++.exe $*'
alias ll="ls -alh"

// New 'vs' function
vs() {
  FILES_LIST="$(ls *.sln 2>/dev/null)"
 for file in $FILES_LIST; do
            start $file
  done
}

The vs() function does all the work and vs is what I type on the command line. This function finds .sln files in the current directory and then for each one calls the start command on it, which is already in my PATH as the Visual Studio ‘open this solution file’ command. Most of my projects have just one .sln file in each directory, so there is probably a better block of shell script to find the first .sln file and run the start command with it.