How to (left) align text column (normal text, not code)


#1

NB. I have completed this task *yesterday with other editor, am asking now for 2 reasons:

  • benefit of other people that might have the same need
  • beginner learning of Atom

The question is about how to (left) align normal text, not code; primarily text where every line contains the same number of words.
Ability to align lines with different number of words would be extremely nice but probably more complex.
The question is about text separated by a variable number of whitespace (ex. no csv), text like this:

welcher welche     welches    welche
 welchen welche    welches          welche
welches welcher welches welcher
welchem welcher   welchem      welchen

* completed the task with other editor after asking for help on this forum, getting some replies whose main content was to say that I did not ask a clear question Etc.Etc.
Also got a reply that did not suggest a solution within Atom but was kind and constructive; while writing to thank the author of this last reply my account and post disappeared (never seen something like that, since the day of NNTP), recreated account and am posting again


#2

For completeness: just found out that disappearance from account was due to a misunderstanding with support, not to some technical problem


#3

Hello.

The formatting in your topic question did not come out as you intended. I will not divert your attention to forum usage at this time. My guess is you are looking for (ignore the table lines)…

column column column
these are a few words these are some other words another thought
yes no maybe

(Above generated in Markdown: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)

Are you looking to use this in a text file or mark-up file?

I am not aware of any built-in functionality that would do this. Please have a look at https://atom.io/packages, perhaps looking for search words relating to table, beautify, align or such. Also consult (old) posts relating to “writing” or technical writing or documenting (I did see something that may be helpful.)

If you feel strongly about this topic and think this is something that Atom should be able to do then make a write-up at the Github Atom project. Please focus on logical explaining and motivation on how it is helpful.

As summary - Atom does not have what you ask (AFAIK). It is recommended that you look into 3rd party add-ons.

Regards.


#4

Hello Dan,
I am interested in plain text.
The text in my first post was a fictional sample of the originary text that wold need to be formatted, with the columns not aligned. The desired result would be this

welcher welche  welches  welche
welchen welche  welches  welche
welches welcher welches  welcher
welchem welcher welchem  welchen

Another simple example, original text:

aaa                      bbb ccc
1111111111  1 97879

desired result:

aaa        bbb ccc
1111111111 1   97879

I looked at Atom buil in functionalities at made several research and tests on plugins without finding a solution. The search was very quick and superficial as I had to complete the task urgently, so there might be a solution but I did not see it.
I will continue to look and post it if I find it, but posted this question ow for eventual benefit of other beginners.
I do not feel strongly about this topic, also because Atom focus on code so

  1. it would not be surprising if something not related to code is not (yet) available
  2. not being related to code this feature does not seem to me important enought to deserve a feature request or anything that takes meaningful time of people doing real work on Atom or its plugins

In the future as I accumulate more knowledge of Atom (and have the needed time, not the case right now unfortunately) I hope to have time to look into plugins code and maybe see that there is a way through cofiguration (30% chance would say) or through some modifications of existing plugins (90% chance would say)


#5

Notice now some typos but cannot find a way to edit my post to correct them, so:
I looked at Atom buil in functionalities at made …
should have been
I looked at Atom built-in functionalities and made …


#6

Hello.

The more you read posts and/or make posts, the more usage rights you get. Frustrating but understandable. Spam generators are really tough to concur.

Type in ‘align’ in the install section of Atom’s settings and hit the packages button. The first two on the list is already full of potential; the first the most.

It will need your customization… but you put me under the impression that you will be able to handle this.

My suggestion - make characters your alignment indicator. For example ,, … then the add-on can align the text according to the ,, and then when done can delete those characters again.

That is all I have for you…
As a courtesy to me - behave yourself this time and keep your posts co-operatively in nature.
I hope we can further learn from each other.

Cheers.


#7

Adding markers was the first thing I tried and probably would works with several plugins, my interest was to find out if there was a more direct solution (also as a way to learn Atom), but am glad that to see that you also suggest that.

The plugins that, at first sight, look promising and that I will examine as soon as I will have accumulated more skills and have the time to do it are:



Thanks again for your posts, useful in content and kind in form


#8

Here’s the finished algorithm. It would require minor modifications in order to be put into init.coffee, such as how to source the text (you haven’t mentioned if you want to change the whole file or only a selection) and inserting the new text into the TextEditor.

data = "This is a string\nwith specific white space.\nOne of these lines is substantially longer."

rows = data.split("\n")
rowLen = []
rowMax = 0

i = 0
for row in rows
  rows[i] = row.split(/\s+/)
  rowLen[i] = row.length
  if (rowLen[i] > rowMax)
    rowMax = rowLen[i]
    
  i++

