I use SublimeText since a few months with ruby, and I have a issue with comment auto-indentation. Indentation uses the comment's indentation, and indent all the following code using this indentation. I expect auto-indentation to ignore(at least) or set indent of previous code (at best), but not to take comment's indentation at all :
All my colleagues who use this editor have the same issue
Here's a sample code re-indented by SublimeText
class Test
def method1
end
#Bad indentation
def method2
somecode
end
def method3
somecode
end
end
Wanted :
class Test
def method1
end
#Bad indentation
def method2
somecode
end
def method3
somecode
end
end
I did a quickfix on
~/.config/sublime-text-2/Packages/Default/Indentation Rules - Comments.tmPreferences
Replacing
<key>scope</key>
<string>comment</string>
<key>settings</key>
<dict>
<key>preserveIndent</key>
<true/>
</dict>
With
<key>scope</key>
<string>comment</string>
<key>settings</key>
<dict>
<key>preserveIndent</key>
<false/>
</dict>
But it affects the default behavior, and I do prefer only affect Ruby's behavior.
Does anyone has greater solution ?
I suggest you use the BeautifyRuby ST2 package. You will also need to install the htmlbeautifier gem. Not only your comments, but also your code will be indented nicely.
If you use rvm, you might need to change the BeautifyRuby.sublime-settings to use the ruby installed by the rvm, instead of the system installed ruby. To find out the path of ruby you are using, type which ruby at the shell prompt. Paste this path as the value for the key named "ruby" such as:
"ruby": "/home/thetuxracer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby"
and beautifyruby can be use with : edit->beautify ruby
or you can change it keybind:
{
"keys": ["alt+tab"],
"command": "beautify_ruby",
"context": { "key": "selector",
"operator": "equal",
"operand": "source.rb, source.ruby" }
},
{
"keys": ["alt+tab"],
"command": "reindent",
"args": {
"single_line": false
},
"context": { "key": "selector",
"operator": "not_equal",
"operand": "source.rb, source.ruby" }
},
I put this in my User settings file:
Preferences -> Settings -- User :
{
"color_scheme": "Packages/Theme - Refined/Color Schemes/Danro.tmTheme",
"ensure_newline_at_eof_on_save": true,
"font_size": 18.0,
"hot_exit": false,
"ignored_packages":
[
"Vintage"
],
"remember_open_files": false,
"save_on_focus_lost": true,
"tab_size": 2,
"translate_tabs_to_spaces": true
}
I tried to recreate your problem and I couldn't with these setting enforced.
For indentation following is the key setting
"tab_size": 2,
"translate_tabs_to_spaces": true
This works for me
{
"caret_style": "solid",
"color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme",
"enable_tab_scrolling": false,
"folder_exclude_patterns":
[
".git",
".hg",
".sass-cache",
"log",
"tmp",
"script",
"vendor",
],
"font_size": 10,
"highlight_line": true,
"highlight_modified_tabs": true,
"ignored_packages":
[
"Vintage"
],
"rulers":
[
100
],
"scroll_past_end": false,
"tab_size": 2,
"translate_tabs_to_spaces": true
}
For ST3,
Preferences -> Settings -- User
Add following code:
{
"tab_size": 2
}
By default SublimeText uses hard-tabs that are 4 characters long.
Related
I'm trying to set my Sublime Text 3, but I'm not able to do my desired settings (which was worked in ST2 on my old computer).
What I need
When I type in CSS, I type eg. color: and I'd like to have autocomplete to color: |; (where | is a cursor).
What I have so far
I've found an advice to add
{ "keys": [":"], "command": "insert", "args": {"characters": ": ;"}}
into sublime-keymap. It partially works, it add space and semicolon but cursor if after, not inside.
When I googled, I had 99% of results for ST2 not ST3.
Any idea? Thanks.
The insert command just inserts exactly the text that you give it, as if you typed it yourself. If you want to do something like insert text and specify the location at which the cursor ends up, you want insert_snippet instead.
The default key bindings have several examples of keys bound using insert_snippet as the command that demonstrate this. For example:
// Auto-pair single quotes
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "['a-zA-Z0-9_]$", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true }
]
},
As noted here, the insert_snippet command takes as one of its arguments contents, which specifies the text of the snippet to insert, and this text can contain things like snippet fields and the like just as a sublime-snippet file can. The special field $0 specifies where the cursor should be placed.
This particular example also contains context items that define exactly in what situations this binding should be active.
As outlined in your question, your binding will trigger every time you type a colon, which stops you from ever being able to just type a single colon. So, you may want to add context to your key as well if you haven't already done so.
As an aside, one of the features of Sublime Text 4 is that it will automatically inject a snippet just like this one when entering CSS properties.
I'm using this two bindings for auto complete:
{
"keys": ["tab"],
"command": "move",
"args": {"by": "lines", "forward": true},
"context":
[
{ "key": "auto_complete_visible", "operator": "equal", "operand": true }
]
},
{
"keys": ["shift+tab"],
"command": "move",
"args": {"by": "lines", "forward": false},
"context":
[
{ "key": "auto_complete_visible", "operator": "equal", "operand": true }
]
},
I would like to add "commit_completion" command to space key:
"keys": ["space"],
"command": "commit_completion",
"context":
[
{ "key": "auto_complete_visible", "operator": "equal", "operand": true }
]
},
But it's not binding, when I press space it acts as normal space(it makes space xD). I'm able to bind it to any other key but not space. What am I missing?
There's not a key code for the space bar that you can bind like that; if you want to bind something to space, you need to use a literal space character:
{
"keys": [" "],
"command": "commit_completion",
"context": [
{ "key": "auto_complete_visible", "operator": "equal", "operand": true }
]
},
This is visible in the Sublime console by using sublime.log_input(True) to turn on input logging and then pressing the key; the only thing that gets logged is a character event, and not a key event.
It's also important to note that you never want to bind anything to a character like this unless you're using a context to constrain when the binding applies, or you lose the ability to type that character.
I want the cursor to be on a new line
currently it is on the same line as the highlight line.
I have tried adding "newline" : true, but it didn't work.
How can I resolve it ?
I'm pretty new to Oh My Posh installed on my Mac. I've found very little about what to do to add a \n at the end of the prompt in the documentation. I've even tried what I've read in this dev.to powershell article by adding this block at the end of the json file...
...
{
"type": "prompt",
"alignment": "left",
"newline": true,
"segments": [
{
"type": "text",
"style": "plain",
"foreground": "#007ACC",
"properties": {
"prefix": "",
"text": "\uE602"
}
}
]
}
...
...but it didn't work for me. At best I can only get a "newline": true with an empty line between executed commands, e.g.:
~/Downloads $
~/Downloads $
instead of the desired
~/Downloads
$
~/Downloads
$
SO!
Even if it might be the less elegant workaround I simply found any "trailing_diamond": "\ue0b4" in the omp.json file and (by tries and errors) added to almost all them \n$:
...
"trailing_diamond": "\ue0b4\n$ "
...
EDIT:
Found a new solution
I stumbled upon a GitHub Issue comment by JanDeDobbeleer (owner of JanDeDobbeleer/oh-my-posh) which is something I'm pretty sure I hadn't found on the documentation, especially in the arguments listed in Type.
Adding a new block with just type property and just newline as argument works like a the charm 😉:
{
"type": "newline"
},
Despite what I thought, the last approach is now deprecated!
The solution would still be to use a block (first and last examples) as the last item in the blocks array/list
{
"$schema": ... ,
...
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
...
]
},
{
...
},
{
"alignment": "left",
"newline": true,
"segments": [{
"foreground": "#007ACC",
"style": "plain",
"template": "\ue602 ",
"type": "text"
}],
"type": "prompt"
},
]
}
Check first your #function Prompt() in your profile path.
If it includes, as shown here, $nonewline, then you might have to set nonewline to false, instead of newline to true.
I have no idea when I try to exit multiple selection state in sublme text with keyboard.
I don't want to use the mouse.
I found a way to fix it.
Firstly, try to read the doc:https://www.sublimetext.com/docs/2/multiple_selection_with_the_keyboard.html
secondly, Maybe the editor become command mode when you useing Esc try to exit the multiple selection state. Opening Setting-User and adding "ignored_packages":["Vintage"].It will be ok.
Even as of Sublime Text 3.2.1 exiting multi-line mode is difficult by default. Instead of switching to vintage mode, you may also bind a new key to exit multi-line mode, which I find a better solution as escape has other functions already that can conflict.
The relevant (default) keybindings are:
{ "keys": ["ctrl+shift+l"], "command": "split_selection_into_lines" },
{ "keys": ["escape"], "command": "single_selection", "context":
[
{ "key": "num_selections", "operator": "not_equal", "operand": 1 }
]
}
In my case I added
{ "keys": ["ctrl+shift+;"], "command": "single_selection", "context":
[
{ "key": "num_selections", "operator": "not_equal", "operand": 1 }
]
}
to my user bindings, but you may choose any key combination you prefer.
In Sublime Text 3, I am trying to alternate a setting's value using the same shortcut, but with differing contexts. Basically, I want to alternate the draw_white_space setting between its three possible values: none, selection, and all.
I change the setting easily enough with three separate shortcuts/keymaps. Here is that code (working):
{
"keys": ["ctrl+e", "ctrl+w"],
"command": "set_setting",
"args": {
"setting": "draw_white_space",
"value": "all",
}
},
{
"keys": ["ctrl+e", "ctrl+q"],
"command": "set_setting",
"args": {
"setting": "draw_white_space",
"value": "none",
}
},
{
"keys": ["ctrl+e", "ctrl+s"],
"command": "set_setting",
"args": {
"setting": "draw_white_space",
"value": "selection",
}
}
But, what I would really like is to be able to press ["ctrl+e", "ctrl+w"] and have it alternate through each possible value. Yes, it is a Visual Studio shortcut that I'm used to!
I created what looks to me like it should work, but it doesn't. At least not how I want it to. Here is that code (broken):
{
"keys": ["ctrl+e", "ctrl+w"],
"command": "set_setting",
"args": {
"setting": "draw_white_space",
"value": "none",
},
"context": [
{ "key": "setting.draw_white_space",
"operator": "equal", "operand": "all" }
]
},
{
"keys": ["ctrl+e", "ctrl+w"],
"command": "set_setting",
"args": {
"setting": "draw_white_space",
"value": "selection",
},
"context": [
{ "key": "setting.draw_white_space",
"operator": "equal", "operand": "none" }
]
},
{
"keys": ["ctrl+e", "ctrl+w"],
"command": "set_setting",
"args": {
"setting": "draw_white_space",
"value": "all",
},
"context": [
{ "key": "setting.draw_white_space",
"operator": "equal", "operand": "selection" }
]
}
I have tested out my contexts, so I know they work. For instance, I can manually set all in my settings file, then the shortcut that checks for all will work the first time only. It nor the others will work after that.
Another thing I noticed is that my settings file does not change the draw_white_space value when the shortcut does work (including the three separate shortcuts). I assumed that might be default behavior - the settings changes may be per session - and that would be fine. But, I removed the setting completely and it is still the same behavior.
I am changing the file opened via the Preferences | Key Bindings - User menu, which opened the <Sublime Text>\Data\Packages\User\Default (Windows).sublime-keymap file.
Any ideas? Am I doing something wrong or missing something?
Maybe not what you want, but you can get that behavior with a pretty simple plugin.
import sublime
import sublime_plugin
class CycleDrawWhiteSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
white_space_type = view.settings().get("draw_white_space")
if white_space_type == "all":
view.settings().set("draw_white_space", "none")
elif white_space_type == "none":
view.settings().set("draw_white_space", "selection")
else:
view.settings().set("draw_white_space", "all")
After you save the plugin, bind your key binding to cycle_draw_white_space