Set runtimepath, adding a directory from an expression in vim?
Asked Answered
S

3

21

Inn ~/script.vim, I have:

set runtimepath+=string(substitute(expand("%:p"), 'script\.vim', '', 'g'))

I have an alias in .bashrc:

alias vimscript="vim -S ~/script.vim"

Running string(substitute(expand("%:p"), 'script\.vim', '', 'g')) works as intended.

The problem is when using it in the set runtimepath expression, it doesn't work when I call vimscript in terminal which calls script.vim. When I run set rtp in vim after being called by vimscript to check the runtimepath, the desired appended string isn't showed (but the other ones are there).

Snowshoe answered 4/1, 2011 at 20:9 Comment(0)
N
29

I have some additions to @Laurence Gonsalves answer:

  1. There is also «concat and assign» operator: .=, so

    let foo=foo.bar
    

    can be rewritten as

    let foo.=bar
    
  2. Code

    let &runtimepath.=','.string(path)
    

    will append ,'/some/path' to &runtimepath, while you probably need ,/some/path.

  3. I guess that you want to append path to your script to runtimepath. If it is true, then your code should be written as

    let &runtimepath.=','.escape(expand('<sfile>:p:h'), '\,')
    

    inside a script, or

    let &runtimepath.=','.escape(expand('%:p:h'), '\,')
    

    from current editing session (assuming that you are editing your script in the current buffer).

Nickey answered 4/1, 2011 at 20:28 Comment(2)
your approach is better IMO, using substitute was indeed ugly when it's possible to use expand parameters. Thanks.Snowshoe
Or if you already saved a path into a variable, e.g. let dein_path = <my-path>, then you can simply set the runtime path with let &runtimepath.=','..dein_path (dotdot stands for concat).Glomerate
N
5

The right hand site of a set command is not an expression, it's a literal string.

You can manipulate options (the things set sets) by using let and prefixing the option name with an &. eg:

let &runtimepath=substitute(expand("%:p"), 'script\.vim', '', 'g')

To append to runtimepath with a let you can do something like:

let &runtimepath=&runtimepath . ',' . substitute(expand("%:p"), 'script\.vim', '', 'g')

(The . is the string concatenation operator.)

Northeast answered 4/1, 2011 at 20:12 Comment(5)
I think I need to read all vim documentation, and some books to understand all this concepts. I tried the second option, but still doesn't work: now, instead of string(substitute(expand( I have '' when I run set rtp. What am I missing?Snowshoe
@Somebody The expression you had included string(), which it shouldn't have. (I'd just copied your expression verbatim into the right pattern.) I've updated the answer to use the corrected form of your expression. It could probably still be simplified, as ZyX has done, but this should at least get you on the right track.Northeast
@Somebody BTW, I've never read a vim book. :help in vim is actually quite good, but there are a lot of nooks and crannies. I've been using Vim for well over a decade and I still learn new stuff about it on a regular basis.Northeast
Removing the script wasn't working. I think it was a setup issue. I've updated my question with more information. Thanks anyway, +1!Snowshoe
@Somebody Your updated questiopn shows another problem, which is that you're using expand() incorrectly. You're using % which expands to the buffer's filename, but you want <sfile>. Also, where are you putting this line, in script.vim or in your vimrc? It won't work in the latter.Northeast
N
0

by 2022 i used this:

if ( isdirectory($some_shell_variable)  )
    set runtimepath+=$vi
endif

You can use a plain vim variable or whatever else just as well.

Negrete answered 4/1, 2023 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.