大家好,我是Ai学习的老章
经常在外冲浪,看到很多优秀的技术文章,独享不如分享
我会使用DeepSeek-R1+科技类翻译大师prompt翻译出来,大家一起学习
如有翻译偏差,请大家指教,原文:# Google Gemini CLI 快速参考[1]
Gemini CLI 是一个开源的 AI 代理,将 Gemini 的强大功能直接带入您的终端,能够使用自然语言执行广泛的任务,如编程、问题解决和任务管理。
Gemini-CLI的正确用法,看视频,听播客 ,看论文,做PPT,做总结,整理文件......
本速查表提供了安装、配置和使用 Gemini CLI 的快速参考,重点是通过 Gemini API 密钥进行身份验证的用户。
Gemini CLI Architecture
全局安装:
npm install -g @google/gemini-cli
无需安装运行:
npx @google/gemini-cli
Authenticate with an API key before first use. See the authentication guide[2] for details.
获取您的密钥: 从 Google AI Studio[3] 获取 API 密钥。
Set Your Key: Make the key available to the CLI with one of these methods.
Method 1: Shell Environment Variable Set the GEMINI_API_KEY
environment variable. To use it across terminal sessions, add this line to your shell's profile (e.g., ~/.bashrc
, ~/.zshrc
).
export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
方法 2:环境文件 在 ~/.gemini/
创建一个 .env
文件以供全局使用,或在特定项目中创建一个 ./.gemini/
文件。CLI 会自动加载它。
# In .gemini/.env
GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
交互模式(REPL): 启动一个会话。
gemini
非交互模式: 输入提示,获取单个响应。
gemini -p "Summarize the main points of the attached file. @./summary.txt"
通过管道传递给 CLI: 将内容通过管道传递给 CLI。
echo "Count to 10" | gemini
沙盒模式: 在安全的沙盒中运行工具(需要 Docker 或 Podman)。
gemini --sandbox -p "your prompt"
其他标志:
-m, --model <model>
: 使用特定模型。-d, --debug
:启用调试输出。--yolo
:自动批准所有工具调用。--checkpointing
: 在文件修改前保存项目快照。使用 /restore
恢复更改。完整标志列表[4] 。
settings.json
)通过创建 settings.json
文件来自定义 CLI。设置的优先级如下:
.gemini/settings.json
(覆盖用户和系统设置)。~/.gemini/settings.json
(覆盖系统设置)。/etc/gemini-cli/settings.json
(适用于所有用户)。示例 settings.json
:
{ "theme": "GitHub", "autoAccept": false, "sandbox": "docker", "checkpointing": { "enabled": true }, "fileFiltering": { "respectGitIgnore": true }, "usageStatisticsEnabled": true}
autoAccept
: 自动批准安全的、只读的工具调用。sandbox
: 隔离工具执行(例如,true
,"docker"
,或 "podman"
)。checkpointing
: 启用 /restore
命令以撤销文件更改。usageStatisticsEnabled
: 设置为 false
可以禁用使用统计。所有详细信息请参阅 配置指南[5] 。
GEMINI.md
)使用 GEMINI.md
文件向模型提供指令并根据项目需求对其进行定制。
层级加载: CLI 从多个位置组合 GEMINI.md
文件。更具体的文件覆盖通用文件。加载顺序为:
~/.gemini/GEMINI.md
(适用于所有项目的说明)。GEMINI.md
文件。GEMINI.md
文件,从而允许针对特定组件的指令。使用 /memory show
查看最终发送给模型的综合上下文。
使用导入模块化上下文: 您可以使用 @file.md
语法导入其他 Markdown 文件来组织 GEMINI.md
文件。仅支持 .md
文件。
使用导入的 GEMINI.md
示例:
# Main Project Context: My Awesome App ## General Instructions- All Python code must be PEP 8 compliant.- Use 2-space indentation for all new files. ## Component-Specific Style Guides@./src/frontend/react-style-guide.md@./src/backend/fastapi-style-guide.md
更多内容请参见完整上下文文件指南[6] 。
list_directory(path="/src")
,glob(pattern="src/**/*.ts")
, read_file(path="/path/to/file.txt")
, write_file(file_path="/path/to/new_file.js", content="console.log('hello');")
, replace(file_path="...", old_string="...", new_string="...")
, search_file_content(pattern="myFunction", include="*.js")
settings.json
中使用 excludeTools
。例如: "excludeTools": ["run_shell_command(rm)"]
google_web_search(query="Gemini API rate limits")
, web_fetch(prompt="Summarize https://my-blog.com/article")
save_memory(fact="My preferred CSS framework is Tailwind CSS.")
通过运行 Model Context Protocol (MCP) 服务器并在任何 settings.json
中配置它们,可以扩展 CLI。
mcpServers
配置示例:
"mcpServers": { "myPythonServer": { "command": "python", "args": ["-m", "my_mcp_server", "--port", "8080"], "cwd": "./mcp_tools/python", "env": { "DATABASE_URL": "$DB_URL_FROM_ENV" }, "timeout": 15000, "trust": false, "includeTools": ["safe_tool_1", "safe_tool_2"], "excludeTools": ["dangerous_tool"] }}
传输(选择一个):
command
, args
, cwd
: 通过 Stdio 启动本地进程url
: SSE 端点(例如,"http://localhost:8080/sse"
)httpUrl
: HTTP 流式传输端点(例如,"http://localhost:8080/mcp"
)可选:
env
: 环境变量。使用 $VAR_NAME
语法来引用 shell 变量。headers
: url
/httpUrl
传输的 HTTP 头的键值对映射。timeout
: 请求超时时间(毫秒,默认:10 分钟)。trust
: 为该服务器跳过所有工具确认。includeTools
/excludeTools
: 白名单/黑名单特定工具。excludeTools
优先级更高。使用 OAuth 查看 mcp-server.md[7]
/
)命令 | 描述 |
---|---|
/compress | 用总结替换整个聊天上下文以节省令牌。 |
/copy | 将最后一个响应复制到剪贴板。 |
/mcp | 列出配置的 MCP 服务器及其可用工具。 |
/clear | 清空终端屏幕和上下文(也可使用 Ctrl+L)。 |
/tools | 列出可用工具。 |
/extensions | 列出已激活的扩展。 |
/stats | 展示会话令牌的使用情况和节省。 |
/memory show | 显示所有 GEMINI.md 文件的综合上下文。 |
/memory refresh | 重新加载所有 GEMINI.md 文件。 |
/chat save <tag> | 使用标签保存当前对话。 |
/chat resume <tag> | 恢复保存的对话。 |
/restore | 列出或恢复项目状态检查点。 |
/auth | 更改当前的身份验证方法。 |
/bug | 针对 Gemini CLI 提交问题或错误报告。 |
/help | 显示帮助信息和可用命令。 |
/theme | 更改 CLI 的视觉主题。 |
/quit | 退出 Gemini CLI。 |
@
)在提示中引用文件或目录。CLI 默认尊重 .gitignore
。
包含单个文件:
> Explain this code to me. @./src/main.js
包含整个目录(递归):
> Refactor the code in this directory to use async/await. @./src/
!
)在 CLI 中直接运行命令行命令。
运行单个命令:
> !git status
切换 Shell 模式: 输入 !
以切换到持久 Shell 模式。再次输入 !
以退出。
使用 TOML 文件创建自定义命令。将它们存储在 ~/.gemini/commands/
(全局)或 <project>/.gemini/commands/
(项目特定)。更多详情请参阅 自定义命令指南[8] 。
示例: ~/.gemini/commands/test/gen.toml
# Invoked as: /test:gen "Create a test for the login button"description = "Generates a unit test based on a description."prompt = """You are an expert test engineer. Based on the following requirement, please write a comprehensive unit test using the Jest testing framework. Requirement: {{args}}"""
创建扩展以添加功能。将它们放在 <workspace>/.gemini/extensions/
或 ~/.gemini/extensions/
中。每个扩展都是一个包含 gemini-extension.json
文件的目录,可以配置 MCP 服务器、工具和上下文文件。有关更多详细信息,请参阅 扩展指南[9] 。
例如:
<workspace>/.gemini/extensions/my-extension/gemini-extension.json
{ "name": "my-extension", "version": "1.0.0", "mcpServers": { "my-server": { "command": "node my-server.js" } }, "contextFileName": "GEMINI.md", "excludeTools": ["run_shell_command"]}
当启用了 checkpointing
时,CLI 会在工具修改文件之前保存项目快照。
可以在 settings.json
中启用或使用标志:
gemini --checkpointing
恢复到之前的状态:
# List available checkpoints/restore # Restore a specific checkpoint/restore <checkpoint_file_name>
搭建完美的写作环境:工具篇(12 章)图解机器学习 - 中文版(72 张 PNG)ChatGPT 、大模型系列研究报告(50 个 PDF)108页PDF小册子:搭建机器学习开发环境及Python基础 116页PDF小册子:机器学习中的概率论、统计学、线性代数 史上最全!371张速查表,涵盖AI、ChatGPT、Python、R、深度学习、机器学习等
参考资料
[1]
原文:# Google Gemini CLI 快速参考: https://www.philschmid.de/gemini-cli-cheatsheet
[2]
authentication guide: https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/authentication.md
[3]
Google AI Studio: https://aistudio.google.com/app/apikey
[4]
完整标志列表: https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/configuration.md#command-line-arguments
[5]
配置指南: https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/configuration.md
[6]
完整上下文文件指南: https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/configuration.md#context-files-geminimd
[7]
mcp-server.md: https://github.com/google-gemini/gemini-cli/blob/main/docs/tools/mcp-server.md#oauth-support-for-remote-mcp-servers
[8]
自定义命令指南: https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/commands.md#custom-commands
[9]
扩展指南: https://github.com/google-gemini/gemini-cli/blob/main/docs/extension.md