Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For instance, I want to convert "CamelCasedName" to "camel_cased_name". Is there a way to do this in emacs?

share|improve this question
1  
The short answer to any question of the form "Is there any way to _____ in emacs?" is Always "YES" –  Brian Postow Apr 9 '10 at 21:33
add comment

4 Answers 4

up vote 4 down vote accepted

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)))
share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
the package string-inflection is more complete now: github.com/akicho8/string-inflection (note that there's also string-inflection-camelize-lower to change hello_world to helloWorld). –  Ehvince May 7 at 12:42
add comment

For display purposes only, you can use this:

M-x glasses-mode

If you want a script which actually converts the text, I imagine you'd have to write some elisp. That question is better asked on stack overflow.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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