19

If you launch your first Vim session with the argument --servername VIM: $ vim --servername VIM then you can send any command from any other shell with the argument --remote-send. For example, if you use your statusline to display the value of some variable g:myvar, and you have the following lines in your vimrc: let g:myvar = "foo" set stl+=%{g:myvar} ...


10

You can use :let with Vim options as well, by prefixing the option name with a & sigil; cp. :help :let-option let &tags = getcwd().'\tags' Note that the literal string must be in single quotes (or the backslash doubled); else, the \t will expand to a tab character. The other way would be by using :execute, but then you'd have to take care of ...


9

You could use :exe, but this is extremely cumbersome to use and you'll need to escape a few things. I used to use my own path fixing function. It looked like: exe 'set rtp+='.lh#path#fix(somevariable) " with standard tools, may be it'd be (untested) exe 'set rtp+='.escape(somevariable, ' \|,') let &rtp = expression is really our friend. But indeed, ...


9

Yes you can, but Vim calls it list unpacking. It can be found in Vim's help: :h let-unpack Vim allows you to create and populate two variables from a list: let [var1, var2] = mylist which is equivalent to: let var1 = mylist[0] let var2 = mylist[1] There is also an (untagged) section on that help page titled "List unpack". It says you can do: let [...


9

Poking around with :helpgrep \<g:\A, I found this is documented under :help internal-variables: There are several name spaces for variables. Which one is to be used is specified by what is prepended: (nothing) In a function: local to a function; otherwise: global |buffer-variable| b: Local to the current buffer. |window-variable| w: ...


8

This command does what you want: :let i = 1|g/^Do/s/^/\=i/|let i = i + 1 Explanation… let i = 1 initializes counter i, g/^Do/s/^/\=i/ prepends i to each line starting with Do, let i = i + 1 increments i. The trick is that the incrementation happens before the next substitution. --- edit --- If we used a single substitution, the counter would only be ...


8

The reason your example attempts don't work is that in many locations text is simply seen as a literal string, rather than VimScript. So functions, variables, and the like don't work. For example, if you do: :let var='value' :set option=var Then Vim will simply set the value of option to the literal value var, since it doesn't recognize VimScript in :set. ...


7

The :map command takes :help key-notation, but not variables. Vimscript is evaluated exactly like the Ex commands typed in the : command-line. There were no variables in ex, so there's no way to specify them. When typing a command interactively, you'd probably use <C-R>= to insert variable contents: :sleep <C-R>=timetowait<CR>m<CR> ....


7

You've picked an unfortunate example, as it's more easily achieved with: let myvar .= 'bar' Still, for more complicated edits, you could use the following commands: Insert the variable into the buffer: o<C-R>=myvar<CR> N.B. In the above, <C-R> denotes a press of Ctrl-R, and <CR> is a press of Return. Perform your edits. Reassign ...


7

I though it wasn't possible, but I was wrong. See :h :func-closure You need to patch two things: add closure at the end of the inner of the function declaration respect the usual naming conventions For instance function! s:my_function(dict_arg) let darg = copy(a:dict_arg) func! s:my_inner_func(cond) closure return darg[a:cond] endfunc ...


6

You could have your variable be a list and add values with :let g:var += ['item']. However, it won't be possible to prevent the same item to be added several times. That's why I've come up with lh#list#push_if_new() function! lh#list#push_if_new(list, value) abort let matching = filter(copy(a:list), 'v:val == a:value') if empty(matching) call add (...


6

One method is to use a <expr> mapping which evaluates the RHS of the mapping as Vimscript rather than use it literally as for regular mappings. The tricky part of this is the fact that we have two levels of variable expansion: snippet_home and the environment variable contained within, $HOME. The first approach expands the env var when the containing ...


5

