20

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.

19

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

12

If LANG is an environment variable 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 agree to our terms of service, privacy policy and cookie policy

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