SublimeText 3 - Nothing works to force indentation to 4 spaces - sublimetext3

Issue:
Can't set/change tab indentation in documents
Have tried:
With document open: >Indentation >Tab width: 4
With document open: >Indentation >Convert Indentation to Spaces
Adding the following to user settings:
"smart_indent": true,
"tab_size": 4,
"translate_tabs_to_spaces": true,
"trim_automatic_white_space": true,
"trim_trailing_white_space_on_save": true,
"use_tab_stops": true,
"word_wrap": false
Adding the following to the php-specific preferences:
"tab_size": 4,
"translate_tabs_to_spaces": true
Questions:
Does it seem weird that changing the spaces from the menu doesn't even work? SublimeText states that the tab indentation is 4 spaces - but it's not.
Does anyone know how to force SublimeText to apply a tab indentation to a document or to a project?
Update:
It looks as if ST3 is not recognising some spaces as indentation and thinks they're just spaces - see image
The above image is the result of converting spaces to tabs (the opposite of what I want) to see what ST3 thinks is indentation
Does anyone know how to fix this besides going through every document
and changing spacing on every single line?

The only command from that list that actively changes all indentation in the current file is the > Indentation > Convert Indentation to Spaces. The other settings only take effect from "that point on" (i.e. existing indentation will remain tabs, newly created ones will be spaces). Also, make sure you try running the command after selecting Indentation > Indent using spaces, and Tab Width 4
Could you check that running Convert Indentation to Spaces, and with "draw_white_space": "all", on, that indentation is shown using semi-transparent dots? Navigation with the caret will still "jump" the 4 spaces.
Only other thing I can think of is it's somehow being a read-only file.
Can't comment because I've less than 50 rep.

There is a status bar at the very bottom of the window. If not visible go to View> Show status bar.
At the rightmost end you will see the language of the current file, Here python.
After that, it will show you whether you are using tabs or spaces and how many of them are being used. Click on it and you will get
Taken from https://css-tricks.com/changing-spaces-tabs-sublime-text/

Related

Sublime editor: how to force all files to use spaces or tabs?

I use Sublime 3.0 editor and my editor switches to Spaces: 2 for any *.vue file and if I open a JS file, it switches to Tab: 4. This is causing issues with my linter. How can I set a standard for all files?
// Set to false to disable detection of tabs vs. spaces on load
"detect_indentation": true,
This setting defaults to being turned on, and when it is Sublime will analyze files as they're loaded to try and detect how they're indented. That will cause it to switch from using tabs to spaces if the file seems to be indented one way or the other, and for space indented files it can also adjust the tab_width setting as appropriate.
Turning the setting off will stop it from doing that, which will leave the settings as they are even if they don't match what the current file looks like.

How to remove tabs on multiple rows at once

