You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sublime Text自定义快捷键格式化选中文本方法求助

Got it, let's figure out how to set up a custom shortcut that wraps your selected text into Django's {% trans "..." %} tag—this is totally doable with editor-specific tools or cross-platform scripting. Here are step-by-step solutions for the most common setups:

VS Code makes this easy with user snippets and custom keybindings:

  • Step 1: Create a custom snippet
    Open the command panel (Ctrl+Shift+P), type Configure User Snippets, then select New Global Snippets file. Name it something like django-trans.code-snippets, then paste this:
    {
      "Wrap selected text in trans tag": {
        "prefix": "trans-wrap",
        "body": [
          "{% trans \"$TM_SELECTED_TEXT\" %}"
        ],
        "description": "Wrap selected text in Django trans template tag"
      }
    }
    
  • Step 2: Bind a shortcut
    Open keyboard shortcut settings (Ctrl+K Ctrl+S), click the "Open Keyboard Shortcuts (JSON)" icon in the top-right. Add this entry (replace ctrl+shift+t with your preferred shortcut):
    {
      "key": "ctrl+shift+t",
      "command": "editor.action.insertSnippet",
      "args": {
        "name": "Wrap selected text in trans tag"
      },
      "when": "editorTextFocus && editorHasSelection"
    }
    
    The when condition ensures the shortcut only works when you have text selected and the editor is active.
Solution 2: Sublime Text

For Sublime users, use snippets + keybindings:

  • Step 1: Create a snippet
    Go to Tools > Developer > New Snippet, then paste this:
    <snippet>
      <content><![CDATA[
    {% trans "${SELECTION}" %}
    ]]></content>
      <tabTrigger>trans-wrap</tabTrigger>
      <scope>text.html.django</scope>
      <description>Wrap selected text in trans tag</description>
    </snippet>
    
    Save it as django-trans.sublime-snippet in Sublime's User directory.
  • Step 2: Add a shortcut
    Open Preferences > Key Bindings and add this:
    {
      "keys": ["ctrl+shift+t"],
      "command": "insert_snippet",
      "args": {"name": "Packages/User/django-trans.sublime-snippet"},
      "context": [{"key": "selection_empty", "operator": "equal", "operand": false}]
    }
    
Solution 3: Cross-Editor (AutoHotkey for Windows)

If you want this shortcut to work in any editor, AutoHotkey is a great cross-platform (Windows-focused) tool:

  • Install AutoHotkey, then create a new .ahk file with this code:
    ^+t:: ; Ctrl+Shift+T as the shortcut
    Send, ^c ; Copy selected text
    ClipWait, 1 ; Wait for clipboard to populate
    if ErrorLevel
        return
    Clipboard := "{% trans """ . Clipboard . """ %}" ; Format the text
    Send, ^v ; Paste the formatted text back
    return
    
    Run the script, and you can use the shortcut in any app where you can select text.

All these solutions directly solve your problem: select text like Home, hit your custom shortcut, and it becomes {% trans "Home" %}. For other editors (like IntelliJ/PyCharm), the core idea is the same—use the editor's snippet/macro system to wrap selected text and bind it to a shortcut.

内容的提问来源于stack exchange,提问作者CodeUnsolved

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:28:09