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.)

  • 3
    You don't need to learn every feature that org-mode offers just to view a file in column view. – nispio Oct 10 '14 at 14:12
  • Not an answer, but maybe helpful as a general tip: There is a built-in command for toggling line-wrapping: toggle-truncate-lines. – itsjeyd Oct 10 '14 at 14:31
  • 5
    You might want to look at csv-mode in ELPA. It supports setting a different separator character from ,. – Vamsi Oct 10 '14 at 16:33
  • @Vamsi please consider posting csv-mode as an answer. – Jordon Biondo Mar 24 '16 at 1:02
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.

  • 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 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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