" Get running OS
let os = ""
if has("win32")
  let os="win"
  if has("unix")
    let s:uname = system("uname")
    if s:uname == "Darwin"
      let os="mac"
      let os="unix"

" Brackets expanding
let preBracket="`"
if os=="mac"
  let preBracket="$"

exe 'vnoremap' . preBracket . '1 <esc>`>a)<esc>`<i(<esc>'
exe 'vnoremap' . preBracket . '2 <esc>`>a]<esc>`<i[<esc>'
exe 'vnoremap' . preBracket . '3 <esc>`>a}<esc>`<i{<esc>'
exe 'vnoremap' . preBracket . '$ <esc>`>a"<esc>`<i"<esc>'
exe 'vnoremap' . preBracket . 'q <esc>`>a''<esc>`<i''<esc>'
exe 'vnoremap' . preBracket . 'e <esc>`>a"<esc>`<i"<esc>'
" Map auto complete of (, ", ', [
exe 'inoremap ' . preBracket . '1 ()<esc>i'
exe 'inoremap ' . preBracket . '2 []<esc>i'
exe 'inoremap ' . preBracket . '3 {}<esc>i'
exe 'inoremap ' . preBracket . '4 {<esc>o}<esc>O'
exe 'inoremap ' . preBracket . 'q ''''<esc>i'
exe 'inoremap ' . preBracket . 'e ""<esc>i'

As vimscript above I want to map the shortcut to key expand bracket depend on vim's platform. Actually my macbook use different layout but I love the ease of using ``` character in keyboard so I map to $ which stays at the same place on mac pro keyboard (da layout).

But it seems that the script works fine on linux but doesn't work on mac. Did I miss something or I need to add something to make this script work on Mac OSX ?

  • Are you using the MacVim? If so, use has(macunix) or has(mac) -- see this question.
    – user13742
    Jun 5, 2012 at 14:10
  • I use vim in mac terminal Jun 5, 2012 at 14:12

2 Answers 2


i manage to answer my own question, for comparing string in vimscript we should use =~ instead of == . The reverse one is !~

So edit this line if s:uname == "Darwin" to if s:uname =~ "Darwin" to make this little script work.

I rewrite this as function:

function! GetRunningOS()
  if has("win32")
    return "win"
  if has("unix")
    if system('uname')=~'Darwin'
      return "mac"
      return "linux"
let os=GetRunningOS()

In case, someone interest in my .vimrc, check my dotFiles

  • There is actually a has('mac') value. No need to use uname in this case.
    – dza
    Jul 17, 2016 at 11:32

Linux, OS X, FreeBSD, UNIX and a distro-specific (Debian) detection.

" OS Platform Detection
let g:platform = 0
let g:distro = 0
" more distros to detect:
let g:uname = substitute(system("uname"), '\n\+$', '', '')
if filereadable("/etc/debian_version")
    let g:platform="Linux"
    let g:distro="Debian"
elseif has('mac')
    let g:platform="Darwin"
    let g:distro="OS X"
elseif g:uname == "FreeBSD"
    let g:platform="FreeBSD"
elseif has('unix')
    let g:platform="UNIX"
elseif has('win32') || has('win64') "win32 also matches on win64 so redundant
    let g:platform="Windows"
elseif has('win32unix')
    let g:platform="Cygwin"
    let g:platform="Unknown"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.