명령줄에서 파일을 편집하려면 어떻게 합니까?

명령줄에서 파일을 편집하려면 어떻게 합니까?

2022-10-18 last update

10 minutes reading other shell
저자: 팔코 팀메, 히만슈 아로라, 틸 브렘
이 자습서에서는 명령줄에서 파일을 편집하는 방법을 보여줍니다. 이 기사에서는 vi(또는 vim), nano 및 emacs의 세 가지 명령줄 편집기를 다룹니다. 이러한 편집기는 Arch Linux, CentOS, Debian, Fedora 및 Ubuntu와 같은 모든 Linux 배포판에서 사용할 수 있습니다.

Vi 또는 Vim 명령줄 편집기로 파일 편집하기


명령줄에서 파일을 편집하려면 vi와 같은 편집기를 사용할 수 있습니다. 파일을 열려면 다음을 실행하십시오.
vi /path/to/file

Now you see the contents of the file (if there is any. Please note that the file is created if it does not exist yet.).

As an example. I opened the /etc/passwd file with vi:

Linux vi Editor

The most important commands in vi are these:

Press i to enter the Insert mode. Now you can type in your text.

To leave the Insert mode press ESC.

To delete the character that is currently under the cursor you must press x (and you must not be in Insert mode because if you are you will insert the character x instead of deleting the character under the cursor). So if you have just opened the file with vi, you can immediately use x to delete characters. If you are in Insert mode you have to leave it first with ESC.

If you have made changes and want to save the file, press :x (again you must not be in Insert mode. If you are, press ESC to leave it).

If you haven't made any changes, press :q to leave the file (but you must not be in Insert mode).

If you have made changes, but want to leave the file without saving the changes, press :q! (but you must not be in Insert mode).

Please note that during all these operations you can use your keyboard's arrow keys to navigate the cursor through the text.

So that was all about the vi editor. Please note that the vim editor also works more or less in the same way, although if you'd like to know vim in depth, head here.

Nano 명령줄 편집기로 파일 편집

Next up is the Nano editor. You can invoke it simply by running the 'nano' command:

nano

Here's how the nano UI looks like:

You can also launch the editor directly with a file.

nano [filename]

For example:

nano test.txt

The UI, as you can see, is broadly divided into four parts. The line at the top shows editor version, the file being edited, and the editing status. Then comes the actual edit area where you'll see the contents of the file. The highlighted line below the edit area shows important messages, and the last two lines are really helpful for beginners as they show keyboard shortcuts that you use to perform basic tasks in nano.

So here's a quick list of some of the shortcuts that you should know upfront.

Use arrow keys to navigate the text, the Backspace key to delete text, and Ctrl+o to save the changes you make. When you try saving the changes, nano will ask you for confirmation (see the line below the main editor area in screenshot below):

Note that at this stage, you also have an option to save in different OS formats. Pressing Altd+d enables the DOS format, while Atl+m enables the Mac format.

Press enter and your changes will be saved.

Moving on, to cut and paste lines of text use Ctrl+k and Ctrl+u. These keyboard shortcuts can also be used to cut and paste individual words, but you'll have to select the words first, something you can do by pressing Alt+A (with the cursor under the first character of the word) and then using the arrow to key select the complete word.

Now comes search operations. A simple search can be initiated using Ctrl+w, while a search and replace operation can be done using Ctrl+\.

So those were some of the basic features of nano that should give you a head start if you're new to the editor. For more details, read our comprehensive coverage here.

Emacs 명령줄 편집기로 파일 편집하기

Next comes Emacs. If not already, you can install the editor on your system using the following command:

sudo apt-get install emacs

Like nano, you can directly open a file to edit in emacs in the following way:

emacs -nw [filename]

Note: The -nw flag makes sure emacs launches in bash itself, instead of a separate window which is the default behavior.

For example:

emacs -nw test.txt
에디터의 UI는 다음과 같습니다.

nano와 마찬가지로 emacs UI도 여러 부분으로 나뉩니다. 첫 번째 부분은 그래픽 응용 프로그램에서 볼 수 있는 것과 유사한 최상위 메뉴 영역입니다. 그런 다음 열었던 파일의 텍스트가 표시되는 기본 편집 영역이 나타납니다.
편집 영역 아래에는 파일 이름, 편집 모드(위 스크린샷의 '텍스트') 및 상태(수정의 경우 **, - 수정되지 않은 경우, 읽기 전용인 경우 %%)와 같은 항목을 표시하는 또 다른 강조 표시된 막대가 있습니다. 그런 다음 입력 지침을 제공하는 마지막 영역이 나옵니다. 출력도 참조하십시오.
이제 기본 작업으로 이동하여 변경한 후 저장하려면 Ctrl+x를 누른 다음 Ctrl+s를 사용합니다. 마지막 섹션은 'Write ........' 줄에 있는 메시지를 보여줍니다. 다음은 예입니다.

이제 변경 사항을 취소하고 편집기를 종료하려면 Ctrl+x 다음에 Ctrl+c를 사용하십시오. 편집기는 프롬프트를 통해 이를 확인합니다. 아래 스크린샷을 참조하십시오.

'n' 다음에 'yes'를 입력하면 편집기가 변경 사항을 저장하지 않고 종료됩니다.
Emacs는 'Ctrl'을 'C'로, 'Alt'를 'M'으로 나타냅니다. 예를 들어 C-x와 같은 것을 볼 때마다 Ctrl+x를 의미합니다.
다른 기본 편집 작업과 마찬가지로 삭제는 우리 대부분이 이미 익숙한 백스페이스/삭제 키를 통해 작동하므로 간단합니다. 그러나 삭제 경험을 원활하게 만드는 바로 가기가 있습니다. 예를 들어 전체 줄을 삭제하려면 Ctrl+k를, 단어를 삭제하려면 Alt+d를, 문장을 삭제하려면 Alt+k를 사용합니다.
실행 취소는 'Ctrl+x' 다음에 'u'를 눌러 수행하고 다시 실행하려면 Ctrl+g를 누른 다음 Ctrl+_를 누릅니다. 정방향 검색에는 Ctrl+s를 사용하고 역방향 검색에는 Ctrl+r을 사용합니다.

계속해서 교체 작업을 시작하려면 Alt+Shift+% 키보드 단축키를 사용하십시오. 대체할 단어를 묻는 메시지가 표시됩니다. 입력하세요. 그러면 편집자가 교체를 요청할 것입니다. 예를 들어, 다음 스크린샷은 사용자에게 'This'라는 단어를 대체할지 묻는 emacs를 보여줍니다.

대체 텍스트를 입력하고 Enter 키를 누릅니다. emacs가 수행할 각 교체 작업에 대해 먼저 귀하의 허가를 구합니다.

'y'를 누르면 단어가 바뀝니다.

이것이 emacs 사용을 시작하기 위해 알아야 하는 거의 모든 기본 편집 작업입니다. 아, 그리고 맨 위에 있는 메뉴들입니다. 액세스 방법에 대해서는 논의하지 않았습니다. F10 키를 사용하여 액세스할 수 있습니다.

이 메뉴에서 나오려면 Esc 키를 세 번 누릅니다.