col = 0
spacer = 2
done = false
until done
  for row in rows
    if row.length > col
      if row[col].length > spacer
        spacer = row[col].length
      
  for row in rows
    if row.length > col
      row[col] = row[col].padEnd(spacer, " ")
  
  col++
  spacer = 2
  
  if col == rowMax
    done = true
    
    i = 0
    for row in rows
      rows[i] = row.join(" ")
      i++
    
    output = rows.join("\n")

console.log(output)

#9

It will take me some time to understand it and start using but it looks great, not only as an answer to the question but also as a way to to start learning how to hack.
Many thanks.


#10

If you let me know how to determine which text to format like that, I can give you a modified version right now that you can drop into your init.coffee file and get started immediately. If you want to learn how for yourself, the documentation starts here (but it’s not very newbie-friendly by itself).


#11

… I see I am missing the party (smile)
…I prepared a response a little earlier.

Hello.

You have listed some usable add-ons. align-regex works well.

Test text:

column      column   column
these are a few words   these are some other words   another thought
yes   no   maybe
  1. select all text

  2. trigger the package Ctrl-Alt-A

  3. put into the dialog the regular expression:
    g: [ ]{2}\S
    Note: At least x3 spaces infront of the text. \S identifies the character that is not a space (negative of \s). Care with the space after the g:, this is a notation must. More is explained in the readme of the package.

  4. Result:

column                  column                       column
these are a few words   these are some other words   another thought
yes                     no                           maybe

Your sample:

welcher  welche  welches  welche
  welchen  welche  welches  welche
welches  welcher  welches  welcher
welchem  welcher  welchem  welchen

Regular expression:
g: [ ]{2}\S

Result:

welcher      welche    welches   welche
   welchen   welche    welches   welche
welches      welcher   welches   welcher
welchem      welcher   welchem   welchen

…meaning that the first column needs to be aligned already. Alternatively the find-replace can help to convert ^[ ]+ to (blank).


#12

With you willing to help others to solve their problems -
I do not see this as too big of a hurdle.
Thank you for you volunteering your time and efforts!


#13

Thank you very much.


#14

I want to learn that for myself (as soon I can find sufficient “contiguous” free time), thanks for the pointers to the doc.


#15

@DamnedScholar - I have a bit of a snag…
image

Is padEnd a new function?
I know it exists: https://developer.mozilla.org… - it is just not on my system. Atom also wants to push through me firewall to phone home after executing that line.

TypeError: row[col].padEnd is not a function
    at HTMLElement.<anonymous> (file:///C:/AtomBeta/.atom/init.coffee:29:29)
    at CommandRegistry.handleCommandEvent (C:\AtomBeta\Atom-1.22-beta1\resources\app\src\command-registry.js:381:36)
    at KeymapManager.module.exports.KeymapManager.dispatchCommandEvent (C:\AtomBeta\Atom-1.22-beta1\resources\app\node_modules\atom-keymap\lib\keymap-manager.js:621:22)
    at KeymapManager.module.exports.KeymapManager.handleKeyboardEvent (C:\AtomBeta\Atom-1.22-beta1\resources\app\node_modules\atom-keymap\lib\keymap-manager.js:412:28)
    at WindowEventHandler.module.exports.WindowEventHandler.handleDocumentKeyEvent (C:\AtomBeta\Atom-1.22-beta1\resources\app\src\window-event-handler.js:100:42)
    at HTMLDocument.<anonymous> (C:\AtomBeta\Atom-1.22-beta1\resources\app\src\window-event-handler.js:3:65)

Failed to load resource: net::ERR_NETWORK_ACCESS_DENIED
Failed to fetch
TypeError: Failed to fetch
ERR_NETWORK_ACCESS_DENIED

#16

Notice only now your question whole file or selection, sorry about that. A selection seems nicer for a general solution, but for me whole file/buffer is perfectly ok.


#17

I guess it must be that Atom’s version of Chrome is behind where padEnd() was introduced. That line could be easily replaced by one that takes the difference between row[col].length and spacer and adds that many spaces to the end. It also wouldn’t be hard to reimplement it.

padEnd = (word, len, fill=" ") ->
  diff = len - word.length
  if diff > 0
    return word + fill.repeat(diff)

#18

Thank you for your reply and giving an alternative.
I am curious why my system did not run the code as-is…
I assume yours did - right?

Cheers.


#19

It’s because Atom’s internal Chrome bits are slightly behind. According to MDN, padEnd() was introduced in Chrome 57. I don’t know how to check Atom’s version, but the current version of Chrome is 61. There’s a little lag time in upgrading the Chrome part of Electron, and then there’s more lag time between when Electron updates and when that filters into Atom.

I wrote that code in the online REPL, so I didn’t even think about compatibility.


#20

…yes you do know - let me remind you:
image

Good information you have given - thank you.