在emacs中从camelcase转换为_

Dav*_*hme 25 emacs camelcasing elisp

是否有emacs函数将驼峰式单词转换为下划线?就像是:

longVariableName

M-x to-underscore

long_variable_name

Nik*_*Nik 25

使用MELPA上提供的string-inflection包,或访问https://github.com/akicho8/string-inflection.

https://www.emacswiki.org/emacs/CamelCase复制的有用键盘快捷键:

;; Cycle between snake case, camel case, etc.
(require 'string-inflection)
(global-set-key (kbd "C-c i") 'string-inflection-cycle)
(global-set-key (kbd "C-c C") 'string-inflection-camelcase)        ;; Force to CamelCase
(global-set-key (kbd "C-c L") 'string-inflection-lower-camelcase)  ;; Force to lowerCamelCase
(global-set-key (kbd "C-c J") 'string-inflection-java-style-cycle) ;; Cycle through Java styles
Run Code Online (Sandbox Code Playgroud)


ens*_*ens 23

我使用以下内容在camelcase和underscores之间切换:

(defun toggle-camelcase-underscores ()
  "Toggle between camelcase and underscore notation for the symbol at point."
  (interactive)
  (save-excursion
    (let* ((bounds (bounds-of-thing-at-point 'symbol))
           (start (car bounds))
           (end (cdr bounds))
           (currently-using-underscores-p (progn (goto-char start)
                                                 (re-search-forward "_" end t))))
      (if currently-using-underscores-p
          (progn
            (upcase-initials-region start end)
            (replace-string "_" "" nil start end)
            (downcase-region start (1+ start)))
        (replace-regexp "\\([A-Z]\\)" "_\\1" nil (1+ start) end)
        (downcase-region start (cdr (bounds-of-thing-at-point 'symbol)))))))
Run Code Online (Sandbox Code Playgroud)


mat*_*thk 17

(progn (replace-regexp "\\([A-Z]\\)" "_\\1" nil (region-beginning) (region-end))
       (downcase-region (region-beginning) (region-end)))
Run Code Online (Sandbox Code Playgroud)

  • 分配`to-underscore`(所以你可以做`Mx to-underscore`):`(defun to-underscore()(interactive)(progn(replace-regexp“ \\([AZ] \\)”“ _ \\ 1“ nil(region-beginning)(region-end))(downcase-region(region-beginning)(region-end))))` (2认同)

Che*_*eso 9

我在将C#代码转换为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)))))
Run Code Online (Sandbox Code Playgroud)

然后在我的php模式fn中:

(local-set-key "\M-\C-C"  'un-camelcase-word-at-point)
Run Code Online (Sandbox Code Playgroud)


wha*_*old 5

2018年现在还有一个通用的方式:magnars/s.el:失传已久的Emacs字符串操作库。- github.com,关于 OP 问题的一些示例:

  1. 不管是什么情况下的蛇案(下划线分隔):

    (s-snake-case "some words") ;; => "some_words"
    (s-snake-case "dashed-words") ;; => "dashed_words"
    (s-snake-case "camelCasedWords") ;; => "camel_cased_words"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 无论如何降低骆驼案例:

    (s-lower-camel-case "some words") ;; => "someWords"
    (s-lower-camel-case "dashed-words") ;; => "dashedWords"
    (s-lower-camel-case "under_scored_words") ;; => "underScoredWords"
    
    Run Code Online (Sandbox Code Playgroud)

在其 repo 中查看更多示例。