BIBLIOGRAPHY
“Emacs Evil Mode Regular Expression Search and Replace - Emacs - Makble.” n.d. Accessed March 29, 2023. http://makble.com/emacs-evil-mode-regular-expression-search-and-replace.
Related-Notes
2025 리마인드
괄호로 그룹 잡고, 그냥 둘 부분과 아닌 부분 확인하고 시행착오
이맥스에서 정규식을 이용해 텍스트를 검색 후, 일부 텍스트만 변경하려면?
예를 들어 영어알파벳2개 뒤에 나오는 숫자0을 숫자1로 바꾸는 경우에 활용할 수 있는 방법
이맥스에서 정규 표현식을 이용해 특정 패턴을 검색하고, 일부 텍스트를 변경하려면 replace-regexp
명령을 사용할 수 있습니다. 아래의 단계를 따라하세요:
-
검색할 텍스트 정의: 영어 알파벳 2개 뒤에 나오는 숫자 0을 대체하려면, 정규 표현식은 다음과 같습니다:
\([a-zA-Z][a-zA-Z]\)0
-
replace-regexp
사용:- 명령어 실행: =M-x replace-regexp=를 입력한 후 Enter.
- 검색할 패턴 입력:
\([a-zA-Z][a-zA-Z]\)0
입력 후 Enter. - 바꿀 내용 입력: =\1=를 입력한 후 다음에 변경할 숫자 =1=를 입력하여 =\1=으로 첫 번째 그룹(두 개의 알파벳)을 포함시키고, 후에 1을 붙입니다. 즉, 그냥 =\11=을 입력합니다.
-
변경 적용: Enter를 눌러서 적용합니다.
이제 영어알파벳 2개 뒤에 있는 숫자 0이 숫자 1로 변경되었습니다.
이맥스 Evil에서 VIM 스타일로 위의 작업을 하려면?
Evil 모드에서 Vim 스타일로 위의 작업을 수행하려면 :s
명령을 사용할 수 있습니다. 아래 단계를 따라주세요:
-
레인지 지정: 범위를 선택하거나 파일 전체에서 변경할 경우 범위를 지정하지 않아도 됩니다.
-
명령 실행: 아래와 같은 형식의 명령을 입력합니다:
:%s/\([a-zA-Z][a-zA-Z]\)0/\11/g
:%s
는 전체 파일에서 검색 및 대체를 의미합니다.\([a-zA-Z][a-zA-Z]\)0
는 두 개의 알파벳 뒤에 오는 0을 찾는 정규 표현식입니다.- =\11=은 첫 번째 그룹(알파벳)과 1을 결합한 것입니다.
/g
는 한 줄에 여러 번 일치하는 경우 모두 변경하겠다는 의미입니다.
-
Enter 키: 명령을 실행하기 위해 Enter 키를 눌러 완료합니다.
이제 해당 패턴의 모든 인스턴스가 변경됩니다.
Emacs Evil mode regular expression search and replace - Emacs - makble
(“Emacs Evil Mode Regular Expression Search and Replace - Emacs - Makble” n.d.)
One of the things I miss about VIM in Emacs is :%s
command, which allows you input pattern and replace text in one step. The Emacs M-x query-replace-regexp
do the same thing but a bit annoying, you need to input regular expression first then press enter, input replace text and press enter again and you need to press !
to execute replace for remaining matches or q
to quit or other moves. So many steps and confirmations, even just pasting I need paste two times. If you already accustomed to VIM’s way of search and replace, you would not like the Emacs way.
This makes me stay away from it for a long time. Sometimes I will copy the content of Emacs buffer to VIM and do substitution there. The Evil mode solved this problem and better than both of VIM and Emacs. You can use all Emacs features, but also have the access to most part of VIM.
The Evil mode search and replace is different things mixed together. Like VIM, you can type everything in one step, just input :%s
and input the rest. But it also use the Emacs regular expression syntax. The best thing is it highlight matches while you typing and show all kinds of hints message in minibuffer if the syntax is wrong or no matches found(this is the same as isearch-forward-regexp
), if you want to highlight all matches, input :%s///g
first and then backward a few characters to start write regular expression, you can see what will be replaced before execute substitution.
There is an Emacs plugin anzu
can highlight matches on the fly but you can not input pattern and replace text in one step, actually the rest is the same as Emacs except the highlight part. In VIM you can enable set incsearch
, but it only works for /
search not for %s
. I don’t yet know whether there is a VIM plugin can highlight matches on the fly for substitution. Seems there is no built in support for this.
Emacs 에서 VIM 에 대해 그리워하는 것 중 하나는 패턴을 입력하고 텍스트를 한 번에 바꿀 수있는 :%s
명령입니다. 이맥스의 M-x query-replace-regexp
는 같은 기능을 하지만 정규식을 먼저 입력한 후 엔터를 누르고 바꾸기 텍스트를 입력한 후 다시 엔터를 눌러야 하고, 나머지 일치하는 부분에 대해 바꾸기를 실행하려면 =!=를, 종료하거나 다른 동작을 하려면 =q=를 눌러야 하는 등 조금 번거롭습니다. 붙여넣기만 해도 두 번이나 붙여넣기를 해야 할 정도로 많은 단계와 확인이 필요합니다. 이미 VIM 의 검색 및 바꾸기 방식에 익숙하다면 Emacs 방식이 마음에 들지 않을 것입니다.
그래서 오랫동안 멀리하고 있습니다. 때때로 Emacs 버퍼의 내용을 VIM 에 복사하고 거기에서 대체를 수행합니다. 이블 모드는 이 문제를 해결해 주었고, VIM 과 이맥 모두보다 낫습니다. 모든 Emacs 기능을 사용할 수 있을 뿐만 아니라 VIM 의 대부분에 액세스할 수 있습니다.
이블 모드의 검색과 바꾸기는 서로 다른 기능이 섞여 있습니다. VIM 과 마찬가지로 모든 것을 한 번에 입력할 수 있으며 =:%s=만 입력하고 나머지는 입력하면 됩니다. 하지만 이 모드 역시 Emacs 정규식 구문을 사용합니다. 가장 좋은 점은 입력하는 동안 일치하는 부분을 강조 표시하고 구문이 잘못되었거나 일치하는 항목이 없는 경우 미니 버퍼에 모든 종류의 힌트 메시지를 표시한다는 것입니다(=isearch-forward-regexp=와 동일). 모든 일치 항목을 강조 표시하려면 =:%s///g=를 먼저 입력한 다음 몇 문자를 뒤로 돌려 정규식 작성을 시작하면 대체를 실행하기 전에 대체될 내용을 확인할 수 있습니다.
일치하는 항목을 즉석에서 강조 표시 할 수 있지만 패턴을 입력하고 텍스트를 한 번에 바꿀 수는 없으며 실제로 강조 표시 부분을 제외한 나머지는 Emacs 와 동일합니다. VIM 에서 set incsearch
를 활성화 할 수 있지만 /
검색에만 작동하고 %s
에는 작동하지 않습니다. 대체를 위해 즉석에서 일치 항목을 강조 표시 할 수있는 VIM 플러그인이 있는지 아직 알지 못합니다. 이에 대한 기본 지원은 없는 것 같습니다.
- Emacs multi-occur Search All Occurrences and List Search Results
- Emacs Evil mode regular expression search and replace
- 3 Ways to Replace string in Emacs
이탤릭 적용 제거
- \/(.*)\/
- \1