Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
" Get running OS
let os = ""
if has("win32")
  let os="win"
else
  if has("unix")
    let s:uname = system("uname")
    if s:uname == "Darwin"
      let os="mac"
    else
      let os="unix"
    endif
  endif
endif

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

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 ?

share|improve this question
    
Are you using the MacVim? If so, use has(macunix) or has(mac) -- see this question. – user13742 Jun 5 '12 at 14:10
    
I use vim in mac terminal – nXqd Jun 5 '12 at 14:12
up vote 3 down vote accepted

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"
  endif
  if has("unix")
    if system('uname')=~'Darwin'
      return "mac"
    else
      return "linux"
    endif
  endif
endfunction
let os=GetRunningOS()

I hope this helps someone. In case, someone interest in my .vimrc, check my dotFiles

share|improve this answer
    
There is actually a has('mac') value. No need to use uname in this case. – dezza Jul 17 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:
"https://www.novell.com/coolsolutions/feature/11251.htm://www.novell.com/coolsolutions/feature/11251.html 
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"
else
    let g:platform="Unknown"
endif
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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