Sublime Text 4: open multiple windows from terminal - sublimetext

Sublime Text 4 was just released. A behavior change from Sublime Text 3/2 has been implemented. In new Sublime Text 4, if a window is already open when the User (me) uses the $ subl . command from the terminal, it takes me to the currently open Sublime Text window. This is not the behavior I want.
Previously in Sublime 3/2, subsequent use of $ subl . command would open a new Sublime Text window in this directory (I would now have 2 separate windows, both in this directory with all of this projects sub-directories in the sidebar).
How do I get Sublime Text 4 to open Additional Windows from the terminal? I would like to open multiple windows in the same project from the terminal.
Thank you.

The command line argument -n or --new-window tells subl to create a new window.
λ subl --help
Sublime Text build 4106
Usage: subl [arguments] [files] Edit the given files
or: subl [arguments] [directories] Open the given directories
or: subl [arguments] -- [files] Edit files that may start with '-'
or: subl [arguments] - Edit stdin
Arguments:
--project <project>: Load the given project
--command <command>: Run the given command
-n or --new-window: Open a new window
--launch-or-new-window: Only open a new window if the application is open
-a or --add: Add folders to the current window
-w or --wait: Wait for the files to be closed before returning
-b or --background: Don't activate the application
-s or --stay: Keep the application activated after closing the file
--safe-mode: Launch using a sandboxed (clean) environment
-h or --help: Show help (this message) and exit
-v or --version: Show version and exit
Filenames may be given a :line or :line:column suffix to open at a specific
location.

Related

How to create a file from the Mac OS terminal and open it in sublime?

How to create a file from the Mac OS terminal and open it in sublime? But not in a new sublime window, but in a new tab, in an already open sublime?
Example:
touch filename.js -a 'Sublime Text' $_
Is there any flag or argument that can be added here?
All you need to do (assuming you have the subl helper script in your $PATH) is type subl filename.ext and Sublime Text will open it in the currently-active window. You don't need to call touch at all.
Note: this will work on any OS.

Can't open sublime in Bash on Ubuntu on WIndows

I would like to be able to open the sublime text editor from the command line from Bash on Ubuntu on Windows, but each time I attempt to run the command 'subl' I get hit with
-bash: C:/Program Files/Sublime Text 3/sublime_text.exe: No such file or directory
This is definitely the right directory, but I have no clue as to why this isn't working. Any tips?
Run notepad as administrator.
Copy & paste this two line of code below:
#!/bin/bash
"$(dirname "$0")/subl.exe" "$#"
Save it as subl.txt inside C:\Program Files\Sublime Text 3
Open the folder 'C:\Program Files\Sublime Text 3'
Finally right click and rename this file to subl. That means remove the .txt extension from that file.
Now, you are ready to use subl command on gitbash or wsl linux terminal.

Unix Shell .bash_profile script open two files in sublime text in same window

So I have the following code in my .bash_profile file. This code allows me to use the sub command followed by the folder/filename to open that folder/file in sublime text! Thats awesome!
However I'm trying to open the .bash and .bash_profile files in sublime text using the editbashpro command as defined by the alias... however it opens BOTH files in SEPARATE windows.
I do NOT want to fix this issue by changing settings in Sublime Text itself, which it seems all of the tutorials out there are directing me towards this...
Is there any way to open both .bash and .bash_profile in the same window in sublime using only the terminal/bash_profile script? I'm extremely new to the terminal and using shell scripting? idk what its even called? see how new i am?
# Sublime Text Open File/Folder Command
function sub() {
open $1 -a "Sublime Text"
}
echo 'sub <file/folder> ->
open file/folder in sublime
'
# Edit Bash Profiles Command
alias editbashpro='cd ~/;
sub .bash_profile;
sub .bashrc;'
echo 'editbashpro ->
opens .bashrc and .bash_profile in Sublime Text Editor'

using Sublime Text 3 cli from within another service to open an new window creates a temp file

