Vim - run ctags on current python site-packages
Asked Answered
H

3

14

This is what I need - have a key that will create ctags of my python site-packages.

I have this command, that will print the site-packages path:

!python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

This is how I to the key mapping:

map <F11> :!ctags -R -f ./tags *site-packages-path-goes-here*<CR>

How do I plug in the result of one command into the key binding statement?

The reason I want to get the site-packages path at the runtime is that I use virtualenv intensively. As the result the desired path changes all the time.

Hibben answered 4/7, 2010 at 19:19 Comment(1)
What OS are you running?Fill
O
9

This should work:

map <F11> :exe '!ctags -R -f ./tags ' . shellescape(system('python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"'))<CR>

But if your shell supports it, why not just:

map <F11> :!ctags -R -f ./tags `python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()`<CR>
Ostracod answered 4/7, 2010 at 22:49 Comment(2)
This finally worked for me (your first solution without 'shellescape'): map <S-F11> :exe '!ctags -R -f ./pytags ' . system('python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"')<CR>Hibben
Please add a trailing double quote in your second solution.Interposition
S
3

I know the answer given above works, but I'd like to suggest an alternative

map <F11> :!ctags -R -f ./tags $VIRTUAL_ENV/lib/python2.7/site-packages<CR>
Strath answered 10/10, 2012 at 6:16 Comment(2)
This is what I use and it works perfectly if you're using virtualenv, which you should be.Microcurie
Something like this is probably better, so as to work with any version of Python: $VIRTUAL_ENV/lib/python*/site-packagesBriones
B
0

I've encountered some problems while used command like this (taken from this article):

ctags -R --fields=+l --languages=python --python-kinds=-iv -f ./tags $(python -c "import os, sys; print(' '.join('{}'.format(d) for d in sys.path if os.path.isdir(d)))")

in the activated virtualenv with python 3.6 my system decided to use system-default python 2.7 when used command above.

So I want to show you my solution:

python -c \"import os, sys; print(' '.join('{}'.format(d) for d in sys.path if os.path.isdir(d)) + ' ./')\" | xargs /usr/bin/ctags -R

Brame answered 28/9, 2017 at 10:29 Comment(1)
thanks for this! I made a small edit and worked great: python -c "import os, sys; print(' '.join(d for d in sys.path if os.path.isdir(d)) + ' ./')" | xargs /usr/bin/ctags -RGusher

© 2022 - 2024 — McMap. All rights reserved.