Converting from camelcase to _ in emacs

前端 未结 7 1962

Is there an emacs function to convert a camel-cased word to underscore? Something, like:

longVariableName

M-x to-underscore

7条回答
  •  太阳男子
    2021-02-05 03:19

    I use this when converting C# code to PHP.

    (defun un-camelcase-word-at-point ()
      "un-camelcase the word at point, replacing uppercase chars with
    the lowercase version preceded by an underscore.
    
    The first char, if capitalized (eg, PascalCase) is just
    downcased, no preceding underscore.
    "
      (interactive)
      (save-excursion
        (let ((bounds (bounds-of-thing-at-point 'word)))
          (replace-regexp "\\([A-Z]\\)" "_\\1" nil
                          (1+ (car bounds)) (cdr bounds))
          (downcase-region (car bounds) (cdr bounds)))))
    

    And then in my php-mode fn:

    (local-set-key "\M-\C-C"  'un-camelcase-word-at-point)
    

提交回复
热议问题