I've been setting up a Karabiner (Launcher Mode) shortcut to open a new Sublime Text window.
Normally I call subl -n from a terminal window to bring up a new sublime window. Karabiner lets you specify shell scripts as shortcuts to run, so I tried /usr/local/bin/subl -n
It works, but, whereas calling subl -n from a terminal window (or /usr/local/bin/subl -n) opens a new window with a "blank" file, calling it through Karabiner opens a temp file at tmp/subl stdin YEd3Bc.txt (YEd3Bc is six random numbers/digits) in a new window
It also blocks Karabiner from launching anything else until you close the window, as if I had called subl -n -w "tmp/subl stdin YEd3Bc.txt"
I thought it was a problem with Karabiner, and tried to work around it by using Automator and Applescript to create wrappers for a shell script, but both of them had the same problem, opening temp files at /tmp/subl stdin random.txt
I haven't used that particular software (Karabiner) however it could be opening a temp file due to the user that is launching the command (Karabiner) not having access to the default path to store the temp file there to which it creates a unique named file in the /tmp directory. You could try adding the following su -u "your user without quotes" -c /use/local/bin/subl -n. As for the locking of (Karabiner) that is most likely due to the internals of Karabiner and how it opens a sub process to the subl binary along with sublimes nature of staying at the foreground. You can attempt a & at the end of the command to send it to the background allowing Karabiner the ability to continue processing.

Run / Open VSCode from Mac Terminal

