39

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

44

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

17

If LANG is an environment variable you can just do:

let language = $LANG

Or, even simpler:

if $LANG == 'en'
…
endif
1
  • 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 that you have read and understand our privacy policy and code of conduct.

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