Tab replaces all leading spaces
-
It is appreciated when you include debug info in your original request for help. Thanks! :-)
The animation below (which I had made for a different question) shows how changing the “replace by space” affects
Unfortunately, based on your description of what you want, I’m not sure we can make it work.
In my experiments, if it’s set to “replace by space”, if you hit the TAB key in the spaces or tabs at the beginning of the line, it will convert the initial blanks all to spaces; if you have it disabled, hitting the TAB key in the spaces or tabs at the beginning of the line will convert all the initial spaces/tabs to tabs.
I don’t know of another setting that will allow a mix of tab and space characters at the beginning of the line. Maybe someone else here does know of the setting.
Otherwise, we might need to do a feature request which would be asking for a toggle to “not change existing whitespace” or “allow mixed tabs and spaces” or some such wording. I can easily see how there might be some text editing contexts where you would want to be able to mix spaces and tabs in the start of a line. (For example, where TAB is a record separator, and space is meaningful as the data.)
-
I remember this coming up before, but I can’t find the reference. Basically, it is something that the Scintilla component is doing; Notepad++ doesn’t really have control.
-
Thank you for examining this.
I can avoid this by inserting an extra character and then the Tab, and then delete the extra character.
I didn’t find this issue in other editors, but they don’t use Scintilla. -
@László-Botka said:
inserting an extra character and then the Tab, and then delete the extra character
Glad you found a workaround. Sorry we didn’t have a better solution for you.
I didn’t find this issue in other editors, but they don’t use Scintilla.
I just confirmed SciTE (which is Scintilla’s test-bed GUI) does the same thing: if Options > Change Indentation Settings is set to “use tabs”, then hitting a TAB in the early whitespace will turn it all to TAB; if “use tabs” is disabled, it will convert it all to spaces. So the issue is in SciTE 4.1.4 (and they appear to keep the SciTE version number in sync with the Scintilla version number to avoid confusion) … (but that also means that our “maybe it’s already been fixed in a more recent Scintilla” standard answer won’t apply)
-
Memory might fail me but I think it was decided by the Scintilla folks that this is their desired behavior and it isn’t going to be changed, or a “preference” offered for. Could be wrong (I say that a lot).
I can avoid this by inserting an extra character and then the Tab, and then delete the extra character.
Sounds like a macro to me:
- Start macro record with caret at desired spot
- Press
~
(or other character, maybe best to pick one that won’t trigger Autocomplete) - Press
Tab
- Press left arrow
- Press Backspace
- Press Right arrow
- End macro recording
Assign it to a keycombination of your choosing.
Rather a pain, and it will mess with the Undo buffer, but maybe useful.
-
There are two plugins I havn’t tried yet but maybe they could help you.
The first one is Elastic Tabstops. To obtain some infos about how it works have a look at >>> this site <<<. It can be downloaded via PluginsAdmin as 32 bit and 64 bit version.
The second one is TabIndentSpaceAlign. It is not available via PluginsAdmin and it’s only 32 bit. You can download it >>> here <<< but you have to install it manually. This plugin was available via the old PluginManager. It hasn’t been included in PluginsAdmin’s plugin list because the DLL file doesn’t contain any version information, which is a requirement. But there are chances that it will run under Notepad++ v7.6 and above.
-
After several years of use, this is the first time that this issue has occurred, in a free format text. Thanks for the recommended macro.
-
Thank you for looking at this.
I’ll examine them in detail, but from the short descriptions I think they operate under strict rules - I simply want that a Tab pressed be Tab, a space pressed be space in the text.@ PeterJones
It is very unfortunate that Scintilla does not support the above-mentioned operation, the most simple Tab/Space handling , which is naturally supported by the Windows editors like Notepad, Pspad, Ultraedit, FCW editor,
-
there is a way to work around it, but it involves a scripting plugin.
One would need to set tab indents attribute to false for each buffer which gets activated.
I hope I understood your request correctly. -
Hello, @lászló-botka, @peterjones, @alan-kilborn, @dinkumoil, @ekopalypse, and All,
Have a look to the article, below :
https://stackoverflow.com/questions/13973255/notepad-keeping-spaces-in-indent
Answering to @ellak, the OP, @zennehoy, said something quite pertinent :
We follow the principle that tabs are used for indentation, and spaces are used for alignment. This allows everyone to choose an indentation size they like, without running into misalignment issues when different people use different indentation sizes. The front two tabs are indentation, the spaces, [coming after], are alignment.
Indenting problem is rather complex :
-
It depends on the key pressed (
Tab
orShift + Tab
) -
It depends on the position of cursor ( in the “indentation” part or in the “code” part )
-
It may depends of the character right before the cursor location ( a
Tab
character, aSpace
character or any other char )
As for the indentation part of the next line, when hitting the
Enter
key :-
It may be changed into a complete space characters area
-
It may be changed into a complete tab characters area, with additional space chars to respect the tabstops
-
It may fully copy the precedent indentation, without any modification
I tested on N++
v7.6.3
, both, theElastic Tabstops
andTabIndentSpaceAlign
plugins. To install them :-
Click on the
Plugins > Open Plugins Folder..
option -
Create a folder
TabIndentSpaceAlign
and/or a folderElasticTabstops
-
Stop any Notepad++ instance
-
Place the downloaded
dll
files, inside its respective folder -
Restart N++
After some tests, it happened that the
Elastic Tabstops
is not suitable for your problem as it is rather used to align areas of text, in columns, separated with tabulation character(s), with elastic tabstops, which may be converted, later, in physical space charactersUnfortunately, @ekopalypse, your Python work-around isn’t completely useful, too. Indeed, as soon as you hit the
Enter
key, the default N++ behavior occurs and changes most of thespace
characters totabulation
chars :-(So, @lászló-botka, I do think that the best, for you, would be to install the
TabIndentSpaceAlign
plugin ;-))
Once the
TabIndentSpaceAlign
plugin enabled :-
A hit on the
Tab
key, in the “indentation” part of a line :-
Adds a
Tabulation
character, if the previous character was aTabulation
character ( ofN
space size ) -
Adds
N
space character(s) or less, if the previous characters was aspace
character
-
-
A hit on the
Tab
key, in the “code” part of a line :- Adds
N
space character(s) or less, in all cases ( Hold down theAlt
key and hit , successively, on keys0
and9
, of the numeric keypad, if you really want to insert a tabulation character )
- Adds
-
A hit on the
Shift + Tab
key, in the indentation part of a line :-
Remove the previous
Tabulation
character, right before the cursor location -
Remove all the previous area of space character(s), right before the cursor location
-
-
A hit on the
Shift + Tab
key, in the code part of a line :- Simply move the cursor on the previous tabstop, whatever its location, without deleting* anything
- A hit on the
Enter
key rewrites the exact indentation part of the current line in the next line
Best Regards,
guy038
-
-
Many thanks to everyone for their help
The TabIndentSpaceAlign plugin works fine with Alt numpad 09 in the “code” part.
But it’s complicate to press, and I’m sure I will sometimes press Tab and don’t notice when I write fast.
…
I found the solution:
If I use Ctrl V to Insert Tab (instead of pressing Tab), the result will be what I want: Tabs will be stored as Tab characters, Spaces will be stored as Space characters everywhere.[ To achieve this, I already have a hotkey defined with Ditto, an extension to the Windows Clipboard. I use it regularly, to insert a Tab character into the 'Find what" field, searching in Normal mode in the Ctrl+F “Find window”. ]
-
Thanks for the Alt numpad 09 tip, it can also be used in in my original issue, without any extra plugin.
-
…Alt numpad 09 tip…
It may be worth pointing out that one can easily insert a tab character, without pressing Tab or using the numpad technique, from the ASCII Insertion Panel. Make that panel visible from the Edit menu by choosing Character Panel. Double-clicking any line in that panel inserts the clicked-on character. Useful for others besides tab obviously (although limited to “ASCII” characters).
This panel is also a handy reference when you see “black boxed” characters in your (mostly text) file and you’re wanting to know their binary values (perhaps on your way to figuring out how/why they got into your file). You simply mentally note the text inside the black box and compare it to the Character column values on the panel…but let’s not go too far down this road as N++ is a text file editor, not a binary file editor.
-
your Python work-around isn’t completely useful, too. Indeed, as soon as you hit the Enter key, the default N++ behavior occurs and changes most of the space characters to tabulation chars :-(
does not happen to me.
If i hit enter the line stays as it was but if you refer to the next line, then yes,
this might happen if you have autoindent enabled but this doesn’t make
sense in this case, doesn’t it? -
@Ekopalypse said:
this might happen if you have autoindent enabled but this doesn’t make
sense in this case,I’m going out on a limb to say that if one is coding, and autoindent is NOT turned on, N++ is too painful to use. :)
-
:-D, but if you like to insert spaces and tabs like YOU want, how should a program know this beforehand??
-
@Ekopalypse said:
how should a program know this beforehand
I’m not following this thread super-closely because in my now 3 decades of software development, the coding standard has been to never allow the use of actual tab characters in source files. But I would think it should replicate the indentation exactly of the line-above, but let the user then edit that and leave it the way the user changes it…but I am too dumb about it to continue talking… :)