为何Mac下view命令(软链至vim)会自动以只读模式打开文件?
Why does
view (a symlink to Vim) open files in read-only mode automatically? Great question! This is actually a built-in feature of Vim—it checks the name it was called by, then switches to the corresponding mode automatically.
Here's the breakdown:
- When you run
/usr/bin/view /path/to/file, Vim looks at the 0th command-line argument (which is the name the program was invoked as, in this caseview). If it detects this name matchesview, it automatically enables thereadonlymode—this is exactly equivalent to runningvim -R /path/to/fileor manually typing:set readonlyafter opening Vim. - You can test this logic easily: create your own symlink to Vim (e.g.,
ln -s /usr/bin/vim ~/myview), then use~/myview test.txtto open a file. You'll see it enters read-only mode just like the system'sviewcommand. Conversely, if you force Vim to be called asview(usingexec -a view vim test.txt), it will also trigger the read-only behavior. - This pattern is common in Vim: other symlinks like
vimdiff(for diff mode) orex(for line-editor mode) work the same way—they're all pointing to the same Vim binary, but Vim adjusts its behavior based on the invocation name.
When you try to edit a file opened via view, you'll get a warning like W10: Warning: Changing a readonly file—this is Vim enforcing the read-only state that was automatically enabled based on the call name.
内容的提问来源于stack exchange,提问作者iampkuhz
相关产品推荐
相关产品推荐

