Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Vim中使用CTAG导航Python模块?_Python_Vim_Ide_Ctags_Exuberant Ctags - Fatal编程技术网

在Vim中使用CTAG导航Python模块?

在Vim中使用CTAG导航Python模块?,python,vim,ide,ctags,exuberant-ctags,Python,Vim,Ide,Ctags,Exuberant Ctags,我将Vim与ctagsforpython一起使用,这对类、字段等非常有效,但它似乎没有包括Python文件名,也就是模块名。这可能吗?我更喜欢键入ta跳转到一个模块,而不是使用NERDtree这样的文件浏览器逐级导航,我非常习惯在Java中这样做,因为类名就是文件名。如果您使用丰富的CTAG生成标记文件(还有其他方法吗?)然后尝试添加--extra=+f选项。有关详细信息,请参见的手册页。丰富的标记(带有--extra=+f)为python文件名(例如my_module.py)生成标记,但不为模

我将Vim与ctagsforpython一起使用,这对类、字段等非常有效,但它似乎没有包括Python文件名,也就是模块名。这可能吗?我更喜欢键入
ta
跳转到一个模块,而不是使用NERDtree这样的文件浏览器逐级导航,我非常习惯在Java中这样做,因为类名就是文件名。

如果您使用丰富的CTAG生成标记文件(还有其他方法吗?)然后尝试添加
--extra=+f
选项。有关详细信息,请参见的手册页。

丰富的标记(带有
--extra=+f
)为python文件名(例如
my_module.py
)生成标记,但不为模块名生成标记(例如
my_module
)。我最终创建了脚本的修改版本。将以下内容保存到路径中名为
ptags
的文件中,并使其可执行:

#! /usr/bin/env python

# ptags
#
# Create a tags file for Python programs, usable with vi.
# Tagged are:
# - functions (even inside other defs or classes)
# - classes
# - filenames
# Warns about files it cannot open.
# No warnings about duplicate tags.

import sys, re, os
import argparse

tags = []    # Modified global variable!

def main():
    for root, folders, files in os.walk(args.folder_to_index):
        for filename in files:
            if filename.endswith('.py'):
                full_path = os.path.join(root, filename)
                treat_file(full_path)
        if not args.recursive:
            break

    if tags:
        fp = open(args.ctags_filename, 'w')
        tags.sort()
        for s in tags: fp.write(s)

expr = '^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]'
matcher = re.compile(expr)

def treat_file(filename):
    try:
        fp = open(filename, 'r')
    except:
        sys.stderr.write('Cannot open %s\n' % filename)
        return
    base = os.path.basename(filename)
    if base[-3:] == '.py':
        base = base[:-3]
    s = base + '\t' + filename + '\t' + '1\n'
    tags.append(s)
    while 1:
        line = fp.readline()
        if not line:
            break
        m = matcher.match(line)
        if m:
            content = m.group(0)
            name = m.group(2)
            s = name + '\t' + filename + '\t/^' + content + '/\n'
            tags.append(s)

if __name__ == '__main__':
    p = argparse.ArgumentParser()
    p.add_argument('-f', '--ctags-filename', type=str, default='tags')
    p.add_argument('-R', '--recursive', action='store_true')
    p.add_argument('folder_to_index', type=str, default='.')
    args = p.parse_args()
    main()
现在,通过递归处理当前目录来运行以下操作以生成标记文件:


ptags-R-f tags\u file\u to\u create/path/to/index

不幸的是,ubuntu 12.04没有这个选项(或者非常有用的-R选项)。我使用一个脚本收集文件,运行CTAG,为每个文件名添加一行标记,然后对其进行排序。90年代的ubuntu和ctags到底是怎么回事?