Questions tagged [variables]

For questions about vimscript variables, their scopes, and usages.

Filter by
Sorted by
Tagged with
2
votes
1answer
293 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
2answers
105 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
1answer
19 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
1answer
22 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
2answers
31 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
2answers
67 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
1answer
38 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
2answers
113 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
1answer
33 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
1answer
31 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
1answer
23 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
1answer
50 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 ...
2
votes
1answer
56 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
2answers
186 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
1answer
54 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
1answer
384 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 ...
8
votes
1answer
536 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
2answers
443 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
2answers
463 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
2answers
43 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.
1
vote
2answers
404 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
1answer
77 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
1answer
83 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
1answer
828 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 ...
0
votes
1answer
208 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 ...
0
votes
2answers
1k 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
1answer
62 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
1answer
471 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
1answer
136 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
2answers
39 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?
1
vote
1answer
274 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 (...
2
votes
2answers
383 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
0answers
333 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
2answers
195 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
2answers
303 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 %...
1
vote
2answers
619 views

How to quickly edit vim variable content?

In vim I sometimes want to quickly change the content of a variable using its current content as template using vim editing capabilities. For example I want to append the word "bar" to the existing ...
4
votes
2answers
3k views

How can I use a string variables for filepath in vimscript map command?

I have multiple lines like the once below in .vimrc file. nnoremap \html :-1read $HOME/.vim/snippets/html_basic.snippet<CR> nnoremap \bs :-1read $HOME/.vim/snippets/bootstrap.snippet<CR>...
2
votes
1answer
144 views

How to match pattern with buffer-scoped variable

Goal: write code to do block commenting/uncommenting while keeping the current indentation level. This has been solved, see here for my code Current problem: use s/// to match a line that begins with ...
0
votes
1answer
242 views

How does Vim sets 'shell'?

I have an issue with Vim in NixOS. The problem is that Vim sets to shell the path to not desired bash (non-interactive bash). So I want to know how Vim finds a shell and sets its path to shell to ...
0
votes
1answer
42 views

Vim not using variable value when changing a setting

I have the following vimscript: let g:base_path="~/.config/nvim/" let g:plugins_path=base_path."plugins/" let g:repos_path=plugins_path."repos/github.com/" set runtimepath+=g:repos_path."Shougo/dein....
1
vote
0answers
201 views

How to open a file with variable in the path?

gf runs fine with absolute or relative path. Is it possible to open a file with variable in the path? For example, in a tcl file: ... set FILE_ROOT /user/abc ... read_file ${FILE_ROOT}/some/path/...
0
votes
2answers
209 views

How to correctly use a local script variable

Suppose I have the following code: let s:user_config=fnamemodify('user.vim', ':p') echom 'file found: ' . s:user_config if filereadable(s:user_config) source s:user_config else echom s:...
0
votes
1answer
73 views

passing local script variables to viml functions

If I run this code: let s:user_config=fnamemodify('user.vim', ':p') echom s:user_config if filereadable("s:user_config") echom 'this is true' else echom 'this is false' endif the result will be: ...
-2
votes
1answer
37 views

How do I find out the value of an option? [duplicate]

How do I find out the value of an option? I tried: :echo ignorecase :echo g:ignorecase :echo v:ignorecase None of these work.
1
vote
1answer
390 views

How do you use variables in a search?

I'd like to pull out a variable something like this to save repeating the same thing let s:twoLinesStartingWithWords = '^\w\+.*\n\w\+.*' "next/previous definition nnoremap <Leader>n /\=s:...
1
vote
0answers
73 views

why is b:changedtick never zero?

As the Title states b:changedtick is never zero. No plugin or special setting seems to be involved. Even : vim -u NONE -U NONE :echo b:changedtick returns a value != 0 The same happens if I start ...
1
vote
0answers
525 views

Invoking a command with a defined variable results in “undefined variable”

I'm trying to integrate opam-user-setup's output with vim-plug. I've produced the following: let s:opam_share_dir = system("opam config var share") let s:opam_share_dir = substitute(s:opam_share_dir, ...
2
votes
1answer
393 views

What is the difference between let mapleader =”,”, and let g:mapleader=”,”

In one of the most popular vimrc’s available (Aimx’s vimrc), there is a line which maps both leader and g:leader to “,”. https://github.com/amix/vimrc/blob/master/vimrcs/basic.vim#L58 What is the ...
1
vote
1answer
44 views

variable names prefixed with &

It seems that & followed by an option name is an variable containing the option value, such as &diff. What is &l:diff. I assume it is the local value for diff option, but I cannot find the ...
1
vote
1answer
565 views

the scope of variable in vim

I believe the following script in vimrc, which try to set spellfile, has something to do with the scope of vim variable. Especially the difference of $var and g:var: I can't figure out what's wrong ...