why I can't set path success.
let s:WORKDIR = getcwd()
set path += ".," . s:WORKDIR . "/**"
echo &path
and the echo result is, my current directory is "/home/myname/example", my expected result is
".,/home/myname/example/**", but what i get is,
.,/usr/include,,
it seems this didn't work in my .vimrc script; please help, thanks.
Your syntax of the :set command is wrong; you should be getting errors, too. The += must not be surrounded by whitespace, and you cannot use an expression on the right-hand side. Better use the :let command; it can also modify Vim options (&optionname), not just variables:
let &path .= ",.," . s:WORKDIR . "/**"
Related
This is inside a script:
:let s:submission_path = expand("%:p:h") . '\submissions\test_submission.csv'
:echo s:submission_path
:write s:submission_path
The script raises an error "can't open file for writing", because I'm on windows and ':' is not a valid filename character. If I name the variable 'submission_path' a file with that name is written (edit: and the contents of the file are as I expect, the contents of the buffer the script is editing).
I don't understand this. The echo command before the write does what I expect it to, output the full path to the file I want to write. But write seems to ignore the variable value and use its name instead? Do I need to dereference it somehow?
Try this:
:let s:submission_path = expand("%:p:h") . '\submissions\test_submission.csv'
:echo s:submission_path
:execute "write " . s:submission_path
Execute will allow you to compose an ex command with string operations and then execute it.
vim has very screwy evaluation rules (and you just need to learn them). write does not take an expression it takes a string so s:submission_path is treated literally for write. However echo does evaluate its arguments so s:submission_path is evaluated.
To do what you want you need to use the execute command.
exec 'write' s:submission_path
Environment variables don't have this problem, but you have to dereference them like in unix with a dollar sign:
:let $submission_path = expand("%:p:h") . '\submissions\test_submission.csv'
:echo $submission_path
:write $submission_path
AND it modifies your environment within the vim process, so just be aware of that.
I have a path stored in a variable (say l:s) and want to execute lcd l:s in a vim script, but it tells me the path "l:s" doesn't exist. What is the problem here, because vim resolves variable names in other ex commands just fine (echo, etc.). I don't understand the difference.
You can use exe and construct the command:
let s:some_dir_name = "foo"
exe "lcd " . s:some_dir_name
That'll evaluate the variable s:some_dir_name and execute the command lcd foo.
(I didn't use l:s from your question because that's not a legal variable name, but I think you get the idea.)
Vim lets you set environment variables within a script, and these work with :cd and :lcd. For example:
function foo()
let $SOME_PATH = '/some/path'
lcd $SOME_PATH
endfunction
For example:
If my current directory is /temp/src/com. And the file edited in vim is from /java/test.And now i want to add the path of the file to path environment. So if there is a cmd like set path+=$(filepath) in vim?
case 2:
Run make in terminal will start to compile a project, and it will out put logs about this compile. And now i want to read the outputed logs into vim using some command like r !make.
1) Pull the path into the current Vim buffer:
:r !echo \%PATH\%
Append to the path:
:let $PATH="C:\Test" . $PATH
2) This question is ambiguous, because it depends on your makefile behavior.
If your Makefile simply print to the console, then, :r make should do the trick.
If your make file actually writes to files explicitly, then there is no automatic way.
You'll have to write a custom vimscript function to pull in the logs.
1) Part 2
I do not know of what a way to do it in one line, but here's one way to achieve the functionality you want.
:redir #a "redirect output to register a
:pwd
:redir END "stop redirecting
:let #a = substitute(#a, '\n', '', 'g') "remove the newlines
:let $PATH=#a .":". $PATH
You should be able to wrap this in a function if you need to use it often.
You may reference environment variables using $MYVAR syntax. To set system environment variables use
let $MYVAR=foo
e.g.
let $PATH = "/foo" . $PATH
See http://vim.wikia.com/wiki/Environment_variables or :help :let-environment
Then you may use filename-modifiers to get directory name of a file in a current buffer:
let $PATH = expand("%:p:h") . $PATH
To read and parse compilation output in vim you might be interested to check quickfix mode
Use :make instead of :!make
I'm looking for an example of how to use the findfile function in a vim script to search upwards recursively for a file, specifically using a wildcard.
Whenever I include a wildcard character as part of the first function parameter this doesn't seem to work.
For example, with the following directory structure:
~/MyProject/
Test.sln
src/
Test.cs
If I run the following function, while editing the file Test.cs with pwd set to ~/MyProject/src
function! Test()
let a = findfile("*.sln", ".;")
echo a
endfunction
findfile appears to return nothing. However, if I modify the function to remove the widcard as follows:
function! Test()
let a = findfile("Test.sln", ".;")
echo a
endfunction
It does what I would expect.
I've tested this on both ubuntu and windows and I see the same behavior on both. Am I doing something wrong here, or does findfile just not support wildcard characters? A lack of support for the wildcard character seems like a fairly strange omission. It seems like I must be doing something wrong here.
If you're using wildcards I think the glob() and/or globpath() functions are what you're looking for. See :h glob() and :h globpath().
One way to do it with external (fast) find
function! Test()
let l:list=system("find .. -maxdepth 1 -name \*.sln")
echo l:list
endfunction
There is a vim variable with the string value like this:
foo%:p:r:sbar
%:p:r:s is a vim wildcard.
How can I expand all wildcards in the string variable?
Unfortunately expand(my_variable) doesn't help.
Do I need to specify any additional argument or use other vim function?
Thanks.
Try :help expand()
This section of the docs seems especially relevant to you:
Modifiers:
:p expand to full path
:h head (last path component removed)
:t tail (last path component only)
:r root (one extension removed)
:e extension only
Example: >
:let &tags = expand("%:p:h") . "/tags"
Note that when expanding a string that starts with '%', '#' or
'<', any following text is ignored. This does NOT work: >
:let doesntwork = expand("%:h.bak")
Use this: >
:let doeswork = expand("%:h") . ".bak"
It looks like your trailing (and possibly leading) strings won't work with expand().
This does work though:
:echo "foo" . expand("%:p:r:s") . "bar"
Possibly you can rework your script so wildcards are expanded before they are combined with other strings. Alternatively you could try to split the concatenated string, expand the wildcards, then re-concatenate.
What about
join(map(split(mystring, '\ze[<%#]'), 'expand(v:val)'), '')
?