7

I'm looking for either some built-in setting or a simple mode for viewing and editing tab-separated columns (TSV files).

All I need is for the TSV text to be displayed in a way that respects the vertical alignment of the columns, and the horizontal alignment of the rows. If line-wrapping could be toggled that would be sweet, but secondary.

Searching for column viewing in emacs is made difficult by the fact that the word "column" already has a different meaning in Emacs.

(I specified "simple" to discourage answers like org-mode's column view. At the moment my needs are extremely simple, and I don't want to contend with installing, loading, and learning about a monster mode like org.)

kjo
  • 3,145
  • 14
  • 42

2 Answers2

7

This should give you a chance to try out Org tables with virtually no learning curve. Place the following code in your init file and run it:

(defun my-export-to-parent ()
  "Exports the table in the current buffer back to its parent DSV file and
    then closes this buffer."
  (let ((buf (current-buffer)))
    (org-table-export parent-file export-func)
    (set-buffer-modified-p nil)
    (switch-to-buffer (find-file parent-file))
    (kill-buffer buf)))

(defun my-edit-dsv-as-orgtbl (&optional arg)
  "Convet the current DSV buffer into an org table in a separate file. Saving
    the table will convert it back to DSV and jump back to the original file"
  (interactive "P")
  (let* ((buf (current-buffer))
         (file (buffer-file-name buf))
         (txt (substring-no-properties (buffer-string)))
         (org-buf (find-file-noselect (concat (buffer-name) ".org"))))
    (save-buffer)
    (with-current-buffer org-buf
      (erase-buffer)
      (insert txt)
      (org-table-convert-region 1 (buffer-end 1) arg)
      (setq-local parent-file file)
      (cond 
       ((equal arg '(4)) (setq-local export-func "orgtbl-to-csv"))
       ((equal arg '(16)) (setq-local export-func "orgtbl-to-tsv"))
       (t (setq-local export-func "orgtbl-to-tsv")))
      (add-hook 'after-save-hook 'my-export-to-parent nil t))
    (switch-to-buffer org-buf)
    (kill-buffer buf)))

;; Open the current TSV file as an Org table
(global-set-key (kbd "C-c |") 'my-edit-dsv-as-orgtbl)

Now, from a TSV file you can press C-c | and your table will be converted to an Org table. (With C-u C-c | it will work for a CSV file too.) Once you are done editing the Org table, press C-x C-s and the Org table will be converted back to TSV and saved back into the original file.

nispio
  • 8,175
  • 2
  • 35
  • 73
  • This works very well for small tables. However maybe due to the way `org-mode` deals with tables, things start getting slow, lagged response when tables has about 1000 rows and 10 columns for my case. – biocyberman Apr 15 '16 at 07:46
0

I got this to work as follows:

  1. Install csv-mode from https://elpa.gnu.org/packages/csv-mode.html
  2. Make sure that csv-separators contains "\t", e.g. by evaluating (setq csv-separators '(" "))
  3. Open the tsv file
  4. M-x csv-align-mode