Skip to content

Instantly share code, notes, and snippets.

@unhammer
Last active February 12, 2023 05:40
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save unhammer/c1ac7320141f09ac38e0 to your computer and use it in GitHub Desktop.
Save unhammer/c1ac7320141f09ac38e0 to your computer and use it in GitHub Desktop.
Create .merlin file for a project with all your ocamlfind packages and .opam sources in there
#!/bin/sh
if test -f .merlin; then
echo ".merlin already exists, bailing out ..." >&2
exit 1
else
# You could add your default EXT's and such to this list:
cat >.merlin <<EOF
S .
B _build
EOF
# Add PKG's:
ocamlfind list \
| awk '{ print "PKG "$1 }' >>.merlin
# See https://github.com/the-lambda-church/merlin/wiki/Letting-merlin-locate-go-to-stuff-in-.opam
find ~/.opam -name '*.cmt' -print0 \
| xargs -0 -I{} dirname '{}' \
| sort -u \
| awk '{ print "S "$0"\nB "$0 }' >> .merlin
fi
@copy
Copy link

copy commented Apr 3, 2016

Very useful – thanks!

Loading all packages is a bit of an overkill and makes my editor take very long to load. I'm using it as follows:

merlin-init.sh | grep endian >> .merlin

And changed the script to print to stdout:

#!/bin/sh

# Add PKG's:
ocamlfind list \
    | awk '{ print "PKG "$1 }'

# See https://github.com/the-lambda-church/merlin/wiki/Letting-merlin-locate-go-to-stuff-in-.opam
find ~/.opam -name '*.cmt' -print0 \
    | xargs -0 -I{} dirname '{}' \
    | sort -u \
    | awk '{ print "S "$0"\nB "$0 }'

@omeid
Copy link

omeid commented Apr 15, 2019

Super useful. Thanks to both of you!

Here is a slightly updated version that works with opam switches.

#!/bin/sh

eval $(opam config env)

# Add PKG's:
ocamlfind list \
    | awk '{ print "PKG "$1 }'

# See https://github.com/the-lambda-church/merlin/wiki/Letting-merlin-locate-go-to-stuff-in-.opam
find "$OPAM_SWITCH_PREFIX" -name '*.cmt' -print0 \
    | xargs -0 -I{} dirname '{}' \
    | sort -u \
    | awk '{ print "S "$0"\nB "$0 }'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment