# Ctags for zig

**URL:** <https://ziggit.dev/t/ctags-for-zig/8264>\
**Category:** Help\
**Tags:** tools\
**Created:** [January 30, 2025, 7:01pm UTC](https://ziggit.dev/t/ctags-for-zig/8264 "2025-01-30T19:01:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![earthfail](https://ziggit.dev/user_avatar/ziggit.dev/earthfail/32/3292_2.png) [@earthfail](https://ziggit.dev/u/earthfail)\
**Post date:** [January 30, 2025, 7:01pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/1 "2025-01-30T19:01:53Z")

</div>

Is there a tool to generate tag file that includes the definitions and references for functions/methods/procedures and global variables?  
I know of ZLS but I have my setup for c and would like to keep using my etags.

If not, I would like to make one but at the moment I have a hard time using zig AstGen so if someone has an example of a project parsing zig files it would be appreciated

---

<div class="post-metadata">

**Author:** ![natecraddock](https://ziggit.dev/user_avatar/ziggit.dev/natecraddock/32/3012_2.png) [@natecraddock](https://ziggit.dev/u/natecraddock)\
**Post date:** [January 30, 2025, 7:22pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/2 "2025-01-30T19:22:08Z")

</div>

I haven’t used it, but I am aware of this project [gpanders/ztags: Generate tags files for Zig projects. - Codeberg.org](https://codeberg.org/gpanders/ztags)

---

<div class="post-metadata">

**Author:** ![earthfail](https://ziggit.dev/user_avatar/ziggit.dev/earthfail/32/3292_2.png) [@earthfail](https://ziggit.dev/u/earthfail)\
**Post date:** [January 30, 2025, 8:09pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/3 "2025-01-30T20:09:49Z")

</div>

Thanks, I looked into it a while ago and couldn’t make it to work. I will try again. Thank you again.

---

<div class="post-metadata">

**Author:** ![msw](https://ziggit.dev/user_avatar/ziggit.dev/msw/32/2986_2.png) [@msw](https://ziggit.dev/u/msw)\
**Post date:** [January 30, 2025, 9:05pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/4 "2025-01-30T21:05:45Z")

</div>

Wow. I was curious, and built it successfully, with two different patches to bring it up-to-date WRT zig 0.14.0, and try and deal with the tabs. But really, zig doesn’t allow tab characters inside of string literals? How does one deal with this? The ‘naive’ way (replacing literal tabs by \t) isn’t the way. And yeah there’s tools out there we might wanna generate TSV for, surely we want to allow tabs in strings?

```zig
First difference occurs on line 3:
expected:
MyEnum\ta.zig\t/^const MyEnum = enum {$/;"\tenum
      ^ ('\x5c')
found:
MyEnum	a.zig	/^const MyEnum = enum {$/;"	enum
      ^ ('\x09')

```

[ztags.patch.txt](https://ziggit.dev/uploads/short-url/nyBwTgo4jrskrLdCQ5zCKAihHoX.txt) (5.4 KB)

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [January 30, 2025, 9:16pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/5 "2025-01-30T21:16:10Z")

</div>

Even worse it doesn’t allow tabs in comments, so anyone who uses tabs can’t comment out code.  
Here is the relevant issue defining the specificiation: [grammar clarifications regarding tabs and carriage returns · Issue #38 · ziglang/zig-spec · GitHub](https://github.com/ziglang/zig-spec/issues/38)

---

<div class="post-metadata">

**Author:** ![msw](https://ziggit.dev/user_avatar/ziggit.dev/msw/32/2986_2.png) [@msw](https://ziggit.dev/u/msw)\
**Post date:** [January 30, 2025, 9:20pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/6 "2025-01-30T21:20:12Z")

</div>

~~`std.testing.expectEqualStrings` probably needs to learn how to deal with \t vs. at the very, very least. Also other supported escape sequences - it just does a `std.mem.indexOfDiff` which doesn’t handle escape sequences.~~

**EDIT/Addition** : the above is/was factually wrong. The error cause is/was multi-line strings not supporting escape sequence, as per design and documentation, which slipped me. `expectEqualStrings` is fine as-is.

---

<div class="post-metadata">

**Author:** ![earthfail](https://ziggit.dev/user_avatar/ziggit.dev/earthfail/32/3292_2.png) [@earthfail](https://ziggit.dev/u/earthfail)\
**Post date:** [January 30, 2025, 9:29pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/7 "2025-01-30T21:29:05Z")

</div>

I just used zig 0.13.0 and it compiled successfully but emacs complains that the tag file is formatted poorly. I have no knowledge in tag file format so I don’t know if it is a problem in my emacs setup

---

<div class="post-metadata">

**Author:** ![msw](https://ziggit.dev/user_avatar/ziggit.dev/msw/32/2986_2.png) [@msw](https://ziggit.dev/u/msw)\
**Post date:** [January 30, 2025, 9:31pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/8 "2025-01-30T21:31:15Z")

</div>

if you compiled successfully, I asume you ran zig fmt on it implicitly and zig fmt has replaced the tabs by spaces and that’s not a valid tags file. Or emacs did. Tags are TSV.

---

<div class="post-metadata">

**Author:** ![earthfail](https://ziggit.dev/user_avatar/ziggit.dev/earthfail/32/3292_2.png) [@earthfail](https://ziggit.dev/u/earthfail)\
**Post date:** [January 30, 2025, 9:47pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/9 "2025-01-30T21:47:20Z")

</div>

it is true that tags file generated was not a tsv file but I just removed the first two lines:

```zig
!_TAG_FILE_SORTED	1	/1 = sorted/
!_TAG_FILE_ENCODING	utf-8

```

and I managed to read it with python pandas

```python
import pandas as pd
print(pd.read_csv("new_tags",sep="\t"))

```

but emacs still complains. I will just use the good old search regex and thankfully andrew made function definition saner than c

---

<div class="post-metadata">

**Author:** ![mg979](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mg979](https://ziggit.dev/u/mg979)\
**Post date:** [July 27, 2025, 2:56pm UTC](https://ziggit.dev/t/ctags-for-zig/8264/10 "2025-07-27T14:56:54Z")

</div>

If you don’t mind something simpler, you can put this in `~/.config/ctags/zig.ctags`:

```zig
--langdef=zig
--map-zig=.zig

--kinddef-zig=f,function,functions
--kinddef-zig=m,method,methods
--kinddef-zig=t,type,types
--kinddef-zig=v,field,fields

--regex-zig=/^(export +)?(pub +)?(inline +)?fn +([a-zA-Z0-9_]+)/\4/f/{exclusive}
--regex-zig=/^(export +)?(pub +)?[\t]*const +([a-zA-Z0-9_]+) = (struct|enum|union)/\3/t/{exclusive}{scope=push}
--regex-zig=/^}///{exclusive}{scope=pop}{placeholder}
--regex-zig=/^[\t]+(pub +)?(inline +)?fn +([a-zA-Z0-9_]+)/\3/m/{exclusive}{scope=ref}
--regex-zig=/^(export +)?pub +const +([a-zA-Z0-9_]+)(:.*)? = .*/\2/v/{exclusive}

```
