Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime text 3 - compile program and run in terminal

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.

like image 376
user2648841 Avatar asked Jan 17 '14 21:01

user2648841


People also ask

How do I compile Sublime Text in terminal?

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

How do I run code directly in Sublime Text?

To run code in Sublime Text, go to Tools > Build System, and select the language for your code (Sublime comes with support for various languages like Python, Ruby, Bash, and more). Next, press Cmd+B on Mac or Ctrl+B on Windows to run your code.


3 Answers

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.

like image 56
MattDMo Avatar answered Nov 11 '22 13:11

MattDMo


{
  "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

like image 20
Scalpel Avatar answered Nov 11 '22 11:11

Scalpel


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.

like image 23
jdhao Avatar answered Nov 11 '22 13:11

jdhao