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

查看:72
本文介绍了如何获取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, together with their respective values from inside Vim?

推荐答案

在Vimscript中,没有直接的方法来获取列表当前定义的环境变量.但是,有可能利用Vim命令行完成功能来制作一个.

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 $

不难看出,根据Vimscript语法,完成必须是环境变量的名称.按 wildchar 键(默认为 Tab )或 Ctrl + D 将显示所有这些内容.

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.

为了从脚本中获取此完成列表,我们需要克服其互动性.我建议的一个可能的把戏本文中依靠特征的组合.他们中的第一个是 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 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.

如果我们可以让Vim将这些完成内容直接打印到命令行,我们可以通过重定向命令轻松捕获它们使用:redir 命令输出.但是我们需要实现这一目标副作用是引用用 Ctrl + A 插入的文本:引用使其余的:echo 命令成为字符串文字,可以打印出来!

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

要以这种方式编辑命令行,用户可以键入:ec (:echo 的别名,然后是 $ ,按 Ctrl + A ,键入',跳至通过按 Ctrl + B 行,将光标移到通过按(向右箭头键)两次,选择美元符号,删除该 $ ,最后插入'.相同的顺序可以轻松地以非交互方式复制按键的数量:normal 命令.

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

要使此方法起作用,必须使用Vim编译Vim. + cmdline_compl 功能.

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

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

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