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.

2 Answers 2


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


If LANG is an environment variable you can just do:

let language = $LANG

Or, even simpler:

if $LANG == 'en'
  • This does not exactly answer my too general question... but is even better ! Thanks.
    – Feffe
    Apr 19, 2016 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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