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.

up vote 16 down vote accepted

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 veriable, you don't need to do this. You can just do:

let language = $LANG

Or, even simpler:

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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