How to list all the environment variables in Vim?

强颜欢笑 提交于 2019-12-31 13:07:49

问题


When Vim is started, it grabs many of the environment variables from the operating system (like PATH) and it also sets it own environment variables (like MYVIMRC).

How do I list or view all the environment variables that Vim understands and their respective values from inside Vim?


回答1:


In Vim script, there is no designed way of getting the list of currently defined environment variables. However, it is possible to exploit Vim command-line completion feature.

Consider possible completions for the following unfinished command.

:echo $

It is not difficult to see that, according to Vim script syntax, all of the completions are names of the environment variables. Pressing wildchar key (Tab, by default) or Ctrl+D will display all of them.

In order to get this list of completions from within a script, we need to overcome its interactive nature. A possible trick that we consider here to do that, relies on combination of features. First of them is the Ctrl+A command. In Command-line mode this shortcut triggers insertion of every available completion in front of the cursor. These completions are listed in alphabetical order and separated with spaces.

If we make Vim print those completions inserted right into the command line, we could easily capture them by redirecting command output with the :redir command. But all we need to achieve that side-effect is to quote the text inserted with Ctrl+A: Quoting makes the rest of our :echo command a string literal that will be just printed out!

:echo 'NAME1 NAME2 NAME3'
NAME1 NAME2 NAME3

To edit a command line this way, a user can type :ec (an alias for :echo) then $, press Ctrl+A, type ', jump to the beginning of the command with Ctrl+B, move the cursor over the dollar sign by pressing (right arrow key) twice, delete that $ and insert ' instead. The same sequence of key presses can be reproduced non-interactively using the :normal command.

Putting all these pieces of puzzle together, we obtain the following function.

function! Env()
    redir => s
    sil! exe "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
    redir END
    return split(s)
endfunction

For this approach to work, Vim should be compiled with the +cmdline_compl feature.




回答2:


To list all environment variables, use

:echo $<C-D>

Then, you can start typing the name of the variable of interest, auto-complete with Tab, finally press Enter to show its value.

You didn't tell whether you need this interactively or in a script. For the latter, it's unfortunately not possible to capture the output via :redir.




回答3:


Looking at the other anwers, you can do it without a script.

Enter insert mode, and then press

<C-R>=

Where Ctrl+R allows you to insert any of the registers available (see :help i_Ctrl-R). But particularly you'll use the expression register = here to evaluate a string (see :help @=).

So, now you can insert the expression introduced by ib.'s answer. Enter this:

$<C-A>"<HOME>"<CR><ESC>

Now, you should be back to normal mode and the expression should have evaluated to a string and been placed in the buffer. You can further clean it up with a substitute command:

:s/\s/\r$/g

That should place every environment variable on a different line.




回答4:


Based on the idea by ib and the comments from ZyX, here's the function that I ended up using:

function! GetEnvVars()
    silent execute "normal! :return $\<C-a>')\<C-b>\<C-right>\<Right>\<Del>split('\<CR>"
endfunction


来源:https://stackoverflow.com/questions/11175842/how-to-list-all-the-environment-variables-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!