The IDE that I use is Sublime Text 3.

If I want to open a file through a terminal with Sublime Text, I have to type:

subl <file>.c

I want to edit my .bashrc so that any .c file automatically opens in Sublime Text. How do I do this?


> test.c

test.c now opens in Sublime Text.

4 Answers 4


This won't work under normal circumstances. Only way I could think of is logging each command to a file (e.g. with script and a modified PS1 for bash to rewrite is at any command) and then parse this log file at each new line (e.g. further modify PS1) for some regex ending in .c to start sublime.

Does not sound like it would be worth the time, maybe some alias s="subl " in .bashrc would make you more happy, combined with tab completion it should be a bit easier for you already.

  • tab completion, command history and emacs-like line editing - these are things I wish I know earlier
    – Alex Vong
    Jul 8, 2017 at 4:50

Since your goal is (likely) to reduce typing, you could define an alias or a simple shell script for sublime,

alias s='subl'
alias sb='subl'

Or you could get fancy, and use a shell script named 's', or 'sb' (save in your ~/bin/ in your PATH),

subl $*

Either one of the above results in a shortened command line,

> s myfile.c

Which is longer than your desired (by 2 characters),

> myfile.c

Or you could get really fancy, and insert a shbang (#!) line as the first line of all of your .c files, make the file executable, and evoke the above edit script. This would mean rewriting your makefiles to first pre-process away the shbang line, prior to compilation.

Save this as 'myfile.cs'

#!~/bin/subl myfile.cs
#include <stdio.h>
int main() { printf("hello, world\n"); }

To demonstrate how this would work, save the following file, 'stuff.cs'

#!/usr/bin/vi stuff.sh
//this is a file
//that you can edit with sublime
int main() { printf("hello, world\n"); }

Then run it,


The latter solution does exactly what you want, but requires considerably more typing, and pre-processing overhead.

  • 2
    I'm glad you didn't suggest putting . in your $PATH after making your source files executable shebang scripts. :P I love the answers on this question. So many creatively horrible ways to abuse the shell to get the asked-for behaviour. Jul 8, 2017 at 10:04
  • :-) "creatively horrible" -- I love it! Jul 10, 2017 at 17:56

You probably should not do this, but one can kluge the command_not_found_handle function into service, which in turn may break other things, or need to be combined with any other command_not_found_handle functionality you may desire.

-bash-4.2$ touch foo.c
-bash-4.2$ function command_not_found_handle() { [[ -f "$1" ]] && subl "$1"; }
-bash-4.2$ foo.c
-bash-4.2$ bar.c

(One reason this is a bad idea is that you are changing the command line grammar of command [args..] to command-or-plainfilethatisrunsomehow [args..].)

  • 1
    opens each console typo in sublime, but still…
    – Jaleks
    Jul 7, 2017 at 21:50
  • 2
    I would add a check whether the file ends in .c or .h, e. g. case "$1" in *.[ch]) [ ! -f "$1" ] || subl "$1";; esac. If you want to be really crafty you could also evaluate the output of mimetype or file --mime-type. Jul 8, 2017 at 9:01

As variant, you can use readline binding, like this:

bind '"\e\C-m":"\C-asubl \C-m"'

Then Alt + Enter or Ctrl + Alt + M combination will open any file in the Sublime editor. That is, when you want open file in the Sublime, you type filename (test.c for example) and press Alt + Enter. Keys combination can be changed as desired.

Drawback - bash autocomplete doesn't work for filenames in the first word of line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .