In a script, I have the following not working code:
set eval(rules[formatoption])=value
where rules is a dictionary and formatoption and value are a variable. I want to make Vim read the variable name from rules[formatoption] and set it to value. How can I make Vim set a variable this way? I think there should be a function like setvar(name, value)or something similar, that sets name(string) to value. That line of code would save me from writing about 30 lines of code in a 70 lines script.
Use :execute:
execute 'set' rules[formatoption] . '=value'
You can also change Vim options via :let &optionname = ..., but that doesn't help here. There's also the obscure :help curly-braces-names, but that won't work here, neither.
Related
Recently I wrote a text file without a file extension with the vim text editor and got text highlightning for all words between singel quotes like 'word' and all words ending with a colon like word:
I like this kind of highlightning.
But because I havent typed any command, which usually starts with ESC :command, I was surprised how this could happen.
Is there a command to display the actual syntax highlight which is in use?
I already have tried the command :set syntax=php which seems simliar only with different color for words like word:
Does anyone have suggestions?
Try entering the command
:echo &syntax
This will display the value that the syntax variable has been set to.
set option=value "set the option with value
set option? "read/print the current value of the option
set option! "set the option with opposite value, like set nu and set nu!
the option value is also saved in variable &option so you can read in script way.
I am trying to create a macro to use it globaly. I have inserted this command to my .vimrc file
let #c='<esc>:r c.c'
or
let #c=':r c.c'
but in both cases when I use "#c" on any file it only prints 'c>:r c.c' on the the file
Try adding a '^M' at the end of your macro, then "#c" should work. Else ':#c' should work as mentioned by ebenezer. You should use Ctrl+VEnter to insert '^M'.
let #c=':r c.c^M'
Best way would be to record the macro first and then save it to the .vimrc.
If these doesn't work, you can check the content of your register c using "cp and see if there is something missing.
This is described in another answer. Try something like this:
let #c = ':r c.c'
and then, in your file, use
:#c
to execute it.
If you want to include special characters in your macro (like Escape or Enter) then use a non-literal string (double quotes, not single quotes) and the correct syntax. See :help expr-string. You could use raw special characters, as #Amit suggests, but this will make your vimrc file hard to read.
Assuming that you are starting in Normal mode and want to type #c, there is no need to start off with an Escape.
For testing purposes, I tried
:let #c = ":echo 'foo'\<CR>"
and it worked as expected. I even added this line (without the leading :) to my vimrc file just to make sure, and tested it that way. This should work:
:let #c = ":r c.c\<CR>"
I am trying to do something pretty simple in my vimrc style. As you can see below I am trying to concat two environmental variables into a local variable and then use that variable.
let cs=$menv_dotfiles_dir."/vimrc_style/".${CODING_STYLE}.".vim"
if filereadable(cs)
source cs
endif
Of course the above doesn't work but I think it expresses what I need to do. What is the correct way to do this?
Similar to this: Vim: sourcing based on a string But I need to do it only if the file exists.
You were very close the {} brackets do not work like they do in bash. You have to use exec if you want source from a string variable.
This works:
let cs=$menv_dotfiles_dir."/vimrc_style/".$CODING_STYLE.".vim"
if filereadable(cs)
exec 'source ' . cs
endif
I am reading Learn Vim Script the Hard Way and hit something that confused me whilst doing the exercise to convert the folding functions to script local ones.
I tried to go this:
setlocal foldexpr=<SID>GetPotionFold(v:lnum)
and renamed all the functions to start with s:
To my surprise this didn't work and every line had a fold level of 0? It works if I put GetPotionFold into the global scope. Do you have to use a globally scoped function when assigning it to a option? Why?
The <SID> can be used in a mapping or menu, unfortunately not in an option. (This is a shortcoming in the implementation.)
You'd either have to translate it into the actual <SNR>NNN_ prefix (there's an s:SID() example function at :help <SID>), or use a different scope that is accessible from outside the script that defines the function. It's commendable that you want to avoid clobbering the global function namespace, as this is prone to name clashes.
A nice trick is using the autoload function prefix; it doesn't just work in autoload scripts, but can also be used elsewhere, e.g. in plugin scripts. Just prepend the script's name, and you'll have a function that can be invoked from anywhere, but scoped to the script's name:
:function! MyScriptName#GetPotionFold(lnum)
...
:setlocal foldexpr=MyScriptName#GetPotionFold(v:lnum)
Adding to the previous answer, you could define the function s:SID() to determine the script number as in the help documentation and then use execute to set the fold expression as following:
exe "setlocal foldexpr=<SNR>" . s:SID() . "_GetPotionFold(v:lnum)"
I'm trying to add an autocmd to vim that will execute whenever I open a file in a certain subdirectory and that sets the search path. Unfortunately path name expansion doesn't seem to work inside a set command.
Specifically I'd like to have a line like this in my vimrc:
setlocal path+=**;%:p:h
But this will just give me the literal value. Just calling expand() doesn't work either. Is there a way to get variable expansion to work here?
What about:
execute 'setlocal path +=**;' . fnameescape(expand('%:p:h'))
There's no need for the expansion of the current file's directory; just adding . to path will do. From the help:
To search relative to the directory of the current file, use:
:set path=.
Use
let &l:path.=(empty(&l:path)?(''):(',')).'**;'.escape(expand('%:p:h'), ',\*; ')
. This is much cleaner then using :execute 'setlocal path', especially knowing that fnameescape() was designed to escape paths for commands, not for options and I can say it is not really safe to use it here: it definitely is not going to escape comma and semicolon and add additional escape for space (one for escaping for :set, one for the option itself). (empty(&l:path)?(''):(',')) is here to imitate the behavior of set+=.