티스토리 뷰

Research (연구 관련)

sudo w

홍돌 2019. 6. 1. 14:45

credit to https://catonmat.net/sudo-vim

 

How to save read-only files in vim

How many times have you had a situation when you open a file for editing, make a bunch of changes, and discover that you don't have the rights to write the file? This has happened to me a lot. It usually goes like this. You open a file and you forget to us

catonmat.net

This is a merely Korean translation for the post above


:w !sudo tee % >/dev/null

That's it.

:w ! blabla는 현재 열은 파일을 blabla의 stdin으로 적는(write) 역할을 한다. stdin = standard input. 표준입력장치 tee % 에서 %는 현재 열은 파일을 의미하고, tee는 특별한 command이다. sudo tee FILENAME을 하게 되면 tee의 stdin에 들어오는 content를 FILENAME로 적게된다. 결국 :w ! sudo tee %는 타겟파일 -> tee stdin -> 타겟파일의 구조인 셈. 마지막으로 >/dev/null은 tee의 stdout을 무시하게 하는 역할이다. vim에서 사용자가 stdout을 볼 필요는 없으니까.

마지막으로 :!qa로 파일을 닫고 다시 들어가보면 변경사항이 저장되어있다.

하지만 당연히 가장 좋은 건 애초에 파일을 sudo로 여는 것이다 하하

'Research (연구 관련)' 카테고리의 다른 글

Optical Flow  (0) 2019.06.06
Install cmake  (0) 2019.06.01
Binocular Disparity  (0) 2019.05.27
Ad hoc categories  (0) 2019.05.23
Why do we need validation set?  (0) 2019.05.15
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함