Gemini CLI 演示:快速上手与运行
摘要
本课程介绍如何安装、配置和开始使用 Gemini CLI。你将学习通过 npm 安装、Google 账户认证、探索基本命令(如 /help、/settings、/theme),以及如何使用文件上下文和网络搜索来增强 AI 交互体验。课程还介绍了工作流管理技巧,包括清除会话历史和查看使用统计。
要点
- Gemini CLI 通过 npm 全局安装,支持 Google 账户、API 密钥和 Vertex AI 三种认证方式,课程建议使用 Google 账户以获得免费额度。
- 提供智能模型选择(auto 模式),根据任务复杂度自动在 gemini-3-flash(简单任务)和 gemini-3-pro(复杂任务)之间切换。
- 支持文件引用(使用 @ 符号)、网络搜索集成和文件写入工具,所有操作默认需要用户确认,确保可控性。
- 提供 /clear、/stats、/docs 等实用命令来管理会话状态、查看使用统计和访问文档。
视频信息:A Tour of Gemini CLI (Let’s get Gemini CLI up and running)
中文翻译
让我们把 Gemini CLI 运行起来吧。在本节课中,你将学习如何安装 Gemini CLI,探索其命令并尝试你的第一个工作流。好的,让我们开始吧。
在整个课程中,我们将扮演一位科技会议组织者的角色。我们面临着大量的任务,例如搭建网站、进行调研和策划、管理社交媒体,以及在会议结束后,我们还需要检查所有数据,看看是否有可以跟进的潜在联系人。所有这些工作都可以通过 Gemini CLI 获得极大的助力。
我们要做的第一件事实际上是进行调研,查看去年组织者留下的笔记,并通过网络搜索和查找实际策划组织会议的最佳实践来充实这些内容。
要安装 Gemini CLI,你需要先安装 Node,因为 Gemini CLI 是作为一个 NPM 包发布的。关于本课程中涉及的所有先决条件、安装说明和提示词,请务必查看课程末尾的阅读材料。安装 Gemini CLI 的命令是 npm install -g @google/gemini-cli。这将把该包作为全局包安装,以便你在机器上的所有项目中都能使用它。
好了,现在我们已经安装了 Gemini CLI,有趣的旅程开始了。启动 Gemini CLI 的命令只需输入 gemini。我们在会议的文件夹或项目中启动 Gemini CLI。这里存放着我们所有的资料以及网站的第一个原型。我们在 Ghostty 中使用 Gemini CLI,但你也可以在你选择的任何终端中使用它。
你应该会在屏幕上看到类似的内容,要求你进行身份验证。有三种不同的验证方法。一种是"使用 Google 登录(Login with Google)",你只需使用个人账户登录即可,这是你在本课程中应该选择的方式,因为它可以让你访问免费层级。如果你想在课程结束后继续使用,你可以使用 API 密钥或 Vertex AI 来获取更多请求额度。要启动验证流程,只需在"Login with Google"上按回车键。这将在你的本地浏览器中打开一个 OAuth 流程,你可以用它来完成 Gemini CLI 的验证。验证通过后,你应该返回 Gemini CLI,你会看到验证已成功的提示。
这就是 Gemini CLI 首次启动时的样子。你会看到屏幕上有一些提示,以及这个漂亮的 Gemini 横幅。我们使用的是最新的 Gemini 3 模型。根据你运行它的时间,你可能拥有更新的模型。如果你愿意,你可以通过输入 /model 来更改模型。默认设置是使用"自动(auto)“模式,这实际上让 Gemini CLI 能够智能地决定哪个模型最适合当前的请求。因此,如果你问一些简单的问题,比如"嗨”,它实际上会使用较便宜的 gemini-3-flash 模型之一。而如果你有一个复杂的任务,比如"重构我的整个代码库",它就会去选择 gemini-3-pro,这是能力最强的模型。
开始使用 Gemini CLI 的最佳方式是运行 /help 命令。这将列出 Gemini CLI 自带的所有可用内置命令,以及一些有趣的键盘快捷键。另一个很好的入门命令是 /settings。如果你是 Vim 用户,你可以启用 Vim 模式,这样 Gemini CLI 就会遵循 Vim 的操作习惯。我最喜欢的设置是"隐藏页脚(Hide Footer)"。这会让 Gemini CLI 看起来更简洁,我们按一下 Esc 键退出。现在你可以看到底部的那一行不见了,我们拥有了一个干净整洁的用户界面。
其实你还可以自定义配色方案。这是我推荐尝试的另一个功能,通过输入 /theme 来实现。你可以自定义并创建属于你自己的主题。当我想冒险一点的时候,我喜欢使用紫色的色调。不过你知道吗?今天我不想太冒险,所以让我们坚持使用默认主题。
让我们开始吧,向 Gemini CLI 发送我们的第一个提示词。去年 2025 年会议的组织者给我们留下了一些关于如何举办和组织会议的建议。这些内容保存在一个名为 suggestions.md 的 Markdown 文件中。我们可以使用 @ 符号来实际引入并引用该文件。这样 Gemini CLI 就会去读取它并获取上下文。如果你只是说"读取建议文件",它也能搞定,但它需要先花点时间处理和思考一下。
所以让我们执行这个操作,你会看到 Gemini CLI 实际上在旋转思考,它有时还会讲一些俏皮的笑话,并在推理过程中输出信息。Gemini CLI 已经回复了。我们可以看到去年组织者的建议,比如关于网站的建议,确保日程安排和会议目录易于查找,以及确保我们在社交媒体上有一个独特的标签。我们会考虑这些建议,它们稍后会派上用场。
虽然我们有去年组织者的建议,但这些可能已经过时了。我可以提示 Gemini CLI 看看这些建议是否符合网上的最佳实践,它应该会启动 Google 网络搜索,从网上提取所有信息,这样我们就能确保掌握最准确、最新的信息。之前的组织者与行业总体情况非常吻合,不过网上的指南提供了更结构化的时间表。因此,在推进会议的过程中,我们可能需要通过 Gemini CLI 将这些新发现添加到我们的文档中,以便以后参考。
我们将看到 Gemini CLI 现在实际上正在调用 WriteFile 工具。Gemini CLI 默认会进行确认,以便让你这个用户知情并参与其中。当你收到建议时,你可以使用 Ctrl-S 展开它并查看完整的差异(diff)。你也可以选择是否只允许该工具调用一次,或者是否想始终允许某个特定工具,甚至选择在外部编辑器(如 VS Code)中打开差异。如果你想换个方向,你总是可以拒绝这些建议。我们将允许这个建议,并让它写入文件。
现在,我们的 suggestions.md 文件已经更新了,其中包含了去年组织者的建议以及我们在网上找到的最佳实践。
当你从一个任务转移到另一个任务时,最好清除 Gemini CLI 的状态,以便重新开始。你可以使用 /clear 命令来完成此操作。这将从对话历史记录中删除所有的工具调用以及与 Gemini CLI 的往来对话,让你能够在一个清爽的环境中开始新任务,而不会被杂乱的信息干扰。
要查看当前会话中的所有统计数据,你可以直接运行 /stats 命令。这将显示模型所做的所有不同代码更改、使用的工具、所有的请求以及它们被路由到了哪些模型。因为我们使用的是自动(auto)模式,你可以看到我们实际上将请求分散到了不同的模型中。
如果你想了解更多信息或在使用 Gemini CLI 时遇到任何问题,你可以通过运行 /docs 直接在 Gemini CLI 内部打开文档。所以,如果你想了解更多关于 Gemini CLI 特定功能的细节,这非常方便。
要退出或离开 Gemini CLI,只需输入 /exit 或 /quit。你还会得到一个会话历史的快速快照。至此,我们已经涵盖了基础知识。我们向你展示了 Gemini CLI 的样子以及一些常用命令。我们还提到了上下文。我们向你展示了如何保持整洁并确保 Gemini CLI 高效运行。在下一节课中,我们将更详细地介绍 Gemini CLI 中的上下文和记忆。
English Script
Let’s get Gemini CLI up and running. In this lesson, you’ll learn how to install Gemini CLI, explore its commands and try your first workflow. All right, let’s get into it.
Throughout this course, we’re going to take on the persona of an organizer for a tech conference. There’s a ton of things we are tasked with, such as building out the website, doing research and planning, social media, as well as after the conference, we’re going to want to go through all the data and potentially see if we have any leads that we can reach out to. All of this can be super powered with Gemini CLI.
The first thing we want to do is actually do research, look at the notes from the past year’s organizers, amplify that with searches from the web and best practices for actually planning and organizing a conference.
To install Gemini CLI, you’re going to need Node, as Gemini CLI is deployed as an NPM package. For all the prerequisites, installation instructions, and prompts that we go over through this course, make sure to check the reading items at the end of the course. The command to install Gemini CLI is npm install -g @google/gemini-cli. This will install the package as a global package so that you can use it across all of your projects on your machine.
All right, now we actually have Gemini CLI installed, the fun can begin. The command to start Gemini CLI is just gemini. We’re starting Gemini CLI in the folder or project for our conference. This is where we have all of our materials as well as the first prototype of our website. We’re using Gemini CLI in Ghostty, but you can use it in any terminal of your choice.
You should see something like this on screen where it’s asking you to authenticate. There are three different authentication methods. There is Login with Google where you just use your personal account, and this is what you’ll want to do for the course, as this gives you access to the free tier. If you want to continue on after, past that, you can use an API key or Vertex AI to get more requests. So to kick off the authentication flow, just select enter on Login with Google. This will open up an OAuth flow in your local browser that you can go ahead and use to authenticate to Gemini CLI. Once authenticated, you should go back to Gemini CLI and see that the authentication has succeeded.
So here’s what Gemini CLI looks like when you first start it up. You’ll see there’s some tips on screen as well as this nice Gemini banner. We’re using the latest Gemini 3 models. Depending when you are running this, you might have a more up-to-date model. You can go ahead and actually change the models if you prefer by doing /model. The default is to use auto, which actually lets Gemini CLI intelligently decide which model is best for any given request. So if you ask something simple, like “hi”, it will actually use one of the cheaper gemini-3-flash models. Whereas if you have something complex like “refactor my entire code base”, it’s going to go ahead and choose gemini-3-pro, the most capable model.
The best place to look to get started with Gemini CLI is to run the /help command. This will go ahead and list all the available built-in commands that Gemini CLI comes with, as well as some fun keyboard shortcuts. Another good command to get started with is /settings. If you’re a vim user, you can actually enable vim mode so Gemini CLI respects Vim. My favorite setting is the Hide Footer setting. This will actually go ahead and make Gemini CLI look a lot cleaner and we’ll go ahead and do escape. And now you can see we no longer have that bar underneath, so we actually have a nice clean UI.
You can actually customize the color scheme. This is another one I recommend doing by doing /theme. And you can actually customize and create your own theme. When I’m feeling adventurous, I like to use shades of purple. But you know what? Not feeling super adventurous today, so let’s stick with default.
Let’s get started and send our first prompt to Gemini CLI. So last year the organizers of the 2025 conference left us some suggestions for how to host and organize the conference. We have this in a markdown file, suggestions.md. We can go ahead and use the at symbol to actually pull in and reference that file. So Gemini CLI will go and read it and have access to the context. If you just said “read the suggestions file”, it will also figure it out, but it will have to take some time to process and think about it first.
So let’s go ahead and execute that, and you’ll see Gemini CLI actually will spin and think, and it will actually sometimes have some witty jokes as well as output messages as it reasons through. Gemini CLI has responded. We can see the recommendations from last year’s organizers, such as the website, making sure the schedule and session catalog are easy to find, as well as making sure we have a unique hashtag for socials. We’ll take these into consideration and they’ll be handy later.
So we have the recommendations from last year’s organizers, but maybe these are out of date. I can prompt Gemini CLI to see if this matches the online best practices, and it should go ahead and actually kick off the Google web search, pull all the information from the web, so we can make sure that we are having the most accurate and up-to-date information. The previous organizers align very well with the general industry, though the online guides offer a more structured timeline. So there may be things we want to incorporate as we go forward with our conference.
So we’ll prompt Gemini CLI to add the new findings to our doc so that we can reference it again later. We’ll see that Gemini CLI is actually now calling the WriteFile tool. Gemini CLI will actually by default have a confirmation so that you the user are in the know and in the flow. When you get a suggestion, you can use ctrl-s to expand it and see the full diff. You can also choose whether you want to allow the tool call just once, If you want to always allow the certain tool, or if you want to even open the diff in an external editor, such as VS Code. You can always reject the suggestions if you want to go in a different direction. We’ll go ahead and allow the suggestion and have it write to the file.
Now our suggestions.md is up to date with both the recommendations from last year’s organizers and best practices that we found online.
When you move from task to task, it’s best to actually wipe Gemini CLI so that you can start fresh. You can do this with the /clear command. That will remove all of the tool calls as well as back and forth with Gemini CLI from the conversation history, allowing you to start on a new task fresh and not have it cluttered.
To see all your stats in a given session, you can just run the /stats command. This will show all of the different code changes the model has made, the tool as well as all the requests and which models they’re getting routed to. Because we are using the auto model, you can see that we’re actually spreading out our requests between different models.
If you want to learn more or running into any issues with Gemini CLI, you can actually open the documentation directly from within Gemini CLI by running /docs. So if you want to learn more or get more details on a specific feature of Gemini CLI, this is super handy.
To exit or get out of Gemini CLI, you just type /exit or /quit. You’ll also get a quick snapshot of the session history. So now we’ve covered the basics. We’ve shown you Gemini CLI, what it looks like, and some popular commands. We also mentioned context. We showed you how to keep things clean and make sure Gemini CLI is being efficient. We’re going to go into more detail in the next lesson and cover context and memory within Gemini CLI.