You can capture output with execute() function and then pump it through sort(). The whole command could be then: command! -bang -nargs=1 -complete=command Sorted \ echo join(sort(split(execute(<q-mods> . ' ' . <q-args>), "\n"), \ {s1, s2 -> <bang>(s1 ># s2) - <bang>(s1 <# s2)}), "\n") Now you can do :Sorted ...


5

Use :h <afile> to get unloading buffer name, use :h getbufvar() to get buffer local variable. let b:example_tempfile = tempname() augroup example au! au BufUnload <buffer> echom getbufvar( expand('<afile>'), 'example_tempfile' ) augroup END bunload % -------------------------------- /tmp/vovl8Be/48


4

Would be nice to see the full error, on which line it complains. Nevertheless you have error in the script, the set command requires no spaces around =, or spaces at all after = sign. You should also use setlocal, since set works globally (it will set for all buffers). And whole auto-command expression should be enclosed in execute with concatenation with ...


4

You could use an environment variable defined within your current shell session. let $myvimdir = '/home/abc/vimfiles' That way you can use other variables to compose the path as well: let $myvimdir = $HOME.'/vimfiles' And set the runtime path: set rtp+=$myvimdir/bundle/Vundle.vim More information on environment variables within vim :help expr-env


4

To put the value of a variable into a buffer use the :put command with the expression register, @=: put=b:aaa You can use a range with :put to put it at a specific location. Put below the 9th line: 9put=b:aaa Note: use :put! to put above the current line For more help see: :h :put :h @= :h :range


4

When you use a variable without a scope (i instead of b:i or w:i, etc.) outside of a function, you're using a global variable. From :h internal-variables: There are several name spaces for variables. Which one is to be used is specified by what is prepended: (nothing) In a function: local to a function; otherwise: global buffer-variable ...


4

It has to do with the nature of exists(): Note that the argument must be a string, not the name of the variable itself. For example: exists(bufcount) This doesn't check for existence of the "bufcount" variable, but gets the value of "bufcount", and checks if that exists. Since exists() examines the contents of the argument, if you want to ...


4

In the .vimrc, there is no difference between them. Without an explicit scope, vim chooses between g: and l: depending on the context. l: is implicit within functions, g: is implicit everywhere else.


4

You can include variables in mappings by building the map command as a string and executing it with the :execute command: let s:twoLinesStartingWithWords = '^\w\+.*\n\w\+.*' execute 'nnoremap <leader>n /' . s:twoLinesStartingWithWords . '<CR>' execute 'nnoremap <leader>N ?' . s:twoLinesStartingWithWords . '<CR>'


4

It depends on how you get the result of your external command but you might be interested in :h systemlist(). It executes the command given as parameter and returns a list containing every lines of the output. You can then use :h list-functions on the result or simply listVar[1:]. I think that is more robust and portable than matching the lines and doing ...


4

Sorry, but I very quickly answered my own question, hadn't read far enough down in the docs! let &l:option='value' will set the local value, with let &g:option='value' explicitly setting the global value.


4

Yes. For a local buffer/window option, you can use: let &l:option = 'complex value' And you can also be explicit about a global option, with: let &g:option = 'complex value' See :help :let-option, which includes: :let &l:{option-name} = {expr1} Like above, but only set the local value of an option (if there is one). Works like :setlocal. ...


3

If it is from statusline, then :h statusline is what you need. Vim has help for every single variable that you can set in .vimrc (at least the ones that come with pure Vim, plugins usually also have good docs), thus :h <variable name> should be your friend. This is from :h statusline: F S Full path to the file in the buffer. m F Modified flag, ...


3

The idea was to be able to access the data bits with the . notation. For example g:myTree.base should return __base. However, I have noticed that I need an additional key path in order to be able to package the nested information, so the access is done through g:myTree.base.path for __base. This is not really how dictionaries (or hashes, or tables, or ...


3

I don't think you can set a key as the "default" for a dictionary. You should probably look into the other supported scripting languages, like Python or Ruby. As for the second problem, you can check the type: function! RecurseForPath(dict) for key in keys(a:dict) if key == 'path' echo a:dict[key] elseif type(a:dict[key]) == ...


3

If you want g:myvar to be a string, you could define the following commands :AddMyvar and :RemoveMyvar: command! -nargs=1 -complete=customlist,s:CompleteMyvar AddMyvar \ if !exists('g:myvar') | \ let g:myvar = '<args>' | \ elseif !count(split(g:myvar, ','), '<args>') | \ let g:myvar .= ',<args>' |...


3

The correct syntax for doing this with redir (which works in earlier versions of vim than execute()) is this: redir => list3 oldfiles redir END Or if you really want it to be a one liner: redir => list3 | oldfiles | redir END This is described about halfway down the documentation for :help :redir. As for why the other ideas you tried didn't work: ...


3

You're looking for %:h/ NB: you can tab-complete at any point after the :h.


Only top voted, non community-wiki answers of a minimum length are eligible