Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the list of all environment variables in Vim?

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, together with their respective values from inside Vim?

like image 216
Ashwin Nanjappa Avatar asked Jun 24 '12 07:06

Ashwin Nanjappa


People also ask

How do I get a list of environment variables?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.

Which command can be used to list all environment variables in Unix?

The most used command to displays the environment variables is printenv .

Which command displays all the environment variables defined in the shell?

The printenv command-line utility displays the values of environment variables in the current shell. We can specify one or more variable names on the command line to print only those specific variables. Or, if we run the command without arguments, it will display all environment variables of the current shell.


2 Answers

In Vimscript, there is not a direct way of getting the list of currently defined environment variables. However, it is possible to exploit Vim command-line completion feature to make one.

Consider possible completions for the following unfinished command:

:echo $

It is not difficult to see that, according to Vimscript syntax, the completions must be the names of the environment variables. Pressing the 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 I propose herein relies on a combination of features. The 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. The inserted completions are listed in alphabetical order and separated with spaces.

If we could make Vim print those completions out right into the command line, we would 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 can be just printed out!

:echo 'NAME1 NAME2 NAME3'
NAME1 NAME2 NAME3

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

Putting all these pieces 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 must be compiled with the +cmdline_compl feature.

like image 184
ib. Avatar answered Oct 08 '22 03:10

ib.


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.

like image 22
Ingo Karkat Avatar answered Oct 08 '22 02:10

Ingo Karkat