22
votes
Accepted
How do I use a variable or return value in option, command, or mapping?
The reason your example attempts don't work is that in many locations text is simply seen as a literal string, rather than VimScript. So functions, variables, and the like don't work.
For example, if ...
16
votes
`set` option with variables
You could use :exe, but this is extremely cumbersome to use and you'll need to escape a few things.
I used to use my own path fixing function. It looked like:
exe 'set rtp+='.lh#path#fix(...
13
votes
Accepted
Why is `let foo = $HOME/bar` invalid?
VimScript is more of a programming language. So the following won't work just like in C.
let foo = $HOME/bar
It's division.
let foo = "$HOME/bar"
Inside quoted string only "...
13
votes
Get value of $MYVIMRC from Lua
This is already answered but I just wanted to add another way to access environment variables in lua using vim API.
vim.env.whatever -- will give you the value of whatever
vim.env.test = "test ...
12
votes
Accepted
How to generate a string by concatenating another string with an environment variable
In vim dot is a concatenation operator:
let repo = $Project . '/cfora'
To "refer" variable in the string, well, in your case it is just a concatenation again:
exe '!cmake ' . repo
PS
I would ...
11
votes
Accepted
How can I use the return value of a function in a :set command?
You can use :let with Vim options as well, by prefixing the option name with a & sigil; cp. :help :let-option
let &tags = getcwd().'\tags'
Note that the literal string must be in single ...
11
votes
Accepted
Get value of $MYVIMRC from Lua
$MYVIMRC is an environment variable that Vim adds to the current environment.
Environment variables are usually inherited by child processes from their parent process. For example, you can see $...
10
votes
Accepted
Use variable for mapping inside vimrc
The :map command takes :help key-notation, but not variables. Vimscript is evaluated exactly like the Ex commands typed in the : command-line. There were no variables in ex, so there's no way to ...
9
votes
`set` option with variables
You could use an environment variable defined within your current shell session.
let $myvimdir = '/home/abc/vimfiles'
That way you can use other variables to compose the path as well:
let $myvimdir ...
9
votes
Accepted
Are the internal g:, b:, w:, etc dictionaries documented?
Poking around with :helpgrep \<g:\A, I found this is documented under :help internal-variables:
There are several name spaces for variables. Which one is to be used is
specified by what is ...
9
votes
Accepted
VimL/Vimscript: how to access local variable of outer function from its inner function?
I though it wasn't possible, but I was wrong.
See :h :func-closure
You need to patch two things:
add closure at the end of the inner of the function declaration
respect the usual naming conventions
...
8
votes
Accepted
How can I access buffer variables from BufUnload/BufDelete?
Use :h <afile> to get unloading buffer name, use :h getbufvar() to get buffer local variable.
let b:example_tempfile = tempname()
augroup example
au!
au BufUnload <buffer> echom ...
8
votes
Accepted
Listing global variable
This command will list all global variables:
:let g:
Using :let by itself will list all variables, and passing it just a prefix (such as g: or b: or v:) will filter the output to only include ...
7
votes
Accepted
How to quickly edit vim variable content?
You've picked an unfortunate example, as it's more easily achieved with:
let myvar .= 'bar'
Still, for more complicated edits, you could use the following commands:
Insert the variable into the ...
7
votes
Accepted
How to use variables?
Most is already in the comments, but to summarize:
let indent=4
let &tabstop=indent
let &softtabstop=indent
let &shiftwidth=indent
set expandtab
If you want to set a setting (like ...
7
votes
Accepted
How can I get the list of variables I defined?
If it's a global variable then it resides in g: dictionary, so simply echo g: works.
If g: is too big it's possible to search through it, like any other dictionary, for example:
echo copy(g:)->...
7
votes
Accepted
How to use value under cursor in the shell command?
If you are typing interactively in the vim command line you can hit CtrlrCtrlw to include the current word or CtrlrCtrla to include the current WORD.
If you are writing a function or a command you can ...
6
votes
Accepted
How can I use a string variables for filepath in vimscript map command?
One method is to use a <expr> mapping which evaluates the RHS of the mapping as Vimscript rather than use it literally as for regular mappings. The tricky part of this is the fact that we have ...
6
votes
How to generate a string by concatenating another string with an environment variable
The answer has partly changed as of Vim 8.1 patch 1114, where . is becoming obsolete. While it's not currently wrong to use . in non-Vim9 vimscript (because backwards compatibility), you can use the .....
6
votes
Accepted
How to get the current mode in vim?
You are looking for the function mode(), see :h mode():
mode([expr]) Return a string that indicates the current mode.
If [expr] is supplied and it evaluates to a non-zero Number or
...
6
votes
How can I get the list of variables I defined?
You can use command :let on its own to list the contents of all defined variables.
See :help E121:
:let: List the values of all variables.
If you have an idea of what the beginning of the name of ...
5
votes
Accepted
redirect exmode command to variable
I managed to figure this one out. neovim and modern versions of vim provide an execute function that can be used to do just this.
let list3 = execute(":oldfiles") does the trick.
5
votes
Accepted
shortcut of the current directory path when vsplit/split
You're looking for %:h/
NB: you can tab-complete at any point after the :h.
5
votes
How can I assign the content of a function variable to an option?
:exe and :let (and some other commands like :echo or :call) are basically the only ex commands that expect variables. Almost all other ex commands do not expect such things and therefore take it ...
5
votes
Accepted
Sort output of let or set
You can capture output with execute() function and then pump it through sort(). The whole command could be then:
command! -bang -nargs=1 -complete=command Sorted
\ echo join(sort(split(execute(&...
5
votes
Accepted
Inside a mapping check if a variable is empty in vim
You asked to check the variable in the mapping but I'm going to suggest an alternative approach that will make things a lot easier and give you more flexibility if you want to change anything later.
...
5
votes
Why is `let foo = $HOME/bar` invalid?
You need to use explicit string concatenation here, since :let wants an expression and the /bar looks like a division by a variable named "bar".
What you want here is:
let foo = $HOME . '/...
5
votes
Accepted
What's g:statusline_winid?
The documentation say:
When the option starts with "%!" then it is used as an expression,
evaluated and the result is used as the option value. Example:
:set statusline=%!MyStatusLine()...
5
votes
Accepted
How to unlet a variable in Vim 9 script?
Global variables are still the same as in legacy script, so unlet works as before. The only difference is that the prefix g: now is always required to access global variables.
Unletting non-existent ...
4
votes
Copy the values from a variable?
To put the value of a variable into a buffer use the :put command with the expression register, @=:
put=b:aaa
You can use a range with :put to put it at a specific location. Put below the 9th line:
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
variables × 116vimscript × 36
vimrc × 14
functions × 11
environment-variables × 8
neovim × 7
key-bindings × 6
options × 6
autocmd × 5
statusline × 5
command-line × 4
string-manipulation × 4
regular-expression × 3
path × 3
execute × 3
buffers × 2
substitute × 2
colorscheme × 2
register × 2
ex-mode × 2
cursor × 2
shell × 2
syntax-highlighting × 1
search × 1
gvim × 1