Skip to main content
21 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 ...
D. Ben Knoble's user avatar
  • 26.6k
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(...
Luc Hermitte's user avatar
  • 17.5k
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 "...
Matt's user avatar
  • 20.8k
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 ...
Maxim Kim's user avatar
  • 13.6k
12 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 ...
nikoss's user avatar
  • 359
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 ...
Ingo Karkat's user avatar
  • 17.9k
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 $...
B Layer's user avatar
  • 20k
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 ...
Ingo Karkat's user avatar
  • 17.9k
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 ...
iznav's user avatar
  • 91
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 ...
jamessan's user avatar
  • 11.1k
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 ...
Luc Hermitte's user avatar
  • 17.5k
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 ...
dedowsdi's user avatar
  • 6,288
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 ...
Rich's user avatar
  • 32.2k
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 ...
B Layer's user avatar
  • 20k
6 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 ...
Ralf's user avatar
  • 9,277
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 .....
Zoe's user avatar
  • 712
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 ...
statox's user avatar
  • 50.1k
6 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:)->...
Matt's user avatar
  • 20.8k
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 ...
filbranden's user avatar
  • 29k
6 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 ...
filbranden's user avatar
  • 29k
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.
Luc Hermitte's user avatar
  • 17.5k
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.
Mahmoud Al-Qudsi's user avatar
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 ...
Christian Brabandt's user avatar
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(&...
Matt's user avatar
  • 20.8k
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 . '/...
filbranden's user avatar
  • 29k
5 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 ...
Vivian De Smedt's user avatar
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()...
Moba's user avatar
  • 390
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 ...
Matt's user avatar
  • 20.8k
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: ...
Peter Rincker's user avatar
4 votes
Accepted

vim cannot access variable in autocmd

Would be nice to see the full error, on which line it complains. Nevertheless you have error in the script, the set command requires no spaces around =, or spaces at all after = sign. You should also ...
grodzik's user avatar
  • 4,658

Only top scored, non community-wiki answers of a minimum length are eligible