Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

vim

runtime

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).

like image 345
Somebody still uses you MS-DOS Avatar asked Jan 04 '11 20:01

Somebody still uses you MS-DOS


2 Answers

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).

like image 174
ZyX Avatar answered Oct 12 '22 00:10

ZyX


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.)

like image 21
Laurence Gonsalves Avatar answered Oct 11 '22 23:10

Laurence Gonsalves