Questions tagged [variables]
For questions about vimscript variables, their scopes, and usages.
113
questions
1
vote
1
answer
63
views
How can I echo a variable and redirect it to a file, register or the clipboard?
I'm using a colour scheme which makes some text unreadable in the vim terminal. Therefore I want to alter g:terminal_ansi_colors.
:echo g:terminal_ansi_colors
This shows me the value of the variable, ...
0
votes
1
answer
49
views
Calling variables from SHELL and using them in Vim config (vimrc) [closed]
Question
I am trying to use variables from SHELL inside my Vim configuration file, now, I have researched how to do this and asked ChatGPT about how as well. It is possibly due to the specific ...
1
vote
1
answer
42
views
How can I match a regexp containing newlines?
When developing a vim plugin I'll publish soon, I want to highlight a text contained in a variable g:last_completion_text.
Here below the code to
define an highlight group named PrompterCompletion
...
0
votes
0
answers
23
views
how to reference variables in config [duplicate]
Let's take undodir as an example; we might have something like this in our vimrc:
set undodir=$HOME/.config/nvim/undo/
In order to make config platform-agnostic one might want to use variables ...
0
votes
2
answers
79
views
Set command-line local variable
How can I set a variable that only the current command line sees?
So that
:let s:one = 1 | echo s:one prints 1
but running two commands
:let s:one = 1
:echo s:one
errors E121: Undefined variable: s:...
1
vote
1
answer
206
views
Setting custom global variables from command-line with `nvim` command
Occasionally I would like to override my standard nvim configuration, for example, to tell it not to load vimtex's folding configuration for a very large or complicated tex file for which I don't need ...
0
votes
1
answer
83
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
157
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
63
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
54
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
57
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
34
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
38
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
68
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 ...
3
votes
1
answer
158
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
74
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
150
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
82
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
411
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
207
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
493
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
3
votes
2
answers
2k
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
317
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:
...
1
vote
2
answers
575
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
469
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 ...
7
votes
2
answers
5k
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
110
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
1k
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
932
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 =...
2
votes
2
answers
673
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.
...
2
votes
1
answer
3k
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
309
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
754
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
100
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
41
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
155
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
643
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
301
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
128
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
124
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
299
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
70
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
261
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
243
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
369
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
92
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 ...