简体   繁体   中英

How do I make a command in cygwin to run Sublime Text?

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?

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM