Questions tagged [variables]
For questions about vimscript variables, their scopes, and usages.
107
questions
0
votes
1
answer
18
views
How to use variable in syntax match
I have made (better said got) a syntax file for CSV files. This works fine so far.
But there is the semicolon used as seperator. I want to be able to change the seperator e.g. by a function / mapping /...
0
votes
2
answers
35
views
VimScript: Substitute string variable content for string variable name
I would like to the present working directory (:pwd) be changed across all tabs and subwindows at the same time. I tried:
let PreferredPWD=expand('%:p:h') " The desired working directory
" ...
0
votes
1
answer
47
views
Can a popup_menu's callback access its popup_menu's first argument?
For now I have the feeling the answer is no, but I'm asking to make sure that's the case, and to know if people has found workarounds or can suggest good approaches.
From :help popup_menu I've found ...
0
votes
1
answer
28
views
How do I use variables with the delete command? (i.e., how to expand/reference variables for command arguments?)
I'm using variables for line numbers, and I want to delete a range of lines using these variables. How do I do that?
let x = 5
let y = 20
" I want to delete the range of lines from x ...
2
votes
0
answers
39
views
How do I concatenate these strings? [closed]
I have two string variables:
let url = "https://URL/path/to/repo"
let branch = "GIT-HASH"
I want to concatentate them and echo it on the shell:
let result = url . branch
exec '!...
0
votes
1
answer
26
views
How to define default values for variables defined in ftplugin?
Say that I have defined two files
python.vim
let b:foo="py"
let b:bar="thon"
cobol.vim
let b:foo="co"
let b:bar="bol"
and that I placed both of them in ~/.vim/...
0
votes
1
answer
32
views
How to prevent an extra newline from being appended to macros ending with a newline? [duplicate]
If I save a macro by typing:
:let @s='/{<C-v><C-m>'<Enter>
which looks like:
:let @s='/{^M'
the macro that is actually saved is /{^M^M,
with an extra linebreak (that I did not type)...
0
votes
1
answer
29
views
Put word count of lines into variable
I can get a word count of a range of lines, by doing something like this:
:1,20w !wc -w
(where that gets lines 1 to 20, writes (w) them to the external shell command (!) wc -w, and outputs the result ...
1
vote
1
answer
81
views
How to set g:python_indent.disable_parentheses_indenting?
:help ft-python-indent lists the following command:
let g:python_indent.disable_parentheses_indenting = 1
but when I try to use that in my config I am getting the error message
E121: Undefined ...
0
votes
1
answer
43
views
What asterisk means before variable name in exists() function?
"filetype.vim" has line:
if exists("*fnameescape")
What '*' means here? Is it suppose to dereference any value?
2
votes
1
answer
128
views
How can I store the number of regex matches in a variable?
In Vim Script, I want to check for the amount of matches from a regular expression (/\%^\n*) and store the amount of matches in a variable. I this possible?
0
votes
1
answer
53
views
Vimscript: how to overwrite a variable that is inside an if block?
I have some code in a function like so:
let branch = substitute(system('git rev-list -1 HEAD'), '\n', '', 'g')
let num_branches = system('echo `git branch --contains '.branch.'` | wc -l')
if ...
1
vote
1
answer
270
views
How to unlet a variable in Vim 9 script?
In Vim9 script :let cannot be used. An existing variable is assigned to
without any command. The same for global, window, tab, buffer and Vim
variables, because they are not really declared. Those ...
3
votes
1
answer
109
views
What's g:statusline_winid?
When I do :h 'statusline' I can see
The g:statusline_winid variable will be set to the window-ID of the
window that the status line belongs to.
But :set statusline=[window_id\:%{g:statusline_winid}] ...
1
vote
2
answers
229
views
How to use value under cursor in the shell command?
Is it possible to use word under cursor in shell command? Say:
Elvis
^ cursor here
then
:r! echo <word_under_cursor>
and get "Elvis" read in.
0
votes
0
answers
13
views
autocommand variable is not substituded [duplicate]
let Dropbox = "/usr/Dropbox"
...
autocmd BufNewFile *.php 0r Dropbox/vim/templates/php_start.txt
The variable 'Dropbox' is not substituded by its value
2
votes
2
answers
1k
views
Listing global variable [duplicate]
Is there any way to list all global variable?
I'm guessing i could maintain a list where multiple value represent a global variable but that feel a bit weird. is there an official way to do that? (...
1
vote
1
answer
130
views
How can I use a variable line number with a command?
I want to move a line to another specified in a variable.
So where I might do this to move the current line up seven lines:
:m-7
I want to do it with a variable line destination. Something like this:
...
0
votes
1
answer
323
views
How to `source` a path stored in a variable in Vimscript
tldr: The title pretty much summarizes the entirety of the question
I am trying to make my configuration more adaptable to different machines so that I can use git to keep them in sync between my ...
0
votes
1
answer
331
views
Global variable not setting correctly when starting neovim
I'm trying to setup separate configs for my WSL, Windows, and MSYS2 environments in the same init.vim with simple if-else statements. So, I'll need to know what environment I'm in. To know that I ...
4
votes
2
answers
3k
views
Get value of $MYVIMRC from Lua
I am trying to retrieve the value of $MYVIMRC in my init.lua but I am coming up short because I don't understand what $MYVIMRC actually is.
It is not a global variable, is it?
None of these work:
vim....
7
votes
2
answers
1k
views
Why is `let foo = $HOME/bar` invalid?
Why is let foo = $HOME valid and let foo = $HOME/bar invalid in vimscript?
I have tried let foo = "$HOME/bar" but it is the equivalent of foo = '$HOME/bar' in shell script, $HOME is not ...
0
votes
0
answers
56
views
Save and restore a variable across launches
I wanna save the state of a variable (say, background) such that the state is restored next time I start vim.
More in detail, it should work like this:
I set background to either dark or light.
I ...
3
votes
2
answers
872
views
How can I get the list of variables I defined?
For convenience, I'd like to define some variables. But I worry that I may forget their names. How can I get a list of them?
0
votes
1
answer
42
views
How to assign fileencoding's value to a variable?
In ex mode,i get the value for set fileencoding.
set fileencoding?
fileencoding=utf-8
How can assign str with the value utf-8 and print it?
let str=`set fileencoding?` #it is a wrong statement,i ...
0
votes
2
answers
714
views
Can I use a variable in autocmd pat?
I have 3 languages and any can be a source or target so I need some flexibility.
I am successfully loading a language translation specific setup with this:
let g:tr_source = 'dyu'
let g:tr_target =...
1
vote
2
answers
434
views
Insert output of function with variable into buffer
I'm trying to make a vim function that takes a variable and uses that as part of a shell command and buts it into the current buffer.
I tried using read but that just interprets the var as a string.
...
1
vote
1
answer
2k
views
Inside a mapping check if a variable is empty in vim
So i'm new to vim, and specially new to vimscript, and messing around i made the following code to get the name for a file and then create said file in a new buffer. I want a way to check if FileName_ ...
2
votes
1
answer
307
views
What is {what} supposed to mean in the official documentation?
I keep seeing mention of {what} here and there, mainly in eval.txt, popup.txt, terminal.txt, syntax.txt, and channel.txt.
From :h popup_create()
popup_create({what}, {options})
Open a popup window ...
1
vote
2
answers
645
views
Global options as function local variables
I'm asking about this code snippet:
:function TextwidthIsTooWide()
: if &l:textwidth ># 80
: return 1
: endif
:endfunction
This is what vim-help says on the l (local) prefix:
Inside ...
1
vote
1
answer
83
views
Use variable in vim mapping async task [duplicate]
I am trying to use a variable in this mapping:
let board_ip = "192.168.100.18" │
nnoremap <F6> :AsyncRun echo . ...
0
votes
1
answer
40
views
add python option in vimrc
For my fortran ft I use
au BufWritePre <buffer> :silent %!fprettify --silent
in my ftplugin.
The fprettify is a python script with many command line options, using argparse.
Now, my question is,...
0
votes
2
answers
115
views
How to store state (e.g. variables) between user actions?
The main question is in the title, that is, I have a function, which is invoked by the user and it should 'remember' necessary stuff between invocations, so the next time the user calls it (like, ...
2
votes
2
answers
518
views
How to provide shared variables for plugins?
I've written some Vimscript code* in which it's convenient to have a set of shared color names. To use the names, so far I've had to rely on code like this:
execute 'source ' . expand('<sfile>:...
2
votes
1
answer
264
views
How to store a region of text into a variable?
Suppose I have \label{some text} somewhere in my file and I want to store some text into a variable.
Right now what I am doing to achieve this is to first visually select some text and then store it ...
1
vote
2
answers
125
views
Is there a 'let' equivalent for setting an option only local to a buffer/window?
When working with options that often have complex or calculated string values (e.g. formatprg, which might have additional parameters, separated by whitespace), it's often easier to use let to set ...
1
vote
1
answer
103
views
Possible to set vimrc variables based on the current vim 'incantation'?
I wasn't sure how to word this properly in the title..
what I'm looking to do is set variables one way or another depending on how vim is invoked.
Specifically I have just discovered the Man plugin, ...
2
votes
1
answer
214
views
How do I put the List returned from a 'heredoc' (:h Let-HereDocs) into a Dictionary in Vim? How do I also make this work in Neovim?
In :help let-heredoc it explains that one can set a variable to a 'heredoc' where each line of the heredoc becomes an entry in a list as follows:
let text =<< trim END
if ok
echo 'done'
...
3
votes
1
answer
67
views
Customizing Latex-Suite star environments
The documentation of Latex-Suite says I can customize the generated environment boilerplates by defining g:Tex_Env_... variables. This works for me. Nevertheless, it goes on to say that for ...
1
vote
2
answers
183
views
switch current directory between a base directory and current file directory
Goal:
To achieve filename/path completion relative to current file directory using file name completion CTRL-X CTRL-F.
I open all my files contained in a base directory such that :pwd in some_file ...
4
votes
1
answer
177
views
How can I access buffer variables from BufUnload/BufDelete?
vimdoc says:
BufUnload
Before unloading a buffer.
...
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer being unloaded "<afile>".
Don't change ...
3
votes
2
answers
323
views
Sort output of let or set
How to sort the output of let or get? E.g., I want to sort all global variables containing "python":
:filter python let g:
0
votes
1
answer
86
views
function variable scoping and arg types problems
I'm trying to make a small extension/bug fix to a plugin I rely on (referenced as Plugin in the code snips), therefore I'm constrained by its existing API.
I've got the functionality I want somewhat ...
5
votes
1
answer
1k
views
VimL/Vimscript: how to access local variable of outer function from its inner function?
I posted this question on stackoverflow already, but realized afterwards that it's probably better to ask here.
So I have a function of the following form:
function! s:my_function(dict_arg)
let ...
12
votes
1
answer
2k
views
How do I use a variable or return value in option, command, or mapping?
I have a variable foo with let foo='value', and a function F like
function F()
return 'value'
endfunction
and I want to use them
in an option: :set option=foo, :set option=F(); or
in a command: :...
4
votes
2
answers
2k
views
Get file name and directory in .vimrc file
I have mapped my F5 key in vimrc file to run the code using the following command:
map <F5> :!sh -xc 'cd "/home/hashir/vscode/" && g++ test.cpp && ./a.out
It is working well. ...
1
vote
2
answers
694
views
How to delete a line from a variable?
Inside a function, saved multiple lines of external command output to a variable.
How to delete a line from the variable? Like first line.
I can run let tmp = split(variable, "\n") to assign the ...
1
vote
2
answers
118
views
How to get dynamic hex format variable width with leading zeros?
strlen(var) can return length without leading zeros.
How to include leading zeros?
Example let var=0x00001234, width shall be 8 instead of 4.
3
votes
2
answers
1k
views
Gvim - Change window position horizontally
I want to create a command that will place my gvim window to a different "x" location on my screen while maintaining the "y" position.
I do this in my _gvimrc by calling
winpos 319 16
I want to ...
0
votes
1
answer
195
views
How to use variables in a user defined command? [duplicate]
I have this user defined command:
command! PS silent! w | PlugSnapshot! ~/.vim/plugins/plugins.lock
How could I change it to take the filepath from a variable, something like this:
let ...