programing tip

Emacs를 사용하여 파일의 읽기 / 쓰기 모드를 어떻게 변경합니까?

itbloger 2020. 7. 11. 10:56
반응형

Emacs를 사용하여 파일의 읽기 / 쓰기 모드를 어떻게 변경합니까?


파일이 읽기 전용 모드로 설정되어 있으면 Emacs 내에서 파일을 쓰기 모드로 변경하거나 그 반대로 변경하려면 어떻게해야합니까?


MX 토글 읽기 전용

또는 최신 버전의 Emacs

MX 읽기 전용 모드

내 Windows 상자에서 Alt-x에 도달하면 메타 프롬프트가 표시되고 "toggle-read-only"를 입력하여 올바른 elisp 함수를 호출합니다.

기본 키보드 바인딩을 사용하는 경우

Cx Cq

( "Control-X Control-Q"라고 큰 소리로 읽음) 효과는 동일합니다. 그러나 emacs가 본질적으로 무한정 재구성 가능하다는 점을 감안하면 마일리지가 다를 수 있습니다.

주석에서 후속 조치 : 버퍼의 쓰기 가능 상태는 파일의 쓰기 가능 권한을 변경하지 않습니다. 읽기 전용 파일 에 쓰려고 하면 확인 메시지가 나타납니다. 그러나 파일을 소유 한 경우 파일에 대한 권한을 변경 하지 않고 변경 사항 작성할 수 있습니다 .

쓰기 권한 추가, 변경 쓰기, 쓰기 권한 제거의 여러 단계를 거치지 않고 파일을 빠르게 변경하려는 경우 매우 편리합니다. 나는 마지막 단계를 잊어 버리고 나중에 실수로 변경 될 수 있도록 잠재적으로 중요한 파일을 열어 둡니다.


'file'과 'buffer'를 혼동하지 마십시오. C-x C-q( toggle-read-only)를 사용 하여 버퍼를 읽기 전용으로 설정하고 다시 되돌릴 수 있습니다 . 파일을 읽을 수는 있지만 쓸 수는 없지만 파일을 방문 할 때 얻는 버퍼 ( C-x C-f또는 find-file)는 자동으로 읽기 전용 모드로 설정됩니다. 파일 시스템의 파일에 대한 권한을 변경하려면 파일 dired이 포함 된 디렉토리에서 시작 하십시오. dired에 대한 설명서는 info에서 찾을 수 있습니다. C-h i (emacs)dired RET.


내가 찾은 것은 M-x set-file-modes filename mode

내 Windows Vista 상자에서 작동했습니다. 예를 들면 다음과 같습니다.M-x set-file-modes <RET> ReadOnlyFile.txt <RET> 0666


다른 사람이 언급했듯이 Mx 토글 읽기 전용 이 작동합니다.

그러나 이것은 더 이상 사용되지 않으며 Mx 읽기 전용 모드 는 현재 Cx Cq 키 바인딩으로 설정되어 있습니다.


CTRL + X + CTRL + Q     


파일이 아닌 버퍼 만 읽기 전용 인 경우 toggle-read-only일반적으로에 바인딩 된을 사용할 수 있습니다 C-x C-q.

그러나 파일 자체가 읽기 전용 인 경우 다음 기능이 유용 할 수 있습니다.

(defun set-buffer-file-writable ()
  "Make the file shown in the current buffer writable.
Make the buffer writable as well."
  (interactive)
  (unix-output "chmod" "+w" (buffer-file-name))
  (toggle-read-only nil)
  (message (trim-right '(?\n) (unix-output "ls" "-l" (buffer-file-name)))))

이 함수에 의존 unix-output하고 trim-right:

(defun unix-output (command &rest args)
  "Run a unix command and, if it returns 0, return the output as a string.
Otherwise, signal an error.  The error message is the first line of the output."
  (let ((output-buffer (generate-new-buffer "*stdout*")))
    (unwind-protect
     (let ((return-value (apply 'call-process command nil
                    output-buffer nil args)))
       (set-buffer output-buffer)
       (save-excursion 
         (unless (= return-value 0)
           (goto-char (point-min))
           (end-of-line)
           (if (= (point-min) (point))
           (error "Command failed: %s%s" command
              (with-output-to-string
                  (dolist (arg args)
                (princ " ")
                (princ arg))))
           (error "%s" (buffer-substring-no-properties (point-min) 
                                   (point)))))
         (buffer-substring-no-properties (point-min) (point-max))))
      (kill-buffer output-buffer))))

(defun trim-right (bag string &optional start end)
  (setq bag (if (eq bag t) '(?\  ?\n ?\t ?\v ?\r ?\f) bag)
    start (or start 0)
    end (or end (length string)))
  (while (and (> end 0)
          (member (aref string (1- end)) bag))
    (decf end))
  (substring string start end))

에 기능을 배치 ~/.emacs.el하고 평가하거나 emacs를 다시 시작하십시오. 그런 다음을 사용하여 현재 버퍼의 파일을 쓸 수있게 만들 수 있습니다 M-x set-buffer-file-writable.


파일 디렉토리 (디렉토리)를보고있는 경우 파일 Shift + M이름을 modespec사용하고 chmod명령에 사용 된 것과 동일한 속성을 입력 할 수 있습니다 .
M modespec <RET>

See the other useful commands on files in a directory listing at http://www.gnu.org/s/libtool/manual/emacs/Operating-on-Files.html


I tried out Vebjorn Ljosa's solution, and it turned out that at least in my Emacs (22.3.1) there isn't such function as 'trim-right', which is used for removing an useless newline at the end of chmod output.

Removing the call to 'trim-right' helped, but made the status row "bounce" because of the extra newline.


C-x C-q is useless. Because your also need the permission to save a file.

I use Spacemacs. It gives me a convenient function to solve this question. The code is following.

(defun spacemacs/sudo-edit (&optional arg)
  (interactive "p")
  (if (or arg (not buffer-file-name))
      (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))

I call spacemacs/sudo-edit to open a file in emacs and input my password, I can change the file without read-only mode.

You can write a new function like spacemacs/sudo-edit.

참고URL : https://stackoverflow.com/questions/180910/how-do-i-change-read-write-mode-for-a-file-using-emacs

반응형