Questions tagged [variables]

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

Filter by
Sorted by
Tagged with
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 . ...
0
votes
1answer
21 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
27 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
62 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
35 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
112 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
32 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
27 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
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 ...
1
vote
1answer
48 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
53 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
179 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
51 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
328 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
371 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
387 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
451 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
322 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
59 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
75 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:...
3
votes
1answer
660 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
148 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
889 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>...
3
votes
1answer
444 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 ...
1
vote
1answer
133 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
35 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
250 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
324 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
293 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
174 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
267 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
547 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
131 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
233 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
190 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
155 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
70 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
378 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
70 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
500 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
367 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
43 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
524 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
2answers
774 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 ...
0
votes
2answers
97 views

Find value of variable when it is not set (timeoutlen)

How can I find out the default value of a variable if it is not set? For example, the default timeout for the leader key is controlled by the timeoutlen variable, but if I do: :echo timeoutlen I get ...