Questions tagged [variables]

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

Filter by
Sorted by
Tagged with
12
votes
1answer
952 views

Talking to vim from outside

Can I change a variable in an instance of vim from another shell (without sending keys through tmux)? I ask because I would like to use latexmk's compiling_cmd, success_cmd, and failure_cmd options ...
8
votes
2answers
3k views

`set` option with variables

I am trying to rewrite adding a string to option with variable. The original string is set rtp+=~/.vim/bundle/Vundle.vim The following is the working let way: let MYVIMDIR='/home/loom/foo' let &...
8
votes
1answer
463 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: :...
8
votes
1answer
123 views

How can I use consecutive numbers in an Ex-style substitute command?

For example, let's say I want to put a number before every line that starts with the word "Do". The command would look something like :%s/^(Do )/1. \1/, but what can I do so it will apply numbers ...
6
votes
1answer
333 views

Are the internal g:, b:, w:, etc dictionaries documented?

I knew you could scope a variable by prefixing it with g: b:, etc. but I only just discoverred that the prefix itself references a dictionary containing all the scoped variables. For example, :echo b:...
6
votes
2answers
2k views

Append to global variable and completion

I would like to create a global (configuration) variable that ideally would work as a comma separated list but that we could also append to it. The idea is to have the same behavior as when using += ...
6
votes
2answers
355 views

iskeyword for latex

Context Currently, when I set ft=tex, my set isk? reads iskeyword=@,48-57,_,192-255 that is, the default for vim. My "issue" Currently, in a LaTeX string such as $\abcde_\fgh$ the w jump would ...
5
votes
1answer
466 views

Can I do array destructuring in VimL?

Can I do this in Vimscript? let [a, b] = s:MyFunc() function! s:MyFunc() return [1, 2] endfunction
5
votes
1answer
361 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 ...
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>...
4
votes
2answers
422 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. ...
3
votes
1answer
456 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 workding directory ...
3
votes
2answers
182 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:
3
votes
2answers
969 views

How to handle a nested dictionary with mixed key/values?

I would like to create a dictionary variable for a vimL script. That dictionary will hold information related to a folder structure. However, I have a bit of hard time designing this variable and also ...
3
votes
1answer
162 views

How to `unlet` a variable using from a `argdo` statement in VIM

I do have a bunch of XHTML files and I need to put an @id for all the p elements inside all those files. I have tried the following argdo approach in the following way: :cd d:\testfiles\ :args *....
3
votes
1answer
22 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 ...
3
votes
1answer
761 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 ...
2
votes
2answers
3k views

Use variable for mapping inside vimrc

I'm testing NeoVim and would like to put in my vimrc universal mapping for either vim and nvim. I've sketch sth like below so far, but it doesn't work and it's very rough. function! Meta(key) " ...
2
votes
1answer
54 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 ...
2
votes
1answer
287 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 ...
2
votes
1answer
380 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 ...
2
votes
1answer
623 views

vim cannot access variable in autocmd

I am trying to configure prettier-eslint and prettier-standard as the js formatter with neoformat: " neoformat should try formatprg where available let g:neoformat_try_formatprg = 1 augroup vimrc " ...
2
votes
1answer
719 views

As an argument of a function, when does a variable name need to be quoted?

If I define the variable foo like this: :let foo = '~/Desktop' And I want to check the length of the string it contains, then I can type: :echo strlen(foo) The output is 9, which is right. If I ...
2
votes
2answers
66 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
28 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' ...
2
votes
1answer
37 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 ...
2
votes
2answers
359 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
1answer
136 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 ...
2
votes
1answer
134 views

Strange behaviour when using variables

When I try to use a file name in a variable for the cs add command, I cannot understand what happens. I'm using a bash script myscript.sh which returns the path name of cscope database (/mydir/cscope....
2
votes
0answers
316 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' ...
1
vote
2answers
590 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 ...
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
2answers
789 views

shortcut of the current directory path when vsplit/split

I very often need to open files through vsplit/split. Very often I want to open files in the same directory. Still, I must pass their paths to vsplit/split. :vsplit @/siblingfile.txt @ is an alias ...
1
vote
2answers
814 views

Copy the values from a variable?

I set a variable b:aaa which has the value that I can see via echo b:aaa, how can I copy that to text? I know I can do it by set @a = b:aaa "ap to get it. But can I copy it to the an open file ...
1
vote
2answers
458 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
1answer
384 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
2answers
75 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
2answers
38 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
263 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 (...
1
vote
2answers
729 views

Tell Vim's put to print a string at a position given by the value of a variable

I would like to add a new line in a certain line in a buffer. So far my starting position is as follows: let newl = "\n" " set cursor to beginning of buffer call cursor(1,1) " search up to 10 lines ...
1
vote
1answer
18 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 . ...
1
vote
1answer
49 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 ...
1
vote
2answers
373 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 ...
1
vote
1answer
135 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
1answer
550 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 ...
1
vote
1answer
764 views

how can i assign the content of a function variable to an option?

this is what i wanted to do: function! Spell(lang) if a:lang == "off" set nospell else set spell set spelllang=a:lang endif endfunction unfortunately this does not work ;( this ...
1
vote
1answer
173 views

Sharing variables between different files in a plugin

Supposing I have two scripts that share variables. What is the preferred way to do this, supposing I wanted to keep the variables only in one place? Is there a way to keep them script-local and not ...
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, ...
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
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 ...