[英]For the Vim editor, how do I map a function key in .vimrc on Mac OS X?
[英]how do I detect OS X in my .vimrc file, so certain configurations will only apply to OS X?
我在我的笔记本电脑(OS X)和几个服务器(Solaris和Linux)上使用我的.vimrc文件,并且假设有一天可以在Windows机器上使用它。 我知道如何检测unix和windows,但我如何检测OS X? (就此而言,有没有办法区分Linux和Solaris等等。是否有一个列表可以采用所有字符串?我的Google-fu什么都没发现。)
例如,我会使用这样的东西:
if has("mac")
" open a file in TextMate from vi: "
nmap mate :w<CR>:!mate %<CR>
elseif has("unix")
" do stuff under linux and "
elseif has("win32")
" do stuff under windows "
endif
但显然“mac”不是正确的字符串,也不是我试过的其他字符串。
更新:下面的答案(“macunix”)似乎相当清楚它应该工作,但由于某种原因它不会。 (也许Apple没有正确编译vim来回应这个?似乎不太可能。)
无论如何,我想我需要转移问题的焦点:有没有人有一个能达到同样目的的解决方案? (即,成功检测到.vimrc文件正在Mac OS X上使用。)
我更新的.vimrc
现在使用以下内容:
if has("gui_running")
" Gvim
if has("gui_gtk2") || has("gui_gtk3")
" Linux GUI
elseif has("gui_win32")
" Win32/64 GVim
elseif has("gui_macvim")
" MacVim
else
echo "Unknown GUI system!!!!"
endif
else
" Terminal vim
endif
我原来的答案如下
你可以尝试我在.vimrc中做的事情:
if has("unix")
let s:uname = system("uname -s")
if s:uname == "Darwin"
" Do Mac stuff here
endif
endif
虽然,为了完全透明,我的实际.vimrc读取:
let s:uname = system("echo -n \"$(uname)\"")
if !v:shell_error && s:uname == "Linux"
主要用于检测Linux(而不是OSX)
我不确定你是否绝对必须做那个echo -n \\"$(uname)\\"
东西,但它与uname
调用结束时的换行符有关。 你的旅费可能会改变。
我只能通过添加两个字符来编辑以前的答案:
这是正确的(传递我的macos 10.6和默认的vim控制台版本)
if has("unix")
let s:uname = system("uname")
if s:uname == "Darwin\n"
" Do Mac stuff here
endif
endif
system(“uname”)将出现一个返回字符,如果条件失败则返回第二个字符。 只是一个小修补程序添加“\\ n”。
我做的和你一样。 不要试图检测操作系统。 相反,尝试检测vi / vim的类型。
检查:h feature-list
,查看可以使用的条件的完整列表。
这是我用来检测我的vimrc中的MacVim的内容:
if has("gui_macvim")
set guifont=Monaco:h13
endif
有了这个,您可以检测gvim,vi,vim以及您可能使用的其他任何风格。 好的是你可以在OS X上拥有vim兼容的设置。
编辑:这种方法及其变体( has('mac')
, has('macunix')
, has('gui_mac')
)不适用于OS X中的vim。如果你只使用MacVim,那么你就是安全的。 如果你像我一样奇怪并喜欢偶尔跳进vim,其他一个解决方案可能更合适。
homebrew vim
和MacVim
对于has('mac')
返回true,但是has('unix')
。 为了让它在所有unix平台上运行,可能的解决方案是:
if has('unix')
if has('mac') " osx
set guifont=...
else " linux, bsd, etc
set guifont=...
endif
elseif has('win32') || has('win64')
set guifont=...
endif
另一方面,就像el capitan一样,系统vim
为has('mac')
返回false,而uname
snooping可能是要走的路。 这是一个古老的版本,从未使用它。
你想要macunix
。 引用:h feature-list
:
mac Macintosh version of Vim.
macunix Macintosh version of Vim, using Unix files (OS-X).
mac,AFAIK,仅适用于老式Mac,其中\\ r是行分隔符。
这是我找到的最简单的方法。
if system('uname -s') == "Darwin\n"
"OSX
set clipboard=unnamed
else
"Linux
set clipboard=unnamedplus
endif
gui_macvim gui_gtk2 gui_gtk gui_win32
stackoverflow上有一个操作系统检测脚本 - 找到它的更多关键字:win64 win95 macunix ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.