how do I detect OS X in my .vimrc file, so certain configurations will only apply to OS X?

前端 未结 7 650
庸人自扰
庸人自扰 2020-12-12 15:18

I use my .vimrc file on my laptop (OS X) and several servers (Solaris & Linux), and could hypothetically someday use it on a Windows box. I know how to detect unix gene

相关标签:
7条回答
  • 2020-12-12 15:30

    homebrew vim and MacVim returns true for has('mac'), however so does has('unix'). so to have it work across all unix platforms, a possible solution is:

    if has('unix')
      if has('mac')       " osx
        set guifont=...
      else                " linux, bsd, etc
        set guifont=...
      endif
    elseif has('win32') || has('win64')
      set guifont=...
    endif
    

    on the other hand, as of el capitan, the system vim returns false for has('mac'), and uname snooping is probably the way to go. it's an ancient version, never used it.

    0 讨论(0)
  • 2020-12-12 15:30

    gui_macvim gui_gtk2 gui_gtk gui_win32

    There is a OS detection script somewhere on stackoverflow - more keywords to find it: win64 win95 macunix...

    0 讨论(0)
  • 2020-12-12 15:31

    I'm doing the same thing you are. Don't try to detect the OS. Instead, try to detect the type of vi/vim.

    Check :h feature-list for a full list of the conditionals you can use.

    Here's what I use to detect MacVim in my vimrc:

    if has("gui_macvim")
      set guifont=Monaco:h13
    endif
    

    With this, you can detect for gvim, vi, vim, and whatever other flavors you might use. The nice thing is that you could have vim-compatible settings on OS X.

    Reference from Vim Mailing list

    EDIT: This approach and its variants (has('mac'), has('macunix'), has('gui_mac'))do not work for vim in OS X. If you use only use MacVim, you're safe. If you're weird like me and like to occasionally jump into vim, one of the other solutions may be more suitable.

    0 讨论(0)
  • 2020-12-12 15:32

    You want macunix. To quote :h feature-list:

    mac     Macintosh version of Vim.
    macunix Macintosh version of Vim, using Unix files (OS-X).
    

    mac, AFAIK, only applies to old-school Macs, where \r is the line separator.

    0 讨论(0)
  • 2020-12-12 15:35

    This is the easiest way I have found.

    if system('uname -s') == "Darwin\n"
      "OSX
      set clipboard=unnamed 
    else
      "Linux
      set clipboard=unnamedplus
    endif
    
    0 讨论(0)
  • 2020-12-12 15:42

    My updated .vimrc now uses the following:

    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
    

    My original answer is below


    You could try what I do in my .vimrc:

    if has("unix")
      let s:uname = system("uname -s")
      if s:uname == "Darwin"
        " Do Mac stuff here
      endif
    endif
    

    Although, to be completely transparent, my actual .vimrc reads:

    let s:uname = system("echo -n \"$(uname)\"")
    if !v:shell_error && s:uname == "Linux"
    

    Mainly for detecting Linux (as opposed to OSX)

    I'm not sure if you absolutely have to do that echo -n \"$(uname)\" stuff, but it had to do with the newline at the end of the uname call. Your mileage may vary.

    0 讨论(0)
提交回复
热议问题