This is not a programming question but an inconvenience in the android-studio editor.
If you have an unwanted tab before all your lines, how can you remove them all at once? Now I have to manually go over 50 lines to remove all the tabs to make my code look clean.
If you want to add multiple tabs at once you just select all your code and press the tab button. So I'm looking for the reverse of that.
If I understood you right, you want to beautify the code itself. Fortunately, you don't have to do that manually - at all.
There's a keybinding for it, which may vary by your OS and which layout you use by default. Go to file -> settings -> Keymap and search for auto-indent. Here's what I get on Windows 10 with the default keymap:
Again, which you have may depend on OS (I'm assuming that mainly applies to Mac though) and your keymap, but you can automatically indent your code as per language standards using Ctrl+Alt+I.
Note that this mainly does indentation. If you chose to golf code and want to ungolf it, this will not work. At least it doesn't for Java.
However: This only works with code files the IDE or plugins support. This won't work for i.e. a .txt file out of the box.
If I misunderstood you and you only want to remove tabs without doing the auto-indent, there's at least two other options.
The first option is using multiple cursors. You can add an additional cursor with shift+alt+a mouse click where you want the cursor, or holding the mouse wheel and moving the cursor with the mouse wheel held down. There might be other methods as well, but those are the two I know of.
Once you have multiple cursors, delete the tabs just like normal. But be careful! Doing so might delete the entire line itself. If it does, you can do 1 tab/n units per indent level to the left, and press delete instead.
There's (AFAIK) no limit to how many cursors you can have at once, but you can theoretically do it with 50 lines at once if you want to. But general advice - don't add more cursors than you can see at once. These do run in parallel and it's easy to lose track if you're not careful, and you might end up deleting stuff you didn't want to delete.
And finally, the regex solution:
Note: Be careful with this. If you use it incorrectly, you might get unwanted results.
If you only want to do this in a limited area, highlight it first. Then CTRL+R and you'll be presented with the regular replace menu. Make sure Regex and In Selection are selected.
A base regex to go off is ^([\s]{2,4}|\t). Explanation just for reference:
^ - At the start of the line
(
\s{4} - Match 4 spaces
|\t - Or a tab character
)
Replace with nothing and click "replace all" (or just use the regular "replace" button if you want to double-check before you do anything). This will replace one occurrence of 4 spaces or a single tab character. If you use indentation that isn't based on 4, change the number.
This is only useable and useful if you've found yourself with incorrect indentation that's the same across all the relevant lines - it will not fix indentation mistakes and/or inconsistencies such as 3-space indentation when you want 4, or random indetation for the same block. Use the first or alternatively second method for that instead.

Sublime Text 3 is automatically converting spaces to tabs on save, regardless of settings. How can I stop this?

I'm having trouble with Sublime Text 3.
Even if I select View > Indentation > Convert tabs to spaces and View > Indentation > Indent using spaces as soon as I save the file it all automatically gets converted to tabs.
How can I stop this behaviour from occurring?
I'm currently using default config settings for this:
// The number of spaces a tab is considered equal to
"tab_size": 4,
// Set to true to insert spaces when tab is pressed
"translate_tabs_to_spaces": false,
// If translate_tabs_to_spaces is true, use_tab_stops will make tab and
// backspace insert/delete up to the next tabstop
"use_tab_stops": true,
// Set to false to disable detection of tabs vs. spaces on load
"detect_indentation": true,
// Calculates indentation automatically when pressing enter
"auto_indent": true,
// Makes auto indent a little smarter, e.g., by indenting the next line
// after an if statement in C. Requires auto_indent to be enabled.
"smart_indent": true,
The problem for me was the EditorConfig plugin, when disabling it in PackageControl the problem went away.
Alternatively you can just edit the problematic .editorconfig file and just comment out (or set to false) the trim_trailing_whitespace option.
Use the indentation option under the tab View.
You can select Tab Width:2
My solution was to give up and switch to VSCode. I wasn't happy about it. I resisted. I cursed. But in the end I had to give in as it is way better. I spend far less time fiddling with editor config and more time coding.
You have two solutions:
To do "View > Indentation > Convert tabs to spaces" before saving
the file.
To replace them as a normal text by pressing Ctrl + H. >>> Find all Tabs >>> Replace with 4 spaces

Sublime Text 2 Highlight Gutter

By default the line number / gutter area in sublime text 2 is highlighted when the caret is on that line. How can I turn it off?
I see options to hide the gutter but I want the line numbers to be visible. Thanks.
To turn off line highlighting go to preferences>settings - User through the menus and add this line:
"highlight_line": false,
then save. I am using the default Monokai theme in linux, this will change it from:
to:
As you can see the line is no longer highlighted (it doesn't show in the picture but my cursor is still on the line), but the gutter numbers are. I think you also want to get rid of that? So, open the theme file .../sublime-text-2/Packages/Color Scheme - Default/Monokai.tmTheme, and change the <key>lineHighlight</key> colour, so it is the same as the <key>background</key> color, ie. #3E3D32 goes to #272822, like this:
On saving, it should resolve your problem:
Only the 2nd step is really neccessary.
Some additions
"font_size": 9.0,
"caret_style": "smooth",
"highlight_line": true,
"line_padding_bottom": 2,
"line_padding_top": 2,
"fade_fold_buttons": false,

In Vim, how to keep characters concealed even when cursor enters that line

I may have a unique situation here. I want gVim (gui version, in Linux) to keep concealed characters concealed no matter what, even when the cursor is on that line or that character gets selected. (It should be as close to if the characters never existed as possible.) Currently the concealed characters show themselves when the cursor enters that line, which causes text to jump around when scrolling and when selecting text.
We are using gView (read-only gVim) to view logs, so as to take advantage of its robust syntax highlighting. Problem is, these logs contain lots of escape characters and TTY color codes, that make reading difficult. (^[33mSomeText^[0m)
I'm using this line to hide them:
syntax match Ignore /\%o33\[[0-9]\{0,5}m/ conceal
Since the files are viewed by non-vim-experts, it looks glitchy and broken when the text un-conceals itself. (And also looks glitchy and broken if the color codes are present, and also looks glitchy and broken if the color codes are blacked-out to become invisible, but still show when selected and appear after copy/paste.)
This should be fine because these files are opened read-only in gview, with an extra set nomodifiable making it even more difficult to save the file. While it's possible to edit and attempt to save the logs, doing so is considered both an invalid thing to do, and a harmless thing to do, and requires enough Vim skills that if someone manages to edit a file they know what they're doing. The problem with being able to edit a line with concealed text does not apply.
If 'conceal' can't be configured to keep hidden text hidden no matter what, an acceptable alternative would be to replace the TTY color codes with whitespace when the file gets opened. But, this has to be done in read-only mode, and we can't have gview throwing up a save dialog on closing the window because the file has been modified by its .vimrc.
Note: I am in full control of the .vim script file sourced when these are read, but cannot control the existence of the TTY color codes or the code that opens the log files in gview. (i.e. I can't pass it through sed or anything like that.) The ideal solution is anything that can transparently nuke the color codes from within a .vimrc, but I'll hear any suggestions. The 'conceal' feature is just my most promising lead.
So, any ideas how to permanently get rid of these on file view without dialogs popping up on close?
:help conceal
When the "conceal" argument is given, the item is marked as concealable.
Whether or not it is actually concealed depends on the value of the
'conceallevel' option. The 'concealcursor' option is used to decide whether
concealable items in the current line are displayed unconcealed to be able to
edit the line.
:help concealcursor
Sets the modes in which text in the cursor line can also be concealed.
When the current mode is listed then concealing happens just like in
other lines.
n Normal mode
v Visual mode
i Insert mode
c Command line editing, for 'incsearch'
'v' applies to all lines in the Visual area, not only the cursor.
A useful value is "nc". This is used in help files. So long as you
are moving around text is concealed, but when starting to insert text
or selecting a Visual area the concealed text is displayed, so that
you can see what you are doing.
Keep in mind that the cursor position is not always where it's
displayed. E.g., when moving vertically it may change column.
Also, :help conceallevel
Determine how text with the "conceal" syntax attribute |:syn-conceal|
is shown:
Value Effect ~
0 Text is shown normally
1 Each block of concealed text is replaced with one
character. If the syntax item does not have a custom
replacement character defined (see |:syn-cchar|) the
character defined in 'listchars' is used (default is a
space).
It is highlighted with the "Conceal" highlight group.
2 Concealed text is completely hidden unless it has a
custom replacement character defined (see
|:syn-cchar|).
3 Concealed text is completely hidden.
Only one command is needed: set concealcursor=n
I might have a better idea—you can pass it through sed (using %!sed) or really do a bunch of other :substitute commands—whatever edits you need to get rid of the color codes.
When you’re done, make sure to set nomodified—this forces vim to think there haven’t been any changes!

Resources