Capturing the Current File Location

Marcin Borkowsi has a nice post on how to capture the current file’s location and copy it to the system clipboard. It’s just what you need for sharing information about data in a file with a colleague. By file location he means the path to the file and the line number within that file.

That doesn’t seem too hard but there are some wrinkles. First of all you want the absolute line number within the file not just the number of the line visible in the buffer. That’s pretty easy but the second problem is a bit trickier.

Sometimes, just the name of the file is not enough. It may be that two files within a project have the same name but lives under a different subdirectory. Your first thought is probably to capture the full path to the file but that isn’t optimal either. As Borkowski says, there’s no point in showing the full path on his machine. What’s needed is the path relative to the current project.

That means the path relative to the current VC repository. It’s pretty easy to get that with the vc-root-dir function. Borkowski has some code that captures the file path and line number and copies it to the system clipboard. It’s simple and can be copied and used as is or modified to meet your particular needs.

The post is definitely worth a read if only to discover how to deal with the various issues.

UPDATE [2022-06-26 Sun 14:40]: Added missing link to Borkowski’s post.

This entry was posted in General and tagged . Bookmark the permalink.