Is there any way to convert camel-cased names to use underscores in emacs?

Emacs has glasses-mode which displays camelcase names with underscores in between. (See also http://www.emacswiki.org/emacs/GlassesMode).

If you want to actually change the text of the file M-x query-replace-regexp is probably suitable.


This small bit of code from this page, with a wrapper function and an underscore replacing the hyphen with an underscore, could easily be turned into a command to do that. (Check that it treats leading caps to suit you):

Sample EmacsLisp code to un-CamelCase a string (from http://www.friendsnippets.com/snippet/101/):

(defun un-camelcase-string (s &optional sep start)
  "Convert CamelCase string S to lower case with word separator SEP.
Default for SEP is a hyphen \"-\".

If third argument START is non-nil, convert words after that
index in STRING."
  (let ((case-fold-search nil))
    (while (string-match "[A-Z]" s (or start 1))
      (setq s (replace-match (concat (or sep "-") 
                                             (downcase (match-string 0 s))) 
                                     t nil s)))
    (downcase s)))

Moritz Bunkus wrote an elisp function to toggle between CamelCase and c_style

Tags:

Emacs

Elisp