I am rather new to sublime text but wanted to make a key binding to move the cursor to a specific/constant column, regardless of any previous text entered. This would be used to add comments to code on the far right, or just improving uniformity in coding verilog (or other language).
Basically after any entered text, hit ctrl+1 (or any other key binding) and cursor goes to a specified column adding spaces as filler.
Here is what I have so far.
[{ "keys": ["ctrl+1"],
"command": "run_multiple_commands",
"args": {
"commands": [
{ "command": "move_to", "args": {"to": "eol", "extend": false} },
{ "command": "insert", "args": {"characters": " "} },
{ "command": "insert_snippet", "args": {"contents": "$TM_LINE_INDEX"} },
{ "command": "move", "args": {"by": "words", "forward": false} },
{ "command": "insert", "args": {"characters": "\t\t\t\t"},"context":
[
{ "key": "following_text", "operator": "regex_match", "operand": "40", "match_all": true }
]
},
{ "command": "insert", "args": {"characters": "\t\t\t"},"context":
[
{ "key": "following_text", "operator": "regex_match", "operand": "41", "match_all": true }
]
},
{ "command": "delete_word", "args": { "forward": true } }
]
}
}]
The way I have the code setup is (STEP 3 IS NOT WORKING):
I have a .py script to run multiple commands for a single key binding. Link
Get current column number through a snippet command($TM_LINE_INDEX) and move cursor left of number.
ATTEMPT to use regex to add according number of tabs/spaces. I've only added two cases, but this would be repeated for various cursor column_#'s(locations), not just cursor column locations 40 and 41. Below is the non-working block of code.
{ "command": "insert", "args": {"characters": "\t\t\t\t"},"context":
[
{ "key": "following_text", "operator": "regex_match", "operand": "40", "match_all": true }
]
},
{ "command": "insert", "args": {"characters": "\t\t\t"},"context":
[
{ "key": "following_text", "operator": "regex_match", "operand": "41", "match_all": true }
]
},
Delete the trailing column number snippet (from step 2).
Thanks in advance for any help or comments.
Related
I was trying to add the auto complete feature to sublime for all brackets , inverted commas etc. and escape it using tab but getting parsing error using the following code
// Auto-complete behaviour for ), ], '', ""
// Bind to , to step over the auto-completed character(s)
// -------------------------------------------------------------------------
{ "keys": ["enter"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\\]'\"]", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "[(['\"]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
{ "keys": ["tab"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\\]'\"]", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "[(['\"]", "match_all": true },
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
}
but after that i am getting this as result
If you install package dev for Sublime Text 3 it'll also show you where your errors are with a coloured cursor.
The error is at line 10, character 3, so some line numbers would help with the code you've posted..
I've pasted it into my keymap file (in ST3) and can detect no errors with the code you've posted, is that the entire contents of .sublime-keymap ?
I can also recommend posting your question on the Sublime Text Forum they may be able to help you too.
You could be missing a comma after your final curly bracket, if there's more keybindings in that file i.e. the last line should be:-
},
I am new to C++ and am still trying to set up the C/C++ extension in vscode.
I followed this tutorial provided by vscode official:
https://code.visualstudio.com/docs/cpp/config-clang-mac#_cc-configuration.
With the "lauch.json", "tasks.json", and "c_cpp_properties.json" files completed, I clicked the "play" icon, but my debugger still always show error with ";" and would open the launch.json file.
I think I am encountering the exact same problem with this post but its solution fails to solve mine: Mac VSCode Debugger always show error about ';' and ':'.
This is my code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
vector<string> aaa {"H", "E", "L", "L", "O"};
for (const string& word : aaa)
{
cout << word << " ";
}
cout << endl;
}
This is my launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
This is my tasks.json file:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
This is my c_cpp_properties.json file:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Thanks!
The problem is that the program field is not right in launch.json, if your CPP name is your-program.cpp, you could set the program field to your-program.
Because the compiling output file name is "${fileDirname}/${fileBasenameNoExtension}" as you set in tasks.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "your-program", // set it to right name.
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
I have a simple HelloWorld.cpp file and I want to run it with the following steps, each is run one after the other as follows.
Compile
Clear Integrated Terminal
Run the produced executable.
Unfortunately I fails to setup the second step (clearing the console window). What is the correct setup?
{
"version": "2.0.0",
"tasks": [
{
"label": "COMPILE",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${fileBasenameNoExtension}.exe",
"${file}",
"/std:c++latest"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
]
},
{
"label": "CLEAN",
"command": "cmd.exe /c cls",
"dependsOn": "COMPILE"
},
{
"label": "RUN",
"command": "${fileBasenameNoExtension}.exe",
"dependsOn": "CLEAN",
"options": {
"cwd": "${fileDirname}"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
there is a vscode setting with workbench.action.terminal.clear you'll find related settings there,
or in File >Preferences >Keyboard shortcuts. search for Terminal:clear and assign the key
Aha. I found the solution: Add "type": "shell" to the CLEAN step as follows.
{
"type": "shell",
"label": "CLEAN",
"command": "cls",
"dependsOn": "COMPILE"
}
The complete tasks.json.
{
"version": "2.0.0",
"tasks": [
{
"label": "COMPILE",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${fileBasenameNoExtension}.exe",
"${file}",
"/std:c++latest"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
]
},
{
"type": "shell",
"label": "CLEAN",
"command": "cls",
"dependsOn": "COMPILE"
},
{
"label": "RUN",
"command": "${fileBasenameNoExtension}.exe",
"dependsOn": "CLEAN",
"options": {
"cwd": "${fileDirname}"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
This is the code im trying to build and execute.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
cout << "Hello World";
return 0;
}
i am able to compile and run it using the commands
directory path:// g++ -g hello.cpp
./a.exe
I now made a tasks.json file so that i don't have to run the above two commands everytime, and the .exe file is also named same as the .cpp file.
This is the error i'm facing:
image for build error in VSCode terminal
If anyone knows a fix for this, please help me out.
Let me know if you need the tasks.json file contents. I'll put them up here.
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ (gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "C:/Users/H.P/.vscode/CC++_gcc_Compiler/bin/gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/Users/H.P/.vscode/CC++_gcc_Compiler/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Try these Its working fine on my system
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile and run",
"type": "shell",
"command": "cls",
"args": [
";",
"g++",
"-g",
"${file}",
"-o",
"ans.exe",
";",
// "cls",
// ";",
"./ans.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.):(\\d+):(\\d+):\\s+(warning|error):\\s+(.)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
]
}
and Launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/ans.exe",
"args": [],
"stopAtEntry": false,
"externalConsole":true,
"cwd": "${workspaceFolder}",
"environment": [],
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
I want to build and run c++ source code. And then delete all except source code.
{
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"&",
"${fileDirname}/${fileBasenameNoExtension}",
"&",
"rm ",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
I except they build and run and then delete worthless files, but it is not working.
Taking a look at VScode tasks parameters, you see that we can chain them with dependsOn. Note that the required command can contain the full path to where your tool is located when it's not included in your OS path variable, but options.cwd should contain the path to where your tool should run, i.e. ${fileDirname}. Also you should study the presentation parameter and adjust it for the way you want to see errors.
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"presentation": {
"reveal": "silent",
"panel": "shared"
},
"options": {
"cwd": "${fileDirname}"
},
},
{
"label": "cpp-run",
"type": "process",
"command": "${fileDirname}/${fileBasenameNoExtension}",
"dependsOn": [
"g++ build active file"
],
"presentation": {
"reveal": "always",
"panel": "shared"
},
"options": {
"cwd": "${fileDirname}"
},
},
{
"label": "cpp-test",
"type": "process",
"command": "rm",
"args": [
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": [
"cpp-run"
],
"presentation": {
"reveal": "never",
"panel": "shared"
},
"options": {
"cwd": "${fileDirname}"
},
}
],
}