14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

カーソル前のキャメルケースをスネークケースに変換

Posted at

スネークケースって打ちづらいですよね?
アンダースコアを余分に打たないといけないし、アンダースコアが遠くて打ちづらいし。

そこで、
アッパーキャメルケースの末尾で関数を実行すると、大文字のスネークケースに変換、
ローワーキャメルケースの末尾で関数を実行すると、小文字のスネークケースに変換
する関数を作りました。

UpperCamelCase
    => UPPER_CAMEL_CASE

lowerCamelCase
    => lower_camel_case
(global-set-key (kbd "M-u") 'camel-to-snake-backward-word)
(defun camel-to-snake-backward-word ()
  (interactive)
  (let ((case-fold-search nil)
        (s (buffer-substring
            (point) (save-excursion (forward-word -1) (point)))))
    (delete-region (point) (progn (forward-word -1) (point)))
    (insert (funcall (if (= (string-to-char s) (downcase (string-to-char s)))
                         'downcase 'upcase)
                     (replace-regexp-in-string
                      "\\([A-Z]\\)" "_\\1"
                      (store-substring s 0 (downcase (string-to-char s))))))))
14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?