I'd like to run / open Visual Studio Code from the Mac OSX Terminal by running this command code .. I found instructions here:
https://code.visualstudio.com/Docs/setup
Apparently I need to include this in my .bashrc file, so I did, but to no avail.
code () {
if [[ $# = 0 ]]
then
open -a "Visual Studio Code"
else
[[ $1 = /* ]] && F="$1" || F="$PWD/${1#./}"
open -a "Visual Studio Code" --args "$F"
fi
}
I edited the .bashrc file here:
~/.bashrc which points to /Users/username/.bashrc
Which .bashrc should I be editing?
According to the docs on Launching from the command line:
Open Visual Studio Code
Open the command pallette with Command + Shift + P (or F1)
Type Shell in command palette
Select Shell Command: Install code in PATH from suggested list
That's it.
Now open your terminal type.
$ code .
To make this change persist after restart on MacOS
Many Mac users find this is forgotten and needs to be re-applied after any restart. This may happen if MacOS has applied the quarantine attribute to VS Code, which the OS uses for the "Are you sure?" notice applied on first using apps downloaded from the internet.
To check if this attribute is applied, look for com.apple.quarantine in the list returned by this command (changing the path if that's not where you installed it):
xattr "/Applications/Visual Studio Code.app"
If that does return com.apple.quarantine, you can remove the attribute using the same command with the -d flag (alongside -r to recursively remove it from all contained files and sudo to allow the change):
sudo xattr -r -d com.apple.quarantine "/Applications/Visual Studio Code.app"
...then do Shell Command : Install code in PATH as above after the attribute has been removed, and it should persist after restart.
Credit: derflounder.wordpress.com article linked to by RicardoVallejo in this comment.
I just want to pull out Benjamin Pasero's answer from inside his comment as it seems the best solution. It is the tip given on the Setting up Visual Studio Code page where it says ...
If you want to run VS Code from the terminal, append the following to your ~/.bash_profile file (~/.zshrc in case you use zsh).
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
Now, you can simply type code . in any folder to start editing files in that folder. [Or code test.txt to go to work on the test.txt file]
To setup path permanently for mac users;
open ~/.zshrc using the below command
vi ~/.zshrc
Add the following path
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
And source it using below command
source ~/.zshrc
Now close the terminal and reopen and run code . command should work properly.
follow some simple steps :
open your visual studio code (vs code).
press F1.
pallete will open in top center with symbol >
type shell .
select intall 'code' command in PATH.
it will be automatically intalled.
Now you can use from terminal by typing
$ code .
Open VSCode, press Command + Shift + P, type Shell in command palette, Select that option => Install code in PATH from suggested list in command palette.
If you are on Mac OSX Maverick,
it's ~/.bash_profile not ~/.bashrc
Try putting the code in there, close the terminal and then try again. Should be working
For Mac you can do :
View > Command Palette > Shell command > "install code command in path". I'd assume there would be something similar for other OS's. After I do
which code
and it tells me it put it in /usr/local/bin
Sometimes, just adding the shell command doesn't work. We need to check whether visual studio code is available in "Applications" folder or not. That was the case for me.
The moment you download VS code, it stays in "Downloads" folder and terminal doesn't pick up from there. So, I manually moved my VS code to "Applications" folder to access from Terminal.
Step 1: Download VS code, which will give a zipped folder.
Step 2: Run it, which will give a exe kinda file in downloads folder.
Step 3: Move it to "Applications" folder manually.
Step 4: Open VS code, "Command+Shift+P" and run the shell command.
Step 5: Restart the terminal.
Step 6: Typing "Code ." on terminal should work now.
For macOS 12.0 and above:
Open profile in Notepad
open ~/.zshrc
Create an alias for code, Paste below:
alias code='open -a "Visual Studio Code"' # open file or folder in VSCode e.g. code ~/.zshrc
Now you can open the current folder e.g. code . or any other file/folder by providing its path.
Profit
PS: You can add as many aliases as needed to open a file/folder with different editors. Just mention the editor's name in the alias. For example, open file/folder with sublime text:
alias subl='open -a "Sublime Text"' # open file or folder in sublime e.g. subl ~/.zshrc
And use it like subl .
To set up VS code path permanently on Mac OS;
just open .bash_profile using the following command on terminal
open -t .bash_profile
Then add the following path to .bash_profile
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
save the .bash_profile file and quit the terminal. Then reopen the terminal and type code .to open VS code.
Somehow using Raja's approach worked for me only once, after a reboot, it seems gone.
To make it persistent across Mac OS reboot, I added this line into my ~/.zshrc since I'm using zsh:
export PATH=/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin:$PATH
then
source ~/.zshrc
now, I could just do
code .
even after I reboot my Mac.
For Mac users:
One thing that made the accepted answer not work for me is that I didn't drag the vs code package into the applications folder
So you need to drag it to the applications folder then you run the command inside vs code (shown below) as per the official document
Launch VS Code.
Open the Command Palette (⇧⌘P) and type 'shell command' to find the
Shell Command: Install 'code' command in PATH command.
Yo do this:
Launch Visual Studio Code.
Press Cmd ⌘ + Shift ⇧ + P to open the Command Palette.
Type in shell command and select the Shell command: Install ‘code’ command in PATH to install it.
How about a simple Bash alias that you stick in your .bash_profile ?
alias code="open -a /Applications/Visual\ Studio\ Code.app"
To open the current directory:
code .
I just made a symbolic link from the "code" program supplied in the Visual Studio Code.app bundle to /usr/local/bin (a place where I prefer to put stuff like that and which is already in my path on my machine).
You can make a symbolic link using ln -s like this:
ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code /usr/local/bin/code
I follow this step it work for me .😁
first open VSC .
open terminal of VSC.
Press cmd+shift+p
uninstall path .
Give permission to it.
Press cmd+shift+p
install path .
then open Mac terminal navigate to root project file
type
code . in root folder It will open VSC . :)
To set it up, launch VS Code. Then open the Command Palette (⇧⌘P) and type shell command to find the Shell Command: Install 'code' command in PATH command.enter image description here
https://code.visualstudio.com/docs/setup/mac
add below snipped in your bash profile -
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
I moved VS Code from Downloads folder to Applications, and then i was able to run code in the terminal. I guess, it might help you too.
Open Visual Studio Code
Open the command pallette with Command + Shift + P
Type Shell in command palette
Select Shell Command: Install code in PATH from suggested list
Type code . in mac terminal
I simply created a file called code:
#!/bin/bash
open /Applications/Visual\ Studio\ Code.app $1
Make it executable:
$ chmod 755 code
Then put that in /usr/local/bin
$ sudo mv code /usr/local/bin
As long as the file sits someplace that is in your path you can open a file by just typing: code
I prefer to have symlinks in the home directory, in this case at least. Here's how I have things setup:
: cat ~/.bash_profile | grep PATH
# places ~/bin first in PATH
export PATH=~/bin:$PATH
So I symlinked to the VSCode binary like so:
ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code ~/bin/code
Now I can issue code . in whichever directory I desire.
Since, default shell is zsh in macOS, you can try this:
cat << EOF >> ~/.zshrc
# Add Visual Studio Code (code)
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF
This will add a path to your VS Code, restart your terminal and voila, you're good to go.
code example.py
alias code="/Applications/Visual\ Studio\ Code\ 2.app/Contents/Resources/app/bin/code $1"
the alias to the vs code's bin file with parameters works well
you can do code . after having sourced your bash file
open finder and go to applications and make sure that vscode exists there ,then open type in terminal export PATH="/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
Open VSCode
Go to view --> Command Palette
Search for "shell...install 'code'
Open your terminal and place in the directory you wanna open
Use $ code .
code () {
if [[ $# = 0 ]]
then
open -a "Visual Studio Code"
else
echo "Opening: "$#
"/Applications/Visual Studio Code.app/Contents/MacOS/Electron" $#
fi
}
I put that into my .bash_profile I tested it and it works.

Resources