0

我的 .vimrc 中有这个,这是在新选项卡中打开主机文件的键映射:

nnoremap <Leader>hs :tabe /etc/hosts<cr>:set noreadonly<cr>

所以现在 Vim 不会因为 readonly 问题困扰我,这很棒。但是,当我保存时,我可能会强制击中⌘</kbd>s or :w. I would like to overwrite both (are they the same?) calls to call our normal NVW (nasty Vim way) to write as sudo:

:w !sudo tee % >/dev/null<cr>

但我只想对这个当前缓冲区执行此操作,就像在 hosts 文件中一样。有setlocal什么魔法?

4

1 回答 1

0

由于您使用的是命令,因此我假设为 macvim。我不相信<D-S>做同样的事情,:w但我不确定这一点。如果需要,您可以映射以使用缓冲区本地映射。

autocmd BufRead /etc/hosts nnoremap <buffer> <D-s> :w !sudo tee % >/dev/null<cr>

但是您首先需要从菜单中取消映射它。在你的 gvimrc 中使用类似的东西。

macmenu File.Save key=<nop>

并添加回保存的默认行为。缓冲区本地映射将优先。

nnoremap <D-s> :w<CR>

您可以:w使用Zyx 的 Answer on Aliasing a command in Vim中的技术覆盖。如果它是唯一在线的东西,它将替换:w为。:w !sudo tee % >/dev/null

autocmd BufRead /etc/hosts cnoreabbrev <buffer> <expr> w ((getcmdtype() is# ':' && getcmdline() is# 'w')?('w !sudo tee % >/dev/null'):('w'))
于 2014-08-02T23:59:34.617 回答