Skip to main content

Core Commands & Interactive Mode

File Operations Mastery

0:00
LearnStep 1/3

File Operations Mastery

Introduction to File Operations

Mastering file operations is crucial for navigating and modifying codebases effectively with the Gemini CLI. This lesson covers searching, reading with pagination, and precise editing techniques.

Searching with glob

The glob tool is your primary way to find files based on patterns. It's much faster than manually listing directories when you know what you're looking for.

  • Use **/*.ts to find all TypeScript files recursively.
  • Use src/components/*.tsx to find React components in a specific folder.
  • The tool returns absolute paths, which are required by other file tools.

Reading Large Files with read_file

Reading huge files all at once can exceed token limits or become overwhelming. The read_file tool supports offset and limit for pagination.

Best Practice: Always check the file size or read in chunks if you suspect a file is large. The tool will notify you if the output is truncated.

Precise Editing: write_file vs replace

There are two ways to modify files:

  • write_file: Best for creating new files or completely rewriting small configuration files.
  • replace: Essential for surgical edits in existing code. It requires an old_string and a new_string.

The Importance of Context in replace

To ensure the correct code is updated, replace requires significant context (at least 3 lines before and after). This prevents accidental matches in other parts of the file.