I've been playing a bit with ST2 and it seems like a pretty cute editor. Unfortunatelly, its documentation is horrible.
And I'm being nice. So here's my question.
I have five files in a directory, which I usually build via a .bat file with
ifort file1.f90 file2.f90 file3.f90 ...
how can I define and execute this line on windows cmd (taking account the enviromental variables like PATH) from ST2 via a shortcut and see the output? Is something like that even possible at this stage with ST2?
I just made a new build like this:
{
"cmd": ["$file"],
"selector": "source.dosbatch"
}
Then you would put your ifort ... string in a .bat file and "build" that.
Here's a step by step way to run a batch file as part of your build process:
In Sublime Text 2 go to Tools -> Build System -> New Build System
You'll be presented with a new text file with the following code in it:
{
"cmd": ["make"]
}
Now change the "make" to the exact path of your batch file so it looks something like this:
{
"cmd": ["D:\\xampp\\htdocs\\myproject\\dostuff.bat"]
}
Notice you must use double backslash for windows paths.
Then save this file out as myproject.sublime-build in the default directory it asks you to save it in (should be $HOME_DIR/AppData/Roaming/Sublime Text 2/Packages/User).
Then in your project go back to Tools -> Build System and select the build system that is the same name as the file you just created (in this case it's "myproject").
Hit ctrl + b or go to tools -> build and your batch file should run and output will appear in the ST2 console.
This worked for me and works with paths and files with whitespaces.
I have fixed a bug which I've posted on the ST forum here and this here includes that fix.
Paste this into your Batch.sublime-build file.
This will run cmd.exe and run the code in its native console. This will accept your inputs of the batch file.
{
"file_patterns": ["*.bat", "*.cmd"],
"selector": "source.batch",
// This runs the batch file in the cmd window.
"shell_cmd": "start \"${file_name}\" call \"${file}\""
}
Here's a build that can be saved as BatchStConsole.sublime-build
This will run the code in Sublime Texts' console. This will not accept your inputs of the batch file. But still useful for debugging as it passes any arguments like the native CLI but just no interaction.
{
"file_patterns": ["*.bat", "*.cmd"],
"selector": "source.Batch",
// This outputs to Sublime Texts' console
"shell_cmd": "\"${file}\""
}
Also, in a new file ...\Data\Packages\User\Batch File.sublime-settings you can then place this code and save. This will build these filetypes when you have automatic build as your build detection.
{ "extensions": [ "bat", "cmd" ] }
Relevant help:
https://ss64.com/nt/start.html
https://docs.sublimetext.io/guide/usage/build-systems.html
https://www.sublimetext.com/docs/3/build_systems.html
Related
I'm trying to set up Chrome as a build system in Sublime Text on MacOs.
Tried using the New build menu option and plugged in:
{
"/Applications/Google Chrome.app": "Chrome"
}
It appears in list after I restart as build option but doesn't work. Any ideas?
For reference, the build system documentation has lots of examples if you want to add more functionality. However, for now, we'll just make a simple build system with one command - open the current file in Chrome.
Each build system needs to have a "cmd" key - the command you want to run - and optionally a "selector" key - a rule for selecting which types of files you want to run. For now, we'll just assume you only want to run HTML files. Select Tools → Build System → New Build System… and replace the contens with this:
{
"cmd": ["/Applications/Google Chrome.app", "$file"],
"selector": "text.html"
}
Next, hit Save and save the file in the suggested folder with a name like Chrome.sublime-build. You don't need to restart for it to be visible in the Build System menu.
Now, when you want to view an HTML file in Chrome, you'll first select Tools → Build System → Chrome, then hit ⌘B to actually run the build. Chrome will stay selected as the current build system, so if you want to build again all you have to do is hit ⌘B.
If you do any work on Windows or Linux, the build command shortcut is CtrlB.
Visual Studio Code 1.46.1
Operating system: MacOS 10.15.4 (Catalina)
VS Code Package used: C/C++, Code Runner
Problem: When I run a C++ code using Code Runner, a file with no extension automatically appear.
I would like to know what does this 'useless file' do, and how to make these files stop from spawning.
That is the executable file also called binary file. They are the executable like .exe in windows so it is very useful(as it is the output of source code). If you dont want to see that file in sidebar of vscode then there is a good answer to this question. Alternatively you can configure your build configuration file (located at .vscode in you working directory) and set the build path to somewhere else
You are using Code Runner extension on vscode then you can modify the execution command.
Go to Code-runner: Executor Map Setting
I did this:
"code-runner.executorMap": {
...
"c": "cd $dir && gcc $fileName -o ./temp/$fileNameWithoutExt && $dir./temp/$fileNameWithoutExt",
...
}
create a temp folder.
Now your temp folder will contain those unwanted files.
You can modify execution command like this for c++ also.
So I have started to use Sublime Text 3 recently with my Ubuntu OS. I wanted to test it out so wrote a simple piece of c++ code. But when I try to build it does nothing, I have checked online and still nothing I even installed a build system (https://github.com/shikharkunal99/Sublime-Build-System) and still whenever I go to build it just opens open a black section at the bottom (see picture)
Install g++ to run c++ code
apt-get install g++
Then I will tell you a personal trick that I used. it is:
find | grep "part of your filename"
Replace "part of your filename" section with the name of the file or a part of the name of the file.
Suppose, the file name is Here.c. I type "Here" in place of part of your filename.
Then the final step, type
./a.out
Output is ready in front of you.
This post will help you in setting up Sublime Text 3 in a way that leads to a good workflow specifically for C++ programming environment (Ubuntu, GNU C++ Compiler) :
Note: Only the following step is essential for running c++ programs.
1. Create a Build System in Sublime Editor :
Sublime Text provides build systems to allow users to run external programs.
Go to Tools -> Build System -> New Build System.
Paste the following code in the file
{
"cmd": ["g++ -Wall -Wextra -O2 -pthread -H -std=c++17 \"${file}\" -o runfile && ./runfile <input.in> output.out"],
//above line works fine if input.in and output.out files are present in same directory in which .cpp file is present else add complete address of these files for using them as common input output files in your system.
"shell":true,
"working_dir":"$file_path",
"selector":"source.c,source.c++,source.cpp",
"variants": [
{
"name": "Variant 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\""
],
}
]
}
Save the file (By default the file is placed in "~/.config/sublime-text-3/Packages/User" dir) something like "C++17.sublime-build" to differentiate it from the other build system files.
Create input.in and output.out text files in your working directory. This can be used for piping input from the input.in file, and output to the output.out file.
Note in the first line it uses the -std=c++17 flag to enable the latest features of C++17. If you don't want this or want to use C++14, replace this with the -std=c++14 flag.
Refer to https://linux.die.net/man/1/g++ for different compiler flags.
See Also https://discuss.codechef.com/t/are-any-compiler-flags-set-on-the-online-judge/1866
2. Setup window layout :
Create three new c++ file, file.cpp. Select View > Layout > Columns : 3. This will create three columns in the workspace. Select View > Groups > Max Columns : 2.
Write a hello world program & save inputs if any in the input.in file, and test its working. Use Shift+Ctrl+B and Select C++17 to build and execute the file (If selected C++17 - Variant Run it will execute the program in a separate terminal window like a normal program would).
The windows will look like this when you are done.
Layout Preview
3. Precompile headers :
Generally useful in competitive programming, we can speed up compilation time by precompiling all the header files as mentioned here, i.e. by precompiling the bits/stdc++.h header file.
For this, first, navigate to the stdc++.h file. This will be located at a directory similar to ~/usr/include/x86_64-linux-gnu/c++/9/bits Open terminal window here.
Run the command sudo g++ -std=c++17 stdc++.h, to compile the header. Take care to use the same flags you used in your build system. Check to make sure that the stdc++.h.gch file was created in that directory.
4. Sublime Text features :
Snippets & Completion
Read up on the documentation of snippets and completions at the official guide.
5. Other Features :
Read https://scotch.io/bar-talk/best-of-sublime-text-3-features-plugins-and-settings
This program works perfectly fine for me using Build 3120, and I expect it will work fine with previous builds. First, you need to select Tools → Build System → C++ Single File (Tools → Build System → Automatic should also work, but I prefer to be explicit). Then, either hit CtrlShiftB or select Tools → Build With… and select C++ Single File - Run. This will compile your .cpp file to an executable in the same directory as the source file, then run it.
Well I also got various issues with this thing finally I got an amazing thing in the package control pallet.Follow the instructions:
1.Open up the Package control Pallet
2.Search for C++ Builder
3.You will see C++ Builder-Mingyang Yang
4.click it and then wait for a couple of seconds
5.finally go to tools->build system->select C++ Builder-Mingyang Yang
6.finally tap the Shift+Ctrl+B and then select C++ Builder-Mingyang Yang Build and Run
7.finally here you go you can not only build this but also use the console for input
Note:This will execute only when there is gcc compiler included in the terminal otherwise at first install gcc by the command apt-get install gcc then you can use c++
I really prefer writing code in sublime text or anything else. So, naturally that's what I want to use. However, when I try to open the file in Netbeans, I get an error. So, I want to know how I can save a .cpp file from sublime text and then go about running it through the command prompt. I know I have to set up a path or something, but I'm not exactly sure how to do it. Thanks for any help at all. Also, I am new to C++ and programming in general(have dabbled in Python a bit).
EDIT: Really sorry, I meant how do I actually execute/run the file afterwards. Like if the program were to just print out "Hello World".
The following build system should suit your needs, assuming that you're using the GNU Compiler Collection and g++ for compiling your .cpp files:
{
"cmd": ["g++", "${file}", "-o", "${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_base_name}"]
}
]
}
Please note that the following instructions are for Sublime Text 2 only...
To use it, select Preferences -> Browse Packages... to open the Packages folder in Windows Explorer. It should be located in C:\Users\YourUserName\AppData\Roaming\Sublime Text 2. Or not, depending on your install. In either case, browse to the C++ directory and open the file C++.sublime-build in Sublime and set the syntax to JSON if you want it to look prettier. Replace its entire contents with the code above, then save the file. The old code is kind of convoluted, and also runs some commands needlessly.
Now, set the build system by going to Tools -> Build System and selecting Automatic. Assuming that g++ is in your PATH, you can build your executable using the CtrlB keyboard shortcut, also available via Tools -> Build. If your binary has already been compiled, you can run it by pressing CtrlShiftB.
One final note: if your program asks for any kind of input, or has a graphical user interface, this Run command won't work. Instead, replace it with the following:
"name": "Run",
"cmd": ["start", "cmd", "/k", "${file_path}/${file_base_name}"],
"shell": true
This will open a new instance of the command line and run your program from there, instead of inside Sublime. The /k switch means that the window will be kept open after your program has run, so you can examine output, errors, etc. If instead you want the window to close immediately, simply change the /k to /c.
Good luck!
I created file /Users/maks/Library/Application Support/Sublime Text 3/Packages/User/CoffeeScript.sublime-build for automatic compile coffee to js when I press super B on currently opened .coffee file:
{
"cmd": ["coffee", "-c", "$file"],
"selector" : "source.coffee",
"path" : "/usr/local/bin"
}
It works fine. But the problem is that it try to compile every file, on which I press super B, even if it is not .coffee. How to add filter and restrict it compile files which are not *.coffee.
So I want it to work as following: when I press super B on a file: if it is coffee file it should compile it to js else just save file.
Have a look at Tools > Build System and try enabling/disabling "Automatic"