Column-aligning jagged data
-
I will play reverse-golf and make @Claudia-Frank 's version longer but IMO better…and still one line:
editor.setText(['\r\n', '\r', '\n'][notepad.getFormatType()].join([('{:<' + str(editor.getColumn(editor.getCurrentPos())-1) + '} {}').format(*x.split()) for x in editor.getText().splitlines()]))
Two changes:
- do correct line-endings, not Linux–sorry Claudia!–line-endings
- start the aligned data in the column the caret is in when the script is run (be sure to leave the caret in a column greater than the longest entry in the leftmost data “column”!)
-
I deserve what I get because I didn’t quite ask in the right way. I was sort of looking for the solution to the general case. But in presenting example text I got specific answers to solve that specific thing (2 columns, whole file). Don’t get me wrong, the answers I got were awesome!–thanks to responders! Good ideas, all!
Of the answers I think Scott’s (put caret in column…and then run script) starts getting at the interactivity I was hoping for. Another clarifying situation might be what if I want this to only affect certain lines, or only after a certain column point on specific lines…
So I guess the main answer is something like this is best served by scripting, although in the end I did like Guy’s regexes (although i did try to head off his enthusiasm for them with my earlier post).
-
Hi, @alan-kilborn,
Another clarifying situation might be what if I want this to only affect certain lines, or only after a certain column point on specific lines…
-
Concerning the possibility to change text, after a specific column point
c
, simply use the regex^.{c+ε}\K\x20+
-
Concerning reducing text changed to a specific block of lines, do a normal selection of your range of lines, first. So, when opening the Replace dialog, the In selection option is automatically ticked, and the Replace All operation is performed on the selection, only :-))
Cheers,
guy038
-
-
The old man wasn’t invited to the tournament. Nevertheless, he ambled over to the tee box and took a swing with an ancient wooden driver that has been meticulously maintained for more than 40 years:
gawk "{printf \"%-256s%s\n\",$1,$2}" $(FULL_CURRENT_PATH)
:-)
-
Somewhat tangential but possibly a solution is the Elastic Tabstops plugin. Its would only require a single tab between columns but has the disadvantage of only working within Notepad++ itself.
-
Neither was the simpleton invited to the tournament but he stumbled up to the tee and out from his bag fell a TextFX plugin and hideous python script that would make a crow blush:
# coding: iso-8859-1 selected = editor.getSelText() selStart = editor.getSelectionStart() #replace any existing commas with a weird char selected = selected.replace(",", chr(174)) #replace the double spaces while ( selected.find(" ") > 0 ): selected = selected.replace(" ", " ") #replace the spaces with commas since our 'line up' function uses commas selected = selected.replace(" ", ",") selEnd = len(selected) editor.replaceSel(selected) #re-select the selection editor.setSelectionStart(selStart) editor.setSelectionEnd(selStart + selEnd) notepad.runMenuCommand("TextFX Edit", "Line up multiple lines by (,)") notepad.runMenuCommand("TextFX Edit", "E:Line up multiple lines by (,)") selected = editor.getSelText() #take out the lineup commas selected = selected.replace(",", " ") #put back any original commas selected = selected.replace(chr(174), ",") editor.replaceSel(selected)
This works for any number of columns, and only on lines in the current selection. It makes the columns as narrow as possible. I’m not really sure how you would line up things after a certain column point though.
-
Hello, @cipher-1024, and All,
I’m thinking about an other solution, which still use the TextFX plugin but which avoids this [ hideous :-D ] Python Script !
- First, use the following regex S/R :
SEARCH
\x20+
REPLACE
\x60
Note : I, specially, chose the Unicode Grave Accent character (
U+0060
) , as a dummy character, because it is, both, rarely used in programming languages, ( AFAIK ! ) and part of all character encodings, as belonging to the international ASCII encoding ( from UnicodeU+0000
toU+007F
)-
Copy a single ` ( Grave Accent ) in the clipboard, hitting the
Ctrl + C
shortcut ( IMPORTANT ) -
Now, do a normal selection of the text, which is to be aligned
-
Click on the menu choice TextFX > TextFX Edit > Line up multiples lines by (Clipboard Character)
-
Finally, use the regex, below, to delete the dummy Grave Accent character ` and add some space characters between columns, with a possible delimiter character !
SEARCH
\x60
REPLACE
\x20\x20\x20
OR, for instance :
SEARCH
\x60
REPLACE
\x20\x20|\x20\x20
Cheers,
guy038
-
Other than “rarely used in programming languages,” I like that answer.
Perl uses a pair of Grave Accents (aka “backticks”) as an often-used alternate for the
qx//
quote-like syntax for running a shell command and placing the command’s output in a string.SQL uses backticks for denoting identifiers, such as field names.
Markdown uses it for embedding
inline
fixed width text, like:embedding `inline` fixed width text
But if you know your text has no backticks, then it’s a great choice.
If your data might have backticks, I would use U+001C (
\x1c
), the Field SeparatorFS
character, which is a control code found in ASCII. (I won’t make the claim that it’s “rarely used” in text files or programming language source code… but I’ve never seen it intentionally used in such. :-) )I think this style of solution meets the original requirements of not requiring complicated S/R regex or precomputing, which is nice.
-
Hi, @PeterJones and All,
So, I strongly apologize ! My programming skills are weaker than most N++ users’s ones :-D.
BTW, Peter, just have a look to the link, below :
https://en.wikipedia.org/wiki/C0_and_C1_control_codes
it seems, that the C0 Control character (
\x1C
) rather refers to the File Separator control character ! Anyway, your idea, about using a Control character, is great ! And, if we follow the description notes, it would be logical to prefer the US Control character\x1F
:-DCheers,
guy038
-
IMO the ultimate solution to the question I originally posed is found HERE.
-
I didn’t check the date of the original posting but did immediately say "that’s a job for
BetterMultiSelection
. It was very satisfying to be able to figure out how to solve that problem. (It took me a few attempts, but that’s why Ctrl-Z exists)Thank you to @Alan-Kilborn for a wonderful lesson. It really helped drive home @astrosofista’s examples.
-
…It really helped drive home @astrosofista’s examples
This was not my intention; perhaps you misunderstood.
I was linking directly to a posting, not the larger thread, for the awesome solution to the problem posed here in this thread.
The linked posting discusses using Ctrl+Delete, not any plugin.Plugins (including Better Multiselection) are great, but even better is when something available natively is the solution to something. And the Ctrl+Delete technique is available natively.