How to Generate GitHub Code URLs with Line Highlighting and Range Selection

Appending #L<number> or #L<start>-L<end> to any GitHub file URL highlights specific lines or ranges, enabling precise code references for code reviews and documentation.

The tiimgreen/github-cheat-sheet repository documents this essential feature for sharing exact code locations. You can generate GitHub code URLs with line highlighting and range selection by manipulating the URL fragment identifier, which the GitHub UI processes client-side to scroll and shade the specified lines. This technique works across all languages and file types displayed on GitHub, including diffs, README files, and rendered Markdown.

URL Fragment Syntax for Line Highlighting

GitHub renders source files with each line assigned a unique HTML id. The base URL structure follows this pattern:

https://github.com/<user>/<repo>/blob/<branch>/path/to/file.ext

To highlight specific lines, append a fragment identifier starting with #L followed by the line number. The fragment is not part of the file path; GitHub's frontend processes it entirely on the client side to scroll the viewport and apply highlighting.

Single Line Selection

Add #L<number> to the end of any file URL to highlight one specific line. For example, referencing line 52 in the cheat sheet's README:

https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md#L52

When a user clicks this link, the browser scrolls to line 52 and GitHub shades that entire line.

Range Selection

To highlight a continuous block of code, use the #L<start>-L<end> syntax. This highlights every line from the start number through the end number inclusive.

For example, lines 52 through 57 in README.md (where the "Line Highlighting in Repositories" section is documented):

https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md#L52-L57

This range selection is invaluable for pointing to logic blocks without copying code snippets into comments.

Practical Examples from the Source

The following examples demonstrate real-world usage patterns derived from the repository analysis:

Goal URL Construction Result
Highlight a single line https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md#L52 The file scrolls to line 52 and the line is shaded.
Highlight a range (lines 53-60) https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md#L53-L60 Lines 53 through 60 are all highlighted.
Reference external repository code https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L60 Shows the highlighted range inside Rails' source file.
Link from Markdown [see the validation logic](https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md#L53-L60) Clicking takes the reader directly to the highlighted block.

These fragments work consistently whether the target is a source code file, a diff view, or a rendered Markdown document like README.zh-cn.md or README.ko.md (which mirror the same documentation for non-English readers).

Generating These URLs Programmatically

You can construct these URLs dynamically in scripts by concatenating the repository URL with the fragment. Here is a Python example that generates a line-range URL:

def generate_github_line_url(repo_url, branch, file_path, start_line, end_line=None):
    base = f"https://github.com/{repo_url}/blob/{branch}/{file_path}"
    if end_line:
        return f"{base}#L{start_line}-L{end_line}"
    return f"{base}#L{start_line}"

# Example usage targeting the cheat sheet README

url = generate_github_line_url(
    "tiimgreen/github-cheat-sheet", 
    "master", 
    "README.md", 
    52, 
    57
)
print(url)

# Output: https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md#L52-L57

Summary

  • Fragment syntax: Append #L<number> for single lines or #L<start>-L<end> for ranges to any GitHub file URL.
  • Client-side processing: The highlighting occurs in the browser; the fragment is not part of the server-side file path.
  • Universal compatibility: Works across all languages, diffs, README files, and rendered Markdown in the tiimgreen/github-cheat-sheet repository and all other GitHub repos.
  • Use cases: Essential for precise code reviews, issue discussions, and documentation that references specific logic blocks without copy-pasting code.

Frequently Asked Questions

How do I highlight a single line in a GitHub file?

Append #L followed by the line number to the end of the file URL. For example, https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md#L52 highlights line 52 in the README. The browser automatically scrolls to that line and GitHub applies a visual shade to highlight it.

Can I highlight multiple lines at once on GitHub?

Yes. Use the range syntax #L<start>-L<end> to highlight a continuous block. For instance, #L53-L60 highlights lines 53 through 60 inclusive. This is documented in the README.md file at lines 52-57 of the github-cheat-sheet repository.

Does line highlighting work on mobile GitHub?

The URL fragments function on mobile browsers, causing the page to scroll to the specified line. However, the visual highlighting effect may vary depending on the mobile interface version, though the navigation to the specific line number remains consistent across devices.

Are line number fragments part of the file path?

No. The #L fragment is processed entirely on the client side by the GitHub UI and is not sent to the server as part of the file request. This means any valid line number within the displayed file can be referenced, and the highlighted view can be shared directly without affecting the underlying file content retrieval.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →