I have defined the following function

let g:conda_activate = 'myenv'

" Get conda virtual environment
function Condaenv(env)
    return system("conda activate ". a:env. " && call echo %CONDA_DEFAULT_ENV%")

augroup Condaenvget
    autocmd VimEnter * let g:conda_env = Condaenv(g:conda_activate)
augroup END

and then I place g:conda_env in one section of vim-airline as it follows

au User AirlineAfterInit  :let g:airline_section_b = airline#section#create(['(%{g:conda_env})'])

However, what I see in the statusline is the following

enter image description here

If I :echo Condaenv(g:conda_activate) or just :echo g:conda_activate I don't see such a ^@ . Am I doing something wrong or is it a potential bug of vim-airline? If so, I'll consider to open an issue on their issue tracker.

  • 2
    Try trim()ing the result of system(); I suspect a newline getting translated to a null
    – D. Ben Knoble
    Oct 9, 2022 at 21:57
  • 1
    And for Windows, incorporate trim() with system() into a custom System() function. After switching to Windows, I kept having to trim here and trim there. What a mess.
    – 3N4N
    Oct 9, 2022 at 22:28
  • 2
    Instead of using system(), I would make use of systemlist() which should automatically trim the trailing carriage return + line feed. BTW: Are you sure you want to call conda activate really all the time, e.g. for any key you press? That seems excessive and may slow down vim Oct 10, 2022 at 6:15
  • The trim solution works. The systemlist solution returns a list so I have to access it to extract a string. @ChristianBrabandt not really, I don't want to call conda activate for any key press but I want to call it only at Vim startup. Or, even better, I want to call it every time a virtual environment has changed, but I don't think Vim has defined such an event :D So, I am fine to call it only at Vim startup - isn't VimEnter the Vim startup event?!?
    – Barzi2001
    Oct 10, 2022 at 6:57
  • Why would you use join if you are only interested in the last line? Just use systemlist()[0] or systemlist()[-1] Oct 10, 2022 at 11:48


Your Answer

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

Browse other questions tagged or ask your own question.