4

I'm trying to mimic the subl command in iterm for mac computers in cygwin.

Basically, I want to be able to open a current folder from cygwin by typing subl .

I haven't found any good instructions. I know where my .bashrc file is located. I just dont know what to do to create the command subl and make it so that the path following subl opens with Sublime.

Can anyone help?

Daniel Breen
  • 179
  • 1
  • 2
  • 11

4 Answers4

4

You'd want to make an alias and source it from bashrc.

Example

Create a file ~/.bash_aliases with:

alias subl='/cygdrive/c/sublime.exe' #make sure this command is correct for you

Now in ~/.bashrc do:

source ~/.bash_aliases    

Log out and log back in, and subl . should work

Marc Young
  • 3,854
  • 3
  • 18
  • 22
  • Thanks! That works perfectly. Before you answered, I found a second way to do it by creating a file via file explorer named subl and writing a script inside of that. Your answer seems to load sublime up a lot quicker though. Thanks again! – Daniel Breen Sep 16 '15 at 05:38
  • 1
    Follow up: This works but keep cygwin locked up until I exit sublime again. What would be the syntax to keep this from happening? In the other way, I simply added a `$1 &`. I'm not sure how to accomplish it using the .bash_aliases because the quotation marks were unnecessary in the other example. – Daniel Breen Sep 16 '15 at 06:10
  • I'm not sure if it would work for cygwin bash, but I would launch it as a nohup: $ nohup /cygdrive/c/sublime.exe & – Marc Young Sep 16 '15 at 13:03
  • `nohup` is probably the better option, but you can make the `&` part of the alias if you want. Just add the `&` at the end, before the last quote – pak Sep 17 '15 at 02:17
2

Assuming you want correct behaviour when doing subl ~, a simple alias or adding Sublime Text to your PATH will not work.

You can use the following bash function instead (put it in your .bashrc):

function subl {
    cygpath --windows $@ | sed 's/\\/\\\\/g' | tr '\n' '\0' | xargs -0 -n1 /cygdrive/c/Program\ Files/Sublime\ Text\ 3/subl.exe
}

Note that when passing paths to xargs you need to 1) escape the backslashes, 2) use the NUL character as argument delimiter for it to work with paths with spaces.

Edit: Use subl.exe rather than sublime_text3.exe so that it would detach itself from the terminal.

Alexander Revo
  • 719
  • 9
  • 15
1

To open files and use Git tools (commit, rebase, tag, ... messages):

#!/bin/bash
# To create in [.babun/]cygwin/usr/local/bin/subl with chmod +x

ARGS=""
while test $# -gt 0
do
  ARGS="$ARGS ${1#/cygdrive/[a-zA-Z]}"; # Remove /cygdrive and disk letter from the path
  shift
done

/cygdrive/c/Program\ Files/Sublime\ Text\ 3/subl.exe $ARGS

https://gist.github.com/cmalard/16c58869319c9a88473ec08cc7989c6b

Cédric M.
  • 113
  • 1
  • 6
0

You can also simply add Sublime to your PATH variable. For cygwin, you can:

  1. Go to your home directory and verify .bash_profile exists with ls -a
  2. Open .bash_profile with something like vim .bash_profile
  3. add the line export PATH=$PATH:"/cygdrive/c/Program Files/Sublime Text 3" (or whatever top level installation folder Sublime is in)
  4. Reopen your terminal and use subl to verify it works
antonig
  • 153
  • 1
  • 10