如何从命令行Mac OS X编辑文件?

Iho*_*huk 10 macos command-line text-editor

如何在命令行中打开和编辑文件?我试着使用命令open:

sudo open -e /etc/apache2/extra/httpd-vhosts.conf
Run Code Online (Sandbox Code Playgroud)

但文件是Locked,我不能改变任何东西.

在此输入图像描述

我需要编辑很多文本并使用nanovim不是很方便.

Sin*_*rpa 10

sudo open -a TextEdit /etc/apache2/extra/httpd-vhosts.conf
Run Code Online (Sandbox Code Playgroud)


pro*_*son 9

我怀疑这是因为权限提升是针对open命令的,而不是TextEdit本身.

您应该使用不同的编辑器,可以从命令行中正确运行其二进制文件,如SublimeText(sudo subl /path/to/file)或TextMate(sudo mate /path/to/file).或者只是吮吸它并使用vinano:-)

如果你要做很多这类事情,TextEdit可能是最不受欢迎的编辑器.


Ste*_*ert 6

编辑这些锁定文件的最简单方法是复制它们,编辑副本,然后替换文件:

cp /etc/apache2/extra/httpd-vhosts.conf ~/httpd-vhosts.conf
open ~/httpd-vhosts.conf

sudo mv /etc/apache2/extra/httpd-vhosts.conf /etc/apache2/extra/httpd-vhosts.conf.bkup
sudo mv ~/httpd-vhosts.conf /etc/apache2/extra/httpd-vhosts.conf
Run Code Online (Sandbox Code Playgroud)