Sublime Forum

How to open multiple files each in their own tab?

#1

I want to be able to open multiple files, each in it’s own tab, from command line, on Linux. I am fine with each file in it’s own tabl but in same window, or independent tabs.

For example: subl file1.txt file2.txt file3.txt

If possible, how can I do this?

0 Likes

#2

Does that command not do what you want?

0 Likes

#3

Actually it does (mostly)… I have used that command before but forgotten it DOES open the files in separate tabs… It would be weird if sublime didn’t… (Been a long week).
What I was thinking when I posted, is that I very very often want to open 2 or 3 files for a side-by-side comparison, and want them to be opened in there own tabs, but with a 2 or 3 column layout. (BTW I enjoy you YT vids, and have several and several playlists save)

0 Likes

#4

Ahh; something like that is possible out of the box, but takes a bit of work. The subl helper just hands off to the running Sublime instance to tell it that it should open the file, but there is no arguments you can provide to specify the layout as the files are opening.

That said, subl can tell Sublime to execute a command, which does make this possible from the command line (although there is a caveat, see below).

The following is an example of a bash shell script that does this; if you’re using windows or powershell, presumably similar is also possible there, although I don’t have any big knowledge on scripting for either of those two languages.

The script takes advantage of the fact that the set_layout command can be used to adjust the layout of a Sublime window, and the focus_group command changes the input focus to the specified group (column in this case) in the layout, which controls where files are opened.

#!/bin/bash

# Expect exactly 3 arguments to be provided; get mad otherwise
if [ "$#" -ne 3 ]; then
    echo "Must specify exactly 3 files"
    exit 1
fi

# If Sublime is not currently running, then launch it and wait a moment for it
# to finish starting before proceeding.
if ! pgrep -x sublime_text > /dev/null
then
    subl
    sleep 0.5s
fi

# Create a new Sublime Window with three columns; without the --new-window, this
# would alter the layout of the most recently accessed window
subl --new-window --command "set_layout {\"cells\": [[0, 0, 1, 1], [1, 0, 2, 1], [2, 0, 3, 1]], \"cols\": [0.0, 0.33, 0.66, 1.0], \"rows\": [0.0, 1.0]}"

# This variation alters the window layout to be 2 columns instead
# subl --new-window --command "set_layout {\"cells\": [[0, 0, 1, 1], [1, 0, 2, 1]], \"cols\": [0.0, 0.5, 1.0], \"rows\": [0.0, 1.0]}"

# Focus the first group and open the first file
subl --command "focus_group {\"group\": 0}"
subl "$1"

# Focus the second group and open the second file
subl --command "focus_group {\"group\": 1}"
subl "$2"

# Focus the third group and open the third file
subl --command "focus_group {\"group\": 2}"
subl "$3"

The caveat to this is that subl passes the file/command to the running Sublime instance, and then terminates. If Sublime is not already running, then it starts it first. In that latter case, if Sublime is not running, you can’t --command because the command will be delivered before the plugin system is ready to respond to it.

The sample above detects if sublime_text is already running or not, and starts it first if it’s not (if you happen to be on Linux anyway; your mileage on MacOS may vary and I have no idea how you would pull that off on windows). In a pinch, as long as you are aware of the issue you can just skip that part and make sure that Sublime is running first.

This is just one way to go about this; you could also do something like subl -n 1 2 3 followed by subl --command my_plugin_command to execute a custom plugin command that is smart enough to alter the layout based on the number of files, or etc.

Sometimes simpler is easier though. :slight_smile:

1 Like