Claude 可以使用 Anthropic 定義的文字編輯器工具來檢視和修改文字檔案,幫助您除錯、修復和改進您的程式碼或其他文字文件。這使 Claude 能夠直接與您的檔案互動,提供實際的協助,而不僅僅是建議更改。
| 模型 | 工具版本 |
|---|---|
| Claude 4.x 模型 | text_editor_20250728 |
| Claude Sonnet 3.7 (已棄用) | text_editor_20250124 |
Claude 4 模型的 text_editor_20250728 工具不包含 undo_edit 命令。如果您需要此功能,您需要使用 Claude Sonnet 3.7 (已棄用)。
較舊的工具版本不保證與較新的模型向後相容。始終使用與您的模型版本相對應的工具版本。
以下是一些使用文字編輯器工具的例子:
文字編輯器工具可以按以下方式使用:
文字編輯器工具支援多個用於檢視和修改檔案的命令:
view 命令允許 Claude 檢查檔案的內容或列出目錄的內容。它可以讀取整個檔案或特定行範圍。
參數:
command:必須是「view」path:要檢視的檔案或目錄的路徑view_range(可選):一個包含兩個整數的陣列,指定要檢視的開始和結束行號。行號從 1 開始,結束行為 -1 表示讀到檔案末尾。此參數僅在檢視檔案時適用,不適用於目錄。str_replace 命令允許 Claude 用新字符串替換檔案中的特定字符串。這用於進行精確編輯。
參數:
command:必須是「str_replace」path:要修改的檔案的路徑old_str:要替換的文字(必須完全匹配,包括空格和縮排)new_str:要插入以替換舊文字的新文字create 命令允許 Claude 建立一個具有指定內容的新檔案。
參數:
command:必須是「create」path:應建立新檔案的路徑file_text:要寫入新檔案的內容insert 命令允許 Claude 在檔案中的特定位置插入文字。
參數:
command:必須是「insert」path:要修改的檔案的路徑insert_line:要在其後插入文字的行號(0 表示檔案開頭)new_str:要插入的文字undo_edit 命令允許 Claude 還原對檔案所做的最後編輯。
此命令僅在 Claude Sonnet 3.7 (已棄用) 中可用。Claude 4 模型使用 text_editor_20250728 不支援此命令。
參數:
command:必須是「undo_edit」path:應撤銷其最後編輯的檔案的路徑文字編輯器工具是作為無模式工具實現的。使用此工具時,您不需要像其他工具那樣提供輸入模式;該模式內置於 Claude 的模型中,無法修改。
工具類型取決於模型版本:
type: "text_editor_20250728"type: "text_editor_20250124"實現文字編輯器工具時,請記住:
使用文字編輯器工具時,可能會發生各種錯誤。以下是如何處理它們的指導:
The text editor tool uses the same pricing structure as other tools used with Claude. It follows the standard input and output token pricing based on the Claude model you're using.
In addition to the base tokens, the following additional input tokens are needed for the text editor tool:
| Tool | Additional input tokens |
|---|---|
text_editor_20250429 (Claude 4.x) | 700 tokens |
text_editor_20250124 (Claude Sonnet 3.7 (deprecated)) | 700 tokens |
有關工具定價的更詳細資訊,請參閱工具使用定價。
文字編輯器工具可與其他 Claude 工具一起使用。在組合工具時,請確保您:
| 日期 | 版本 | 變更 |
|---|---|---|
| 2025 年 7 月 28 日 | text_editor_20250728 | 發佈更新的文字編輯器工具,修復了一些問題並新增了可選的 max_characters 參數。除此之外,它與 text_editor_20250429 相同。 |
| 2025 年 4 月 29 日 | text_editor_20250429 | 發佈適用於 Claude 4 的文字編輯器工具。此版本移除了 undo_edit 命令,但保留了所有其他功能。工具名稱已更新以反映其基於 str_replace 的架構。 |
| 2025 年 3 月 13 日 | text_editor_20250124 | 獨立文字編輯器工具文件的介紹。此版本針對 Claude Sonnet 3.7 進行了最佳化,但具有與先前版本相同的功能。 |
| 2024 年 10 月 22 日 | text_editor_20241022 | 文字編輯器工具與 Claude Sonnet 3.5 的初始版本發佈(已停用)。透過 view、create、、 和 命令提供檢視、建立和編輯檔案的功能。 |
以下是如何以更便利和強大的方式使用文字編輯器工具的一些想法:
當您使用文字編輯器工具構建應用程式時,我們很期待看到您如何利用 Claude 的功能來增強您的開發工作流程和生產力。
向 Claude 提供文字編輯器工具和使用者提示
Claude 使用該工具檢查檔案或目錄
view 命令檢查檔案內容或列出目錄內容view 命令的 tool_use 內容區塊執行 view 命令並返回結果
max_characters 參數,請將檔案內容截斷到該長度tool_result 內容區塊的新 user 訊息將結果返回給 ClaudeClaude 使用該工具修改檔案
str_replace 等命令進行更改,或使用 insert 在特定行號處添加文字。str_replace 命令,Claude 會構造一個格式正確的工具使用請求,其中包含舊文字和要替換它的新文字執行編輯並返回結果
Claude 提供其分析和解釋
初始化您的編輯器實現
建立輔助函數來處理檔案操作,例如讀取、寫入和修改檔案。考慮實現備份功能以從錯誤中恢復。
處理編輯器工具呼叫
建立一個函數,根據命令類型處理來自 Claude 的工具呼叫:
def handle_editor_tool(tool_call, model_version):
input_params = tool_call.input
command = input_params.get('command', '')
file_path = input_params.get('path', '')
if command == 'view':
# Read and return file contents
pass
elif command == 'str_replace':
# Replace text in file
pass
elif command == 'create':
# Create new file
pass
elif command == 'insert':
# Insert text at location
pass
elif command == 'undo_edit':
# Check if it's a Claude 4 model
if 'str_replace_based_edit_tool' in model_version:
return {"error": "undo_edit command is not supported in Claude 4"}
# Restore from backup for Claude 3.7
pass實現安全措施
新增驗證和安全檢查:
處理 Claude 的回應
從 Claude 的回應中提取並處理工具呼叫:
# Process tool use in Claude's response
for content in response.content:
if content.type == "tool_use":
# Execute the tool based on command
result = handle_editor_tool(content)
# Return result to Claude
tool_result = {
"type": "tool_result",
"tool_use_id": content.id,
"content": result
}str_replaceinsertundo_edit使用 Claude 執行 shell 命令。