在 CLI 中使用编辑器命令 - Amazon Q 开发者版

在 CLI 中使用编辑器命令

Amazon Q 开发者版 CLI 提供了一个 /editor 命令,可打开您首选的文本编辑器来撰写复杂的提示。这对于多行提示、代码示例或在需要仔细设计问题时特别有用。

基本用法

要打开带有空提示的默认编辑器:

Amazon Q> /editor

要打开带有初始文本的编辑器:

Amazon Q> /editor Write a Python function that calculates Fibonacci numbers

当您使用 /editor 命令时,Amazon Q 会创建一个扩展名为 .md 的临时文件,使用该文件打开指定的编辑器,然后读取内容,并在您保存和关闭编辑器时将其作为提示提交。

设置首选编辑器

Amazon Q 使用系统的 $EDITOR 环境变量来确定要打开哪个编辑器。如果未设置,则它默认为 vi

临时设置(仅限当前会话)

要仅为当前终端会话设置编辑器:

$ export EDITOR=nano

永久设置

要使编辑器首选项在各会话间保持持久,请将 export 命令添加到 Shell 配置文件中:

# For bash (add to ~/.bashrc)
export EDITOR=nano

# For zsh (add to ~/.zshrc)
export EDITOR=nano

# For fish shell (add to ~/.config/fish/config.fish)
set -x EDITOR nano

编辑配置文件后,要么重新启动终端,要么获取文件:

$ source ~/.bashrc  # or ~/.zshrc

常用编辑器选项

以下是一些您可以使用的常用编辑器选项:

  • vivim:Vi/Vim 文本编辑器

  • nano:Nano 文本编辑器(适合初学者)

  • emacs:Emacs 文本编辑器

  • code -w:Visual Studio 代码(要求安装 VS Code CLI)

  • subl -w:Sublime Text(要求安装 Sublime CLI)

注意

GUI 编辑器的 -w 标志很重要,因为它会让终端等到文件关闭。

工作方式

/editor 命令遵循此工作流程:

  1. 当您使用 /editor 命令时,Amazon Q 会创建一个扩展名为 .md 的临时文件

  2. 通过此文件打开指定的编辑器

  3. 您在编辑器中编写提示并保存文件

  4. 当您关闭编辑器时

  5. Amazon Q 读取内容并将其作为提示提交

  6. 自动清理临时文件

在编辑器中处理代码

当您在编辑器中编写代码时,如果您关闭编辑器,整个内容将作为提示发送给 Amazon Q。该代码不在本地执行,而是被视为 AI 的文本输入。

示例:编写和提交代码

  1. 键入 /editor 以打开编辑器

  2. 在编辑器中编写 Python 脚本:

    def fibonacci(n): if n <= 1: return n else: return fibonacci(n-1) + fibonacci(n-2) # This function seems inefficient # How can I improve it?
  3. 保存并关闭编辑器

  4. Amazon Q 将收到这整段文本作为提示,并在回复中提供改进代码的建议

这种方法可用于:

  • 获取代码审查

  • 要求优化

  • 解释复杂的代码结构

  • 为调试帮助提供上下文

与其它命令相结合

当与其它 Amazon Q CLI 命令结合时,/editor 命令会变得功能更加强大。以下是一些增强工作流程的实用组合。

将 /editor 与 /compact 结合使用

/compact 命令使 Amazon Q 回复更加简洁。这种组合非常适合高效的代码审查:

Amazon Q> /editor
# Write in the editor:
Please review this Python function that calculates prime numbers:

def is_prime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True

# Save and close

Amazon Q> /compact
# This makes Amazon Q provide a concise code review

将 /editor 与 /context 结合使用

/context 命令将文件添加到对话上下文中。这种组合对于讨论引用其它文件的代码很有用:

Amazon Q> /context path/to/config.json
Amazon Q> /editor
# Write in the editor:
Given the config.json file I just shared, please help me write a Python function that:
1. Loads the configuration
2. Validates all required fields are present
3. Returns a validated config object

# Save and close

将 /editor 与 /clear 结合使用

/clear 命令开始新的对话。这种组合在切换话题时会有所帮助:

Amazon Q> /clear
Amazon Q> /editor
# Write in the editor:
I want to start a new discussion about AWS Lambda cold starts.
What are the best practices for minimizing cold start times for Python Lambda functions?

# Save and close

使用 /editor 进行多步对话

/editor 命令在每次使用时都会创建一个新的临时文件。您可以在对话中多次使用它,以便在之前回复的基础上继续构建:

# First use of editor for initial complex question
Amazon Q> /editor
# Write in editor:
I need to design a database schema for a library management system.
Requirements:
- Track books, authors, publishers
- Handle member checkouts and returns
- Support reservations and waiting lists
- Generate overdue notices

# After getting Amazon Q's response with initial schema design

# Second use of editor for follow-up with specific implementation details
Amazon Q> /editor
# Write in editor:
Based on your proposed schema, I have some follow-up questions:
1. How would you modify the Member table to support different membership tiers?
2. What indexes would you recommend for optimizing checkout queries?
3. Can you show me SQL to create the Books and Authors tables with proper relationships?

这种方法的好处是,您可以精心制作引用之前对话的复杂后续问题,而不必在命令行中键入所有内容。每个编辑器会话都为您提供空间和格式控制权,让您可以在 Amazon Q 的之前回复的基础上撰写详细的问题。

将 /editor 与 /profile 结合使用

在使用编辑器处理专门的问题之前,切换到不同的上下文配置文件:

Amazon Q> /profile set aws-developer
Amazon Q> /editor
# Write detailed AWS-specific questions that benefit from the AWS developer profile context

将 /editor 与 /help 结合使用

如果您不确定命令选项,可以在 /editor 之前使用 /help

Amazon Q> /help editor
# Review the help information
Amazon Q> /editor
# Use the editor with better understanding of available options

命令组合的最佳实践

  1. 当您需要引用特定文件时,在 /editor 之前使用 /context

  2. 当您想对复杂的问题得到简洁的回复时,在 /compact 之前使用 /editor

  3. 当开始一个全新的话题时,在 /editor 之前使用 /clear

  4. 使用多个 /editor 会话进行复杂的、由多个部分组成的对话,其中您需要仔细编撰后续问题

  5. 使用 /editor 前请考虑您当前的配置文件,以确保您处于正确的上下文中

有效使用提示

  • 使用编辑器处理复杂的提示,这些提示可以从精心设计中受益

  • 包括带有适当缩进的代码示例

  • 用清晰的章节整理由多个部分组成的问题

  • 使用 Markdown 格式以获得更好的结构

  • 如果您保存空文件,则不会提交任何提示

故障排除

  • 编辑器未打开:验证 $EDITOR 环境变量是否设置正确

  • “No such file or directory”错误:确保在 PATH 中安装了编辑器命令

  • 终端挂起:对于 GUI 编辑器,请务必使用等待标志(例如 -w

  • 内容未提交:在关闭编辑器之前,请检查您是否保存了文件