41

As shown in the picture "Before" below, each column separated by comma is not aligned neatedly. Is there any method to align each column vertically like the display effect in Excel?

The effect I wish is shown in the picture "After". Before After

Thanks to @Martin S , I can align the file like the picture "Method_1". As he has mentioned, some characters still cannot align well. I was wondering if this method could be improved? Method_1

6 Answers 6

41

You can use the TextFX plugin:

  • TextFX > TextFX Edit > Line up multiple lines by ...

Notepad++ > Line up...

Note: This doesn't work if the file is read only.

http://tomaslind.net/2016/02/18/how-to-align-columns-in-notepad/

Update 2019: Download link from SourceForge

11
  • 10
    I tried to use TextFX in my 64-bit Notepad++ and got an error that it was a 32-bit plugin and not compatible. Is there a 64-bit version?
    – TikiTavi
    Commented Nov 7, 2018 at 14:52
  • 2
    any alternative for npp 64x?
    – Suncatcher
    Commented Feb 8, 2020 at 4:36
  • 1
    I have notice that the TextFX plugin no longer shows up after about npp v7.5.6. So I downgraded back to that, and the TextFX plugin shows up again.
    – ToddK
    Commented Apr 23, 2020 at 15:50
  • 4
    My suggestion would be to use a different tool that does have an up-to-date plugin for this, e.g. VSCode Commented Sep 2, 2020 at 9:12
  • 1
    ElasticTabs is your friend
    – mcalex
    Commented Nov 25, 2021 at 6:34
22

Maybe not exactly what you're looking for, but I recently added a CSV Lint plug-in to Notepad++ which also adds syntax highlighting for csv and fixed width data files, meaning each column gets a different color so it's easier to see.

CSV Lint syntax highlighting columns

4
  • 2
    Lifesaver! Working on a server without Excel and your plugin for Notepad++ is very useful! Commented Nov 19, 2021 at 11:49
  • 1
    Alternative for viewing CSV on a server without Excel is using PowerShell's Out-GridView, but this is slow for a larger CSV file. Import-csv -LiteralPath $file.FullName -Delimiter $CSVDelimiter -Encoding UTF8 | Out-GridView -Title $file.FullName Commented Nov 19, 2021 at 11:50
  • fyi opening large files was slow in older versions of the plugin, but as of v0.4.5 there was a big performance increase
    – BdR
    Commented Aug 20, 2022 at 9:59
  • 1
    to be clear, CSV Lint can vertically align the data
    – Mafu Josh
    Commented Aug 2, 2023 at 16:04
16

Notepad++ CSVLint

  • Install CSVLint Plugin (Plugins > Plugins Admin... > CSV Lint)
  • Open CSV file. Or manually set Language > CSVLint. This will give you nicely colored output.

enter image description here

To reformat do this:

  • Open lower pane: Plugins > CSV Lint > CSV Lint Window.
  • Click the Reformat button. Check the box Align vertically (not recommended). -- This may screw up your data, so think twice before clicking OK.

enter image description here

Reformatted output: enter image description here

FYI: You can use the Trim all values checkbox to reformat back.

If you want to try this yourself: Here is my sample input:

TIMESTAMP_START,TIMESTAMP_END,TA_ERA,TA_ERA_NIGHT,TA_ERA_NIGHT_SD,TA_ERA_DAY,DA_ERA_DAY_SD,SW_IN_ERA,HH,DD,WW-YY,SW_IN_F,HH
19890101,19890107,3.436,1.509,2.165,6.134,2.889,100.233,283.946,1.373,99.852,2.748,1.188
19890108,19890114,3.814,2.446,2.014,5.728,2.526,91.708,286.451,1.575,100,100.841,0.742

Notepad++ plugins are fragile. This worked for me on Notepad++ v8.4.2 (32-bit). So if this doesn't work you, then consider switching from 64-bit to 32-bit and also downgrading to this exact version and bit-ness.

Related: CSVLint demo video, CSVLint Documentation on GitHub

5
  • Thank you, this is available in 64-bit whereas TextFX is not. Commented Feb 15, 2023 at 14:33
  • @RevitArkitek: I have given up on NPP x64. I had too many problems with plugins. So for me it's gonna be all NPP x86 until something FORCES me to change. Commented Feb 23, 2023 at 11:53
  • 1
    BEST Answer... you can even reformat the data back if needing to. Such a perfect plugin to work with CSV data. I've been dreaming about something like this for years. I thought this would ever only exist in my dreams. Thank you to the community for informing me of this amazing plugin. I hope it doesn't die off like TextFX Plugin did. I'll pour some out for the homies. Commented May 12, 2023 at 20:46
  • @CodeNovice: Try with 32bit NPP => Plugins Admin => "NppTextFX2". Works nicely. (Seems to be a fork or something: github.com/rainman74/NPPTextFX2) Commented Feb 29 at 14:50
  • 1
    @StackzOfZtuff I Just used the Admin Plugins page in Notepad++ to install and voila. Thank you very much for the heads up. Glad to see it back! Commented Feb 29 at 17:53
