Python How to open a .py file in sublime from a terminal console?

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    File
Click For Summary
To open a Python file like hello.py in Sublime Text from the terminal, the correct command is "subl hello.py" rather than "sublime hello.py." This follows the general Unix convention of using the program name followed by the filename. On macOS, users can also utilize the "open" command to open files with a specified application, such as "open -a /Applications/MyFavoriteTextEditor.app <filename>." Additionally, creating an alias for frequently used commands can streamline the process, allowing users to define shortcuts for opening files with their preferred text editor.
member 428835
Hi PF!

To open hello.py in the past I've used gedit hello.py but this opens the file in text editor. I would prefer to use sublime since it has features that are appealing to me. So my question is, how do I open hello.py in sublime from the terminal?
 
Technology news on Phys.org
sublime hello.py doesn't work?
 
  • Like
Likes member 428835 and phyzguy
PeterDonis said:
sublime hello.py doesn't work?
Didn't realize it was this simple; thanks! (it's actually subl hello.py, but thanks)
 
joshmccraney said:
Didn't realize it was this simple

On Unix-type systems it's a pretty general pattern that <program-name> <filename> at the shell opens the program operating on that file. Of course you still get curve balls like the program name being "subl" instead of "sublime". :wink:
 
  • Like
Likes member 428835
Got it! Fortunately tab kinda takes care of this :)
 
On mac machine you can say:
Bash:
$$ open <filename>
and it will select the proper editor or viewer or to be selective:
Bash:
$$ open -a /Applications/MyFavoriteTextEditor.app <filename>
where MyfavoriteTextEditor is a subfolder in the /Applications folder.

I even added an alias like:
Bash:
$$ alias ted=open -a /Applications/MyFavoriteTextEditor.app <filename>

$$ ted <filename>
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
10K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
Replies
35
Views
6K
Replies
1
Views
5K