0

I use csv-mode with csv-align-mode to look at CSVs with header rows.

For small CSVs (not many columns, not many rows) it is easy to determining in which column the current cell (i.e. the cell with the cursor) is. (I just look upwards from the cell to the top row.)

However for huge CSVs (many columns, many rows) this doesn't work well. So I was hoping there might be a command (or an additional minor mode with a command) to display something like e.g.: "Cell under cursor is in col 'Name', row 13124"

halloleo
  • 1,215
  • 9
  • 23

2 Answers2

2

Once you have turned on csv-align-mode, you can also call csv-header-line to display the field names in the buffer header, also correctly aligned.

https://emacs.stackexchange.com/a/37332/9982

db48x
  • 15,741
  • 1
  • 19
  • 23
1

You should turn on csv-align-mode if you haven't already. That way moving to the top vertically should land you on the right column header.

I found this in csv-mode.el:

;; The global minor mode `csv-field-index-mode' provides display of
;; the current field index in the mode line, cf. `line-number-mode'
;; and `column-number-mode'.  It is on by default.

and it seems to work for me. The row number is assumed to be the same as the line number; the field number however seems to work fine in all cases. The mode is enabled by default.

So not quite as nice as your desired interface, but not too far off. AFAICT, there is no csv-move-to-field function, which would make it easy to implement your interface.

EDIT: Actually, there is csv-forward-field which takes an argument of how many fields to move forward (similarly for csv-backward-field). So it is possible to get to the column name with something like this:

  • Let N be the current field number.
  • Set the mark (or use save-excursion in a function)
  • Go to the beginning of the buffer (assuming that's where the column names are.
  • ESC N M-x csv-forward-field and get the column name (the cursor should be positioned after the name).
  • Return to the previous place with C-x C-x.

Writing a function to do that should be easy given the current field number: the function csv--field-index should come in handy for that.

So here's a function that gets you the column name of the field that your cursor is currently on:

(defun my/csv-field-name ()
  (interactive)
  (let ((field (csv--field-index)))
    (save-excursion
      (goto-char (point-min))
      (csv-forward-field field)
      (buffer-substring (save-excursion (csv-backward-field 1) (point)) (point)))))

which is pretty much a transcription of the interactive algorithm above.

NickD
  • 27,023
  • 3
  • 23
  • 42
  • 1
    Very cool use of a bit of Elisp! Thanks heaps. However I do agree that the other solution is simpler. – halloleo Apr 28 '22 at 08:40