9

You can use this python plugin script which utilizes the csv library which takes care of quoted csv and many other variants.

Setup:

  1. Use the plugin manager in Notepad++ to install the "Python script" plugin.
  2. Plugins->Python Script->New Script (name it something like CSVtoTable.py)
  3. Paste the following python script into the new file and save:

CSVtoTable.py

import csv

inputlines = editor.getText().split('\n')
# Get rid of empty lines
inputlines = [line.strip() for line in inputlines if line.strip()]
reader = csv.reader(inputlines, delimiter=',')
csvlist = [line for line in reader]
# transpose to calculate the column widths and create a format string which left aligns each row
t_csvlist = zip(*csvlist)
col_widths = [max([len(x) for x in t_csvlist[y]]) for y in range(len(t_csvlist))]
# To right align - change < to >
fmt_str = ' '.join(['{{:<{0}}}'.format(x) for x in col_widths]) + '\r\n'

text = []
for line in csvlist: 
    text.append(fmt_str.format(*line))

# open a new document and put the results in there.    
notepad.new()
editor.addText(''.join(text))
  1. Open your CSV file in notepad++
  2. Click on Plugins->Python Script->Scripts->(The name you used in step 2)
  3. A new tab with the formatted data should open.

Update (right aligned numbers & left aligned strings):

Use the following python script if you want to right align number fields from the CSV - it looks at the second line of the csv to determine the types of the fields.

import csv
import re

num_re = re.compile('[-\+]?\d+(\.\d+)?')

inputlines = editor.getText().split('\n')
# Get rid of empty lines
inputlines = [line.strip() for line in inputlines if line.strip()]
reader = csv.reader(inputlines, delimiter=',')
csvlist = [line for line in reader]

# Transpose to calculate the column widths and create a format string which left aligns each row
t_csvlist = zip(*csvlist)
col_widths = [max([len(x) for x in t_csvlist[y]]) for y in range(len(t_csvlist))]

# Numbers get right aligned
type_eval_line = csvlist[1 if len(csvlist)>1 else 0]
alignment = ['>' if num_re.match(item) else '<' for item in type_eval_line]

# Compute the format string
fmt_str = ' '.join(['{{:{0}{1}}}'.format(a,x) for x,a in zip(col_widths,alignment)]) + '\r\n'

text = []
for line in csvlist: 
    text.append(fmt_str.format(*line))

# open a new document and put the results in there.    
notepad.new()
editor.addText(''.join(text))
1
  • 1
    The only plugin I see for Python is Python Indent. I don't see Python Script in the list of plugins.
    – Rhyous
    Commented Aug 14, 2019 at 14:16
4

You could use Search&Replace to change all occurrences of , to ,\t. This will add a tab after each ,.

This method has however some drawbacks:

  1. you effectively add white-space characters to your document (in case you need to edit and save it).
  2. This works well only if the difference (in terms of number of characters) between the longest and the shortest numbers is less than 1 tab-size (usually 4 characters).
4
  • Thanks! Now the display effect is quite better, although there are some that cannot align vertically because of the reason you mentioned. Is there some other method to improve it ?
    – T X
    Commented Jul 19, 2017 at 1:19
  • Well, let's say that Notepad++, like any other text editor, is not really the right tool to use for what you want to achieve... If you are for some reason bound to Notepad++, the only further improvement I can think of is using Search&Replace with regular expressions: making multiple iterations to search for strings of length n and pad them with m leading spaces, than search for strings of length n+1 and pad them with m-1 leading spaces, and so on....
    – Martin S
    Commented Jul 19, 2017 at 11:37
  • 1
    What you say "Notepad++, like any other text editor, is not really the right tool to use for what you want to achieve" truly enlightened me ! Now I use Excel to align vertically and it works well ^_^.
    – T X
    Commented Jul 19, 2017 at 14:34
  • 2
    You can also use the Elastic Tabstops Plugin that is included in NPP to address drawback number 2. Commented May 3, 2019 at 10:56
0

As noted in the comments not TextFX (32-bit only, Line Up feature has a not expected formatting), nor NppTextFX2 (64-bit only, Line Up feature is removed).

Another solution is ElasticTabstops: https://github.com/mariusv-github/ElasticTabstops (archive: https://github.com/dail8859/ElasticTabstops)

Side works:

To test:

  1. Replace column separator character to tabulation character or, for example, copy table from https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B into new tab.
  2. (optionally) Check plugins->Elastic Tabstops->Enable, plugins->Elastic Tabstops->Selection only to convert into table view on-a-fly for a selected text. You can even use a block selection (ALT+SHIFT) to immediately select a formatted piece of text.
  3. Select plugins->Elastic Tabstops->Convert Elastic Tabs to Spaces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.