12

I am using Ubuntu 12.04, and I was wondering, is it possible to automatically run c++ program from terminal? It really sucks when you have to use build in console because sometimes I make infinite loops by accident and have to restart sublime text to work again. I am using Sublime text 3.

user2648841
  • 247
  • 1
  • 4
  • 9
  • What do you mean by terminal? I suspect (strongly) that the answer is yes, but I'm not certain what you mean. – Elliott Frisch Jan 17 '14 at 21:22
  • By terminal, I mean command-line interface terminal, I don't know how to explain it more clearly. Here is the picture: http://i.stack.imgur.com/8GaCZ.png – user2648841 Jan 17 '14 at 21:26
  • Yes then. You can execute your build with [Build automation](http://en.wikipedia.org/wiki/Build_automation) or just run make or cmake (depending on how you build). – Elliott Frisch Jan 17 '14 at 21:27
  • You shouldn't have to shutdown SublimeText if your program's in an infinite loop. Can't you just `killall ` in a terminal window? – Praetorian Jan 17 '14 at 22:00

8 Answers8

25

Sublime Text 3 includes two build systems you might be interested in: C++ and Make. The C++.sublime-build file is as follows:

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
        }
    ]
}

To use it, go to Tools -> Build System and select C++. You can now use CtrlB to run the build (top command), or CtrlShiftB to run the Run variant.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Where do I find the C++.sublime-build file on windows? – Calle Bergström Dec 24 '15 at 12:01
  • @CalleBergström you'll need to use the [`PackageResourceViewer`](https://packagecontrol.io/packages/PackageResourceViewer) plugin. See my answer [here](http://stackoverflow.com/a/20768678/1426065) for directions on use. However, instead of `Extract Package`, choose `Open Resource`, then `C++`, then `C++.sublime-build` (it may also be named `C++ Single File.sublime-build`, depending on which version of ST3 you have). – MattDMo Dec 24 '15 at 16:15
  • This will not run the program in a terminal. The OP asks how to run the program in a terminal. – jdhao May 26 '18 at 14:37
9
{
  "cmd": ["g++", "$file", "-o", "${file_path}/${file_base_name}"],
  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
  "working_dir": "${file_path}",
  "selector": "source.c, source.c++, source.cxx, source.cpp",
  "variants":
  [
      {
          "name": "Run",
          "shell": true,
          "cmd": ["gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};echo;echo;  echo Press ENTER to continue; read line;exit; exec bash\"'"]
      }
  ]    
}

It can run in terminal and input data from keyboard

Adamq
  • 29
  • 2
  • 7
Scalpel
  • 91
  • 1
  • 3
3

I think the accepted answer does not achieve what the OP want to achieve. The OP wanted to know how to execute the current file in a terminal.

@Flycode's setting does not work for me. I am using CentOS 7 with Sublime Text 3. Since people may use different terminal emulators, so I list different settings for different terminals.

Note

The following settings are tested on the above environment and works well. I can not guarantee that they will work on other environments. Let me know if it does not work for you.

Option 1: GNOME Terminal

You can use the following setting,

{
    "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "shell": true,
    "working_dir": "${file_path}",
    "selector": "source.c++, source.cxx, source.cpp, source.cc",

    "variants":
    [
        {
            "name": "Run",
          "shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};exec bash \"'",
        }
    ]
}

gnome-terminal will automatically close the execution window, the above command

   "shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};exec bash \"'" 

is used that way to make sure we can see the execution result. See this SO post for a detailed discussion about how to prevent gnome-terminal from closing automatically.

Option 2: XTerm

You can use the following setting (For brevity, I leave out some settings)

{    // same stuff as option 1
    "variants":
    [
        {
           "name": "Run",
            //use this if you want to input other command after programm execution
           "shell_cmd": "xterm -e '${file_path}/${file_base_name}; bash'",
           //or you can use the below setting if you just want to execute this program
           // "shell_cmd": "xterm -hold -e ${file_path}/${file_base_name}",

        }
    ]
}

See this SO post about preventing xterm window from closing automatically.

Option 3: Konsole

You can use the following setting,

{    // same stuff as option 1
        "variants":
        [
            {
                "name": "Run",
                "shell_cmd": "konsole --hold -e ${file_path}/./${file_base_name}",        
            }
        ]
}

See here and here on discussion to hold konsole windows after excuting the program.

jdhao
  • 24,001
  • 18
  • 134
  • 273
2

Tools >> Build System >> New Build System Then paste this And press Ctrl+S to Save file. Now Goto Tools >> Build System >> Select your file >> Now write your code >> Press Ctrl+B >> Select Run in Terminal for Build and run your code

{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"shell": true,
"working_dir": "${file_path}",
"selector": "source.c++, source.cpp, source.cc, source.cxx",
"variants":
[
    {
        "name": "Run in Terminal",
        "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -e 'bash -c \"${file_path}/${file_base_name}&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal    
    }
]

}

HimEl
  • 66
  • 5
2

Through this build you can directly run you C/C++ programmes from subime by pressing ctrl+shift+B.

It allows user input during run time.

It also helps in debugging by showing errors on terminal window as your terminal shows you when you run directly through it.

{ 
   "cmd": "g++ \"${file}\" -o \"${file_path}\\\\${file_base_name}\"",
   "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
   "working_dir": "${file_path}", 
   "selector": "source.c,source.c++,source.cpp",
   "shell":true,
   "variants": [
   { 
       "name": "Run",
        "cmd" : ["gnome-terminal -- bash -c \"g++ $file_name ;echo -------------output--------------; ./a.out;echo;echo;  echo Press ENTER to continue; read line;exit; exec bash\""
     ],
   }
 ]
}
akhil
  • 41
  • 3
0

On Mac, I use fswatch (I'm sure there's something like it on Linux) to automatically build & run the testcase on save.

Eugene
  • 9,242
  • 2
  • 30
  • 29
0

Here is my configuration to compile and run C++ programs. Program takes input from file 'input.txt' and prints output to 'output.txt'.Both the files present in current working directory.
OS: ubuntu 16
sublime 3
-> "Tools > Build System > new Build System" and copy following setting

{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" ",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"shell": true,
"working_dir": "${file_path}",
"selector": "source.c++, source.cxx, source.cpp, source.cc",

"variants":
[
    {
        "name": "Run",
      "shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name} < input.txt > output.txt \"'",
    }
] }
snehm
  • 223
  • 3
  • 13
0

In directory Tools >> Build System >> New Build System. create new file. Now input can also be give.

{

"cmd": ["g++", "-Wall", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",

"variants":
[
    {
        "name": "Run",
        "cmd": ["x-terminal-emulator", "-e", "bash", "-c", "g++ -Wall -std=c++11 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'; read -p 'Press any key to continue...'"]
    }
]

}