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.