19
votes
Accepted
Talking to vim from outside
If you launch your first Vim session with the argument --servername VIM:
$ vim --servername VIM
then you can send any command from any other shell with the argument --remote-send.
For example, if ...
17
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 ...
14
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(...
12
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 "...
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 ...
10
votes
Can I do array destructuring in VimL?
Yes you can, but Vim calls it list unpacking. It can be found in Vim's help:
:h let-unpack
Vim allows you to create and populate two variables from a list:
let [var1, var2] = mylist
which is ...
9
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
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 use consecutive numbers in an Ex-style substitute command?
This command does what you want:
:let i = 1|g/^Do/s/^/\=i/|let i = i + 1
Explanation…
let i = 1 initializes counter i,
g/^Do/s/^/\=i/ prepends i to each line starting with Do,
let i = i + 1 ...
8
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 ...
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 ...
7
votes
Accepted
Append to global variable and completion
You could have your variable be a list and add values with :let g:var += ['item']. However, it won't be possible to prevent the same item to be added several times.
That's why I've come up with lh#...
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 ...
6
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 ...
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
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:)->...
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
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
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 ...
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
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 ...
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:
...
4
votes
Accepted
How to `unlet` a variable using from a `argdo` statement in VIM
When you use a variable without a scope (i instead of b:i or w:i, etc.) outside of a function, you're using a global variable. From :h internal-variables:
There are several name spaces for variables. ...
4
votes
Accepted
As an argument of a function, when does a variable name need to be quoted?
It has to do with the nature of exists():
Note that the argument must be a string, not the name of the
variable itself. For example:
exists(bufcount)
This doesn't check for existence of ...
4
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.
4
votes
Accepted
What is the difference between let mapleader =”,”, and let g:mapleader=”,”
In the .vimrc, there is no difference between them.
Without an explicit scope, vim chooses between g: and l: depending on the context. l: is implicit within functions, g: is implicit everywhere else....
4
votes
Accepted
How do you use variables in a search?
You can include variables in mappings by building the map command as a string and executing it with the :execute command:
let s:twoLinesStartingWithWords = '^\w\+.*\n\w\+.*'
execute 'nnoremap <...
4
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 ...
4
votes
How to delete a line from a variable?
It depends on how you get the result of your external command but you might be interested in :h systemlist().
It executes the command given as parameter and returns a list containing every lines of ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
variables × 95vimscript × 26
functions × 10
vimrc × 9
environment-variables × 7
neovim × 6
key-bindings × 5
autocmd × 5
statusline × 4
options × 3
path × 3
string-manipulation × 3
buffers × 2
substitute × 2
colorscheme × 2
register × 2
ex-mode × 2
cursor × 2
set × 2
regular-expression × 1
search × 1
command-line × 1
gvim × 1
terminal × 1
normal-mode × 1