34

是否有一个 emacs 函数可以将骆驼大小写的单词转换为下划线?就像是:

longVariableName

M-x to-underscore

long_variable_name

4

7 回答 7

40

使用 MELPA 或https://github.com/akicho8/string-inflection上提供的 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
于 2014-12-11T12:23:02.107 回答
25

我使用以下内容在驼峰和下划线之间切换:

(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)))))))
于 2014-09-17T09:03:24.997 回答
21
(progn (replace-regexp "\\([A-Z]\\)" "_\\1" nil (region-beginning) (region-end))
       (downcase-region (region-beginning) (region-end)))
于 2012-02-17T10:56:55.137 回答
10

我在将 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)))))

然后在我的 php-mode fn 中:

(local-set-key "\M-\C-C"  'un-camelcase-word-at-point)
于 2013-03-06T17:34:21.693 回答
7

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"
    
  2. 降低驼峰情况的任何情况:

    (s-lower-camel-case "some words") ;; => "someWords"
    (s-lower-camel-case "dashed-words") ;; => "dashedWords"
    (s-lower-camel-case "under_scored_words") ;; => "underScoredWords"
    

在其 repo 中查看更多示例。

于 2018-08-30T15:25:28.120 回答
1

如果您想使用s.el获得完整的代码:

(defun to-snake-case (start end)
  "Change selected text to snake case format"
  (interactive "r")
  (if (use-region-p)
      (let ((camel-case-str (buffer-substring start end)))
        (delete-region start end)
        (insert (s-snake-case camel-case-str)))
    (message "No region selected")))
于 2020-05-12T06:44:04.027 回答
0

@ens 的答案很接近,但在 Emacs 26.1 上对我来说有点小问题。我修复了这个错误,并通过 Cu 前缀 arg 添加了功能,以指定是否希望驼峰式大小写的第一个字母为小写:

(defun toggle-camelcase-underscores (first-lower-p)
  "Toggle between camelcase and underscore notation for the
symbol at point. If prefix arg, C-u, is supplied, then make first
letter of camelcase lowercase."
  (interactive "P")
  (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
            (replace-string "_" " " nil start end)
            (upcase-initials-region start end)
            (replace-string " " "" nil start end)
            (when first-lower-p
              (downcase-region start (1+ start))))
        (replace-regexp "\\([A-Z]\\)" "_\\1" nil (1+ start) end)
        (downcase-region start (cdr (bounds-of-thing-at-point 'symbol)))))))
于 2019-10-07T18:45:12.617 回答