40

In my .vimrc file, I would like to store in a variable the result of an external command, in my case:

$ echo $LANG

So that my .vimrc file would have :

let language = output(!echo $LANG)
if language == 'en'
   nnoremap <somekey> <ohanotherkey!>
end if

So far I couldn't find how to do it. I've found that in command mode, :read !echo $LANG would insert in my current file the content I am looking for. But I don't know how to write it down in a .vimrc file.

guntbert
  • 1,245
  • 1
  • 13
  • 27
Feffe
  • 1,761
  • 2
  • 14
  • 21

2 Answers2

45

You can do this with the system function:

let language = system('echo $LANG')

Bonus point: if your output is a list, you can use the systemlist instead to get back a list. e.g.

let files = systemlist('ls')
" ['bin', 'dev', ... ]

ref: :h system

muru
  • 24,838
  • 8
  • 82
  • 143
nobe4
  • 16,033
  • 4
  • 48
  • 81
17

If LANG is an environment variable you can just do:

let language = $LANG

Or, even simpler:

if $LANG == 'en'
…
endif
adelarsq
  • 604
  • 3
  • 13
muru
  • 24,838
  • 8
  • 82
  • 143