Can I set the field seperator(s) for the current buffer in Emacs 23?
3 Answers
Sure -- although, by default, you can only do so globally. To do so, customize the variable csv-separators
(M-x customize-variable RET csv-separators RET
) and add the separator characters of your choice to the list. (See C-h v csv-separators RET
/M-x describe-variable RET csv-separators RET
for documentation on the variable, including some caveats worth knowing if you're going to change its value.)
If you really need to apply the change only to a given buffer, you can try making the variable buffer-local by means of make-local-variable
(e.g. M-: (make-local-variable 'csv-separators) RET
), then setting the value for the current buffer directly (e.g. M-: (setq csv-separators (append '(":") csv-separators))
). Be warned, though: I haven't tried this, and don't know whether or not it will work.
This expands on the accepted answer. I would have commented there, but need >50 rep for that. I just found this and the make-local-variable
approach did not work for me. I found the variable csv-separator-chars
and its doc says
Set by customizing ‘csv-separators’ -- do not set directly!
So using customize-variable
on csv-separators
worked for me as this also set the csv-separator-chars
which seems to be the one really used when aligning CSVs.
The accepted answer did not work for me either: although inspecting csv-separators
shows that its value is local, it seems to have no effect.
But since May 2022 (corresponding to csv-mode-1.20, I assume) there is a dedicated function:
Add CSV separator guessing functionality
Add two new commands: `csv-guess-set-separator' that automatically
guesses and sets the CSV separator of the current buffer, and
`csv-set-separator' for setting it manually.
`csv-guess-set-separator' can be useful to add to the mode hook to
have CSV mode guess and set the separator automatically when visiting
a buffer:
(add-hook 'csv-mode-hook 'csv-guess-set-separator)