How to set an option using a string in a vim script - vim

I am trying to set an option but it does not work when using a variable.
This is what actually works:
set runtimepath+=~/.vim/bundle/foo/foo.vim/
When I try this, it DOES NOT work anymore:
g:foo_path = '~/.vim/bundle/foo/foo.vim/'
set runtimepath+=g:foo_path
I have seen a similar topic here and they use the following command to set an option with a variable:
let &backupdir=s:vimetc.'backups/'
However, when I try this:
let &runtimepath+=g:foo_path
It still DOES NOT work. I am getting:
E734: Wrong variable type for +=
Any ideas? Thanks.

The problem is set does not support using string variables and let does not support += for string types.
This should work:
let g:foo_path = '~/.vim/bundle/foo/foo.vim/'
let &rtp.= ',' . g:foo_path

another workaround
exe 'set runtimpath='.a:lines
after reviewer's comment, a 2.0 version
exe 'set runtimepath='.&runtimepath.','.g:foo_path

You've almost got it. Appending options is done (regardless of the type of the option value, i.e. string vs. number) via :set option+=val, whereas the concatenation operation with String variables is done via :let &option .= var. You get the E734 error because += with :let is for numerical addition only.

Related

Modify function/variable values of the plugin via vim config

I use https://github.com/pbogut/fzf-mru.vim plugin which writes information in:
/Users/karl/.cache/fzf_mru/cache.txt
but I would like to set my own path to the file. As far as I understood by default there is no such possibility, so I'm looking for a way to do it via vim config, i.e. without changing plugin itself.
This plugin basically consists of 3 files and in:
https://github.com/pbogut/fzf-mru.vim/blob/master/autoload/fzf_mru/mrufiles.vim
we can see that there is a function fzf_mru#mrufiles#cachefile() which points to the necessary file. :echo fzf_mru#mrufiles#cachefile() also points to:
/Users/karl/.cache/fzf_mru/cache.txt
and if I understand correctly then I need to change the value of variables of this function. Something like let g:cafile='/a/b/file.txt' in the config.
But all my attempts were unsuccessful, apparently I do not quite understand how to do it. Maybe someone can give me a hint. Maybe it is not possible to do this at all without modifying the plugin itself?
s:cache_dir is a script-local variable that can't be overridden with a global one so you can't change the behaviour of the functions from the outside. This means that the code must be changed to allow user customisation.
You have two options:
do it yourself… and send a PR,
open an issue and let the maintainer do it.
If you choose option #1, you will have to change that function:
fu! fzf_mru#utils#opts()
let s:lash = fzf_mru#utils#lash()
let usrhome = $HOME . s:lash( $HOME )
let cahome = exists('$XDG_CACHE_HOME') ? $XDG_CACHE_HOME : usrhome.'.cache'
let cadir = cahome.s:lash(cahome).'fzf_mru'
let s:cache_dir = cadir
endf
to something like:
fu! fzf_mru#utils#opts()
let s:lash = fzf_mru#utils#lash()
if exists('g:fzf_mru_cache_dir')
let s:cache_dir = g:fzf_mru_cache_dir
else
let usrhome = $HOME . s:lash( $HOME )
let cahome = exists('$XDG_CACHE_HOME') ? $XDG_CACHE_HOME : usrhome.'.cache'
let cadir = cahome.s:lash(cahome).'fzf_mru'
let s:cache_dir = cadir
endif
endf
which should allow you to define your cache directory in your vimrc:
let g:fzf_mru_cache_dir = '$HOME/foo/bar'

Replacing a certain part of string with a pre-specified Value

I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"

Converting match object to string in perl6

I was trying to convert a match object to a string in perl6. The method Str on a match object is defined as:
method Str(Match:D: --> Str:D)
I would think I could use Str($match) to accomplish this. And it seems to convert it to a string, but I'm getting an error using the string with the following code:
my $searchme = "rudolph";
my $match = $searchme ~~ /.*dol.*/;
say $match.WHAT;
my $test1 = Str($match);
say $test1.WHAT;
say $test1;
With the output:
(Match)
(Str)
With the error:
Cannot find method 'gist': no method cache and no .^find_method in
block at .code.tio line 6
However, if I run:
my $searchme = "rudolph";
my $match = $searchme ~~ /.*dol.*/;
say $match.WHAT;
my $test1 = $match.Str;
say $test1.WHAT;
say $test1;
I get no error and the result:
(Match)
(Str)
rudolph
Is this a bug or me misunderstanding how it works?
Thanks for reading.
I'm writing this up as an answer even though it's actually an incomplete discussion of a bug, so not at all normal SO fare. The alternative of lots of comments doesn't seem better.
It's a bug. Perhaps you just golfed this.
dd $test1; instead of say $test1; is helpful in that it displays BOOTStr $test1 = (BOOTStr without .perl method).
Based on that I searched the rakudo repo for BOOTStr and that led to the above issue.
Golfing it further leads to:
say $ = Str(Match.new);
Note that these are all fine:
say Str(Match.new);
say $ = Int(Match.new);
say $ = Str(Date.new: '2015-12-31');
It appears to be a combination of leaking some implementation details regarding how Rakudo/NQP/MoarVM bootstrap; Match being an NQP object; Str() on that being wonky; and assigning it to a Scalar container (the $ is an anonymous one) making that wonkiness visible.
I'll add more when/if I figure it out.

Multiple lines for one String batch

I Googled it but i'm not able to find a GOOD solution.
My goal is to put a string which is composed of 6 lines in one string, and only one, in a variable.
For example, my string can look like :
a
b
c
and I want it to be in one string. I tried the thing witch ^, or with ECHO " " but it doesn't work : the cmd put an error "not recognized as an internal command" (and it's normal, it's just some sentences, not batch commands!)
Thanks, Clément
Not so simple but possible :
#echo off
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
REM Example Usage:
setlocal enableDelayedExpansion
set "string_with_new_lines=a!nl!b!nl!c"
echo %string_with_new_lines%
Aacini has a simpler solution for this using empty variable,but I'm struggle to find the link.
Following the comments on the post answer of #npocmaka
It's currently in Python, so the """ thing works.
requete = """
PREFIX resources: <http://www.fluidops.com/resource/>
SELECT DISTINCT ?id ?marque ?modele WHERE {
?voiture resources:uid ?id.
?voiture resources:bpqmqvc ?marque. #myComment
?voiture resources:bpqmodvc ?modele
}
"""

Setting an option to the content of a file in .vimrc

I need to have one of
set foo=bar
or
set foo=baz
in .vimrc. I'd like to read either "bar" or "baz" from a certain file. That is, what one would do in sh with foo=cat file`` (I don't know how to escape backticks...). How does one do that?
let &foo = readfile('filename', '', 1)[0]
The let &foo syntax lets you set options to arbitrary expressions.

Resources