37

I've been searching without finding for a while for a mode that makes editing huge tab/comma/colon-separated files easy. I've been wanting a mode that ensures that columns always line up, just like org-mode tables. I know I can easily turn the whole file into an org-mode table and then turn it back when I'm done, but that gets really slow with huge files, and is a hassle for quick edits (there's also the problem of what happens if a field contains a vertical bar). So does anyone know of either a mode or a built-in function/variable I can use so that I can get a file like

col1\tcol2\tcol3
very long column1\tcol2\tcol3

displayed like

col1               col2  col3
very long column1  col2  col3

? (perhaps with some color lining the separator)

2

4 Answers 4

16

Perhaps you could tell us what you've already found and rejected?

If you've been searching, then you must surely have seen http://emacswiki.org/emacs/CsvMode ? You don't mention it, or say why it wasn't any good, though.

SES (Simple Emacs Spreadsheet) might be a useful approach:

C-hig (ses) RET

You can create a ses-mode buffer and yank tab-delimited data into it (that's the import mechanism).

It's probably more hassle than you were after, though, and I'm not sure how well it will perform with "huge" files.

8
  • 3
    Note also that csv-mode is now in GNU ELPA (and that is not mentioned in the emacswiki because I happen to use an IP that's banned).
    – Stefan
    Commented May 16, 2012 at 13:32
  • Wow. How could I have missed that … (I was searching for table editing modes and tabs and such, never thought of searching for just csv.) Thanks!
    – unhammer
    Commented May 17, 2012 at 20:28
  • csv-mode not compiling for me on 24.2.1 on osx.
    – tenpn
    Commented Nov 21, 2012 at 11:27
  • @tenpn: I've just compiled it with M-x byte-compile-file on osx in 24.2.1. What errors do you get in Compile-Log buffer?
    – koddo
    Commented Dec 18, 2012 at 13:15
  • 1
    @DarrenRinger Maybe setting a goal column would help? C-x C-n to set and C-u C-x C-n to clear.
    – phils
    Commented Nov 1, 2019 at 23:17
14

Try csv-mode, which works in at least Emacs 24.

You can set the variable csv-separators to change the separator if you do not use the default one (comma).

See EmacsWiki.

6
  • To change or define new separators you can add next line to your .emacs file: (setq csv-separators '("," ";" "|" " "))
    – yurilo
    Commented Nov 1, 2012 at 12:19
  • 5
    I ended up using csv-mode, but aligning the whole file was too slow, so instead I use this keybinding that align just visible lines: (add-hook 'csv-mode-hook (lambda () (define-key csv-mode-map (kbd "C-c C-c") (defun csv-align-visible (&optional arg) "Align visible fields" (interactive "P") (csv-align-fields nil (window-start) (window-end)))))) (ideally it would align on scrolling, but I couldn't get that to work very well)
    – unhammer
    Commented Sep 15, 2014 at 8:03
  • Has anybody tried changing the separator? Regardless of which sign(s) I try it ends up using comma only. (emacs 25.2 / cvs-mode 1.50)
    – ivanhoe
    Commented Sep 12, 2017 at 7:36
  • @Markus: You need to customize the variable, and reapply the mode or reload the file.
    – choroba
    Commented Sep 12, 2017 at 7:56
  • @choroba: Yes, I did that. M-x describe-variable RET csv-separators reports "Its value is (";")". But strangely csv-separator-chars is still set to 44 (the ascii code for comma). I put this into my ~/.emacs and restarted emacs, so I don't think it's a reloading issue.
    – ivanhoe
    Commented Sep 12, 2017 at 8:04
5

As @choroba mentioned, use csv-mode. To answer your question specifically:

  • Make sure your separator is in csv-separators, which for example you can set with

    (setq csv-separators '("," "    "))
    
  • Use csv-align-fields (default keybinding C-c C-a) to line up the field values into columns.

@unhammer's comment about aligning only visible lines is great. Their code properly indented:

(add-hook 'csv-mode-hook
          (lambda ()
            (define-key csv-mode-map (kbd "C-c C-M-a")
              (defun csv-align-visible (&optional arg)
                "Align visible fields"
                (interactive "P")
                (csv-align-fields nil (window-start) (window-end))
                )
              )
            )
          )
3
  • 1
    Is it possible to align "selection" instead of visible?
    – SFbay007
    Commented Apr 21, 2020 at 2:52
  • @SFbay007: you can use (csv-align-fields nil (window-start) (window-end) instead in the code above. "Region" is the emacs term for selection.
    – mathrick
    Commented Jun 14, 2022 at 0:22
  • A small comment on "properly indented", it's very unlispy to leave hanging closing parentheses. Proper Lisp style calls for having all of them bunched together on the last non-whitespace line, so (csv-align-fields nil (window-start) (window-end))))))
    – mathrick
    Commented Jun 14, 2022 at 0:24
3

There is pretty-column.el, which I found in Group gnu.emacs.sources years ago (it was added in 1999). That group is now blocked, by Google. I just used pretty-column.el on a ~5000 line tab-separated text file that Org mode choked on (Org mode has a 999 line limit on converting such a file--for that reason). Added in edit: This seems to now be called delim-col.el (see this Emacs Wiki entry); the author is the same person.

2
  • wow, built-in to emacs, and useful for adding delimiters to rectangles and such too, thanks for the tip!
    – unhammer
    Commented Feb 4, 2021 at 16:06
  • This is a great command to format a file. Important to emphasize: it modifies the file to format it. So it is not just a display aid.
    – pglpm
    Commented Jul 10, 2023 at 19:15

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