For the YouCompleteMe plugin, I would like to set the parameter g:ycm_path_to_python_interpreter in my vimrc to the path of the system python3 installation.
I am using let g:ycm_path_to_python_interpreter = system('which python3') however that is invalid, as the system(..) returns the string of the python3 path in a separate buffer it seems like. What I mean is that I see
/home/ubuntu/anaconda3/bin/python3
Press ENTER or type command to continue
when I :echo g:ycm_path_to_python_interpreter. I would expect just the path as string (i.e. /home/ubuntu/anaconda3/bin/python3). How can I do that?
The issue you're having is that system() will include the newline at the end of the command.
You can see that with the command:
:let g:ycm_path_to_python_interpreter
Which will show you:
g:ycm_path_to_python_interpreter /home/ubuntu/anaconda3/bin/python3^#
(The ^# at the end represents the newline character.)
To fix that, just use a call to trim() around the system() call.
let g:ycm_path_to_python_interpreter = trim(system('which python3'))
I often use gf in vim to open files under cursor. Often these file paths use environment variables but when in .tcl script files vim is unable to use the environment variable.
This works for gf:
$tcl_lib/myfile.tcl
These do NOT work for gf:
$env(tcl_lib)/myfile.tcl
$::env(tcl_lib)/myfile.tcl
These are some of the things I have tried:
:set isfname=#,48-57,/,.,-,_,+,,,#,$,%,~,=,{,},(,)
:set isfname=#,48-57,/,.,-,_,+,,,#,$,%,~,=,{,},40-41
:set includeexpr=substitute(v:fname,'\$env(\([^)]\+\))','\$\1','')
Is there a way to make vim understand the syntax for environment variables in tcl scripts (specifically for the 'gf' command)?
There are a few techniques:
Set 'path' & 'includeexpr'
In theory you can just add $tcl_lib to path. e.g. set path=.,$tcl_lib,,. However, any filename starting with / will fail. This can be remedied by removing the starting /.
Add to ~/.vim/after/ftplugin/tcl.vim:
set path=.,$tcl_lib,,
let &l:includeexpr="substitute(v:fname, '^/', '', 'g')"
Reading Environmental variables via 'includeexpr'
Can use a substitution to expand environment variables
let l:includeexpr = "substitute(v:fname, '$\\%(::\\)\\=env(\\([^)]*\\))', '\\=expand(\"$\".submatch(1))', 'g')"
This uses a sub-replace-expression (See :h sub-replace-expression) to use expand() to get the environmental variable.
This might require you to change 'isfname' to allow more characters tto be a part of a filename looking string.
Just map gf and friends
Make buffer-local mappings for gf, <c-w>f, etc which are specific to your language and check certain paths. This completely side-steps many of Vim's built in methods so it should be used as a last resort.
Finally got back to this and was able to solve it in a pretty good way. Add the following tcl.vim file to your ~/.vim/ftplugin and your "gf" should work!
https://github.com/stephenmm/dotfiles/blob/master/vim/ftplugin/tcl.vim
" Add charecters to possible filename types so vim will recognize:
" $::env(THIS)/as/a/file.tcl
set isfname+={,},(,),:
" Turn the string into something vim knows as a filename:
" $::env(THIS)/as/a/file.tcl => ${THIS}/as/a/file.tcl
function! TclGfIncludeExpr(fname)
if a:fname =~? '\$\(::\)\?env([^)]\+)'
return substitute(a:fname, '\$\(::\)\?env(\([^)]\+\))', '${\2}', 'g')
endif
return a:fname
endfunction
" Tie the function to includeexpr
set includeexpr=TclGfIncludeExpr(v:fname)
Adding below 2 lines in ~/.vimrc will work for me.
set isfname+={,},(,),:
let &l:includeexpr = "substitute(v:fname,'$\\%(::\\)\\=env(\\([^)]*\\))','\\=expand(\"$\".submatch(1))', 'g')"
I'm trying to modify my vimrc to include a directory
let g:mydir = system('which someExecutable')
execute "set rtp+=" . g:mydir
The problem is that which someExecutable returns something like
/aDir/a/b.
I need g:mydir set to /aDir/, so two dirs above b.
Is there an easy way to do this in vimscript?
You're looking for fnamemodify(path, ":h")
If you version of vim is recent enough, you can even use exepath('someExecutable') instead of system('which someexecutable'). Which gives:
fnamemodify(exepath('someExecutable'), ":h")
PS: don't forget to escape what must be escaped if you use exe "set rtp+=....
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.
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