如何列出Vim中的所有环境变量? [英] How to list all the environment variables in Vim?

查看:370
本文介绍了如何列出Vim中的所有环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当启动 Vim 时,它会从操作系统中获取许多环境变量(例如 PATH )它也设置它自己的环境变量(如 MYVIMRC )。

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).

如何列表或查看Vim所理解的所有环境变量及其Vim内部的各自的值?

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

推荐答案

在Vim脚本中,没有设计获取当前
定义的环境变量的列表。但是,可以利用Vim
命令行完成功能。

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 $

不难看出,根据Vim脚本语法,所有的
完成都是环境变量的名称。按 wildchar
Tab ,默认情况下)或 Ctrl + D 将显示所有

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.

为了从脚本中获取完成列表,我们需要
克服其交互性。一个可能的伎俩,我们在这里考虑做
,依赖于功能的组合。首先是
Ctrl + A 命令。在命令行模式下,此快捷方式
会触发光标前面的每个可用完成插入。
这些完成按字母顺序列出,并用空格分隔。

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.

如果我们使Vim打印直接插入命令行的完成,
我们可以通过使用:redir
命令重定向命令输出来轻松捕获它们。但是我们需要实现这个副作用就是引用用 Ctrl + A 插入的文本
:引用使我们的
:echo 命令将刚刚打印出来的字符串文字!

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

要以这种方式编辑命令行,用户可以键入:ec 的别名:echo
然后 $ ,按 Ctrl + A ,键入',跳到起始
的命令与 Ctrl + B ,通过按(向右箭头键)将光标移动到
美元符号上两次,删除 $
并插入'。可以使用:normal 命令非交互式复制相同的按键序列

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

这种工作方式,Vim应该使用 + cmdline_compl
功能进行编译。

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

这篇关于如何列出Vim中的所有环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