Questions tagged [variables]
For questions about vimscript variables, their scopes, and usages.
94
questions
2
votes
1
answer
48
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
75
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
441
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
35
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
80
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
85
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 ...
2
votes
2
answers
907
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
28
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
461
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
37
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
502
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
235
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
302
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
576
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
50
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
36
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
72
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
391
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
180
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
123
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
74
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
129
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
64
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
1
answer
113
views
switch current directory between a base directory and current file directory
Goal:
To achieve filename/path completion relative to current file directory using omni completion CTRL-X CTRL-F.
I open all my files contained in a base directory such that :pwd in some_file outputs ...
4
votes
1
answer
143
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
254
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
76
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
974
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 ...
11
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
1k
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
620
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
91
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
180
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 ...
0
votes
1
answer
142
views
Ex command to put value of variable into terminal emulator?
Using a terminal emulator (with :term), I would like to insert the value of a variable at the terminal cursor.
In a normal text buffer, I could use put =g:my_variable or exec ("normal! i" . g:...
4
votes
1
answer
3k
views
How to get the current mode in vim?
I'm writting scripts for vim, and I want to let the script do different things in different mode. I know that the statusline will show the current mode, but I don't know how to get the exact current ...
3
votes
2
answers
3k
views
How to generate a string by concatenating another string with an environment variable
I need to create a variable with a fool folder direct which need to concatenate from a environment variable and the end notes
function! MyFoo(arg)
let repo=$Project + "/cfora" #Q1 ...
1
vote
2
answers
4k
views
How to use variables? [duplicate]
I'm trying to use indent size as variable, but it doesn't work.
let indent=4
set tabstop=&indent
set softtabstop=&indent
set shiftwidth=&indent
set expandtab
I tried both &indent and ...
1
vote
1
answer
67
views
How to define a varialbe and refer to it later
I need to define variables who can hold number or strings. Those variables would be used later in the key mapping
for example
let bookMark=Lib7 //:echo boomMark do give out Lib7
nmap gb :<c-u>...
4
votes
1
answer
547
views
How can I use the return value of a function in a :set command?
I want to set an option to a string containing full path of current working directory. For example
set tags=getcwd()."\tags" " retrieve the full path of the tags file in current ...
1
vote
1
answer
198
views
Is there a way to get a tab-local variable for a tabpage that is not currently activated
I want to retrieve a t:varible for a different tabpage that is not the current tabpage. How can I do it without switching to that tabpage? I know its tabpagenr.
How about window local variables w: ...
1
vote
2
answers
73
views
How to edit variable's content?
For example, I've used redir command to redirect command output to register a.
Register a contains several lines of text.
How to remove a line from register a or substitute a string from it?
2
votes
1
answer
492
views
Give variable to makeprg
I'm trying to put the output of a command into a QuickFix list when a particular key is pressed. That command takes as its argument the word under the cursor, so my function looks basically like this (...
3
votes
2
answers
823
views
How to dynamically define variables?
Want to read variable definitions from a tcl file, then dynamically define vim global variables.
set PATH_A /full/path/a
set PATH_B /full/path/b
other_tcl_commands
Read one line, split by space ...
2
votes
0
answers
564
views
Why gf is not working with variable?
If there is no variable in path, gf can open the file.
Added sfname+={,} to .vimrc to support ${VARIABLE}.
:!echo ${VAR}/test/file can correctly print full path with ${VAR} expanded.
But if run 'gf' ...
0
votes
2
answers
354
views
How to analyse file and get variable definition?
For example, a tcl file, how to analyse the file and get each variable's value?
It can be done by reading each line, check if there is "set XXX ...", then get variable name and its value.
After ...
0
votes
2
answers
697
views
Statusline – update variables on file change
I'm currently learning Vimscript, but I'm stuck at a particular problem.
I'm creating a statusline which shows the full path (~) of the file currently displayed in the buffer. Instead of just using %...