如何在Kdevelop中实现类似Kate的临时文本过滤功能?
How to Implement Kate-like Text Filtering in KDevelop
Great question! KDevelop doesn’t have a built-in "temporary text filter" menu item like Kate does, but you can replicate this functionality perfectly using custom external tools paired with a simple shell script. Here's how to set it up step by step:
Step 1: Create a Shell Script to Handle Dynamic Commands
First, we’ll make a small bash script that prompts you for a filter command, processes the selected text with that command, and outputs the result.
- Open a text editor and paste this code into a file named
filter_text.sh:
#!/bin/bash # Prompt for the filter command using KDE's dialog tool COMMAND=$(kdialog --inputbox "Enter your filter command (e.g., sort, grep 'pattern', perltidy):" "Text Filter") # Exit if no command was entered if [ -z "$COMMAND" ]; then exit 1 fi # Read the selected text from stdin, run it through the command, and output the result cat - | eval "$COMMAND"
- Save the file somewhere accessible (like
~/bin/), then make it executable in the terminal:
chmod +x ~/bin/filter_text.sh
Step 2: Configure the External Tool in KDevelop
Now we’ll hook this script into KDevelop and bind it to a shortcut:
- Open KDevelop and go to
Settings > Configure KDevelop... - In the left sidebar, navigate to External Tools
- Click the
Addbutton at the bottom to create a new tool:- Name it something clear, like "Filter Selected Text"
- In the
Commandfield, browse to thefilter_text.shscript you just created (e.g.,/home/your-username/bin/filter_text.sh)
- Switch to the Input tab:
- Select
Current selectionfrom the "Input source" dropdown (this sends your selected text to the script's stdin)
- Select
- Switch to the Output tab:
- Select
Replace current selectionfrom the "Output destination" dropdown (this replaces your selected text with the command's stdout)
- Select
- Switch to the Keyboard Shortcuts tab:
- Click the "None" field next to your new tool, then press your preferred shortcut (e.g.,
Ctrl+Alt+Fto match Kate's default)
- Click the "None" field next to your new tool, then press your preferred shortcut (e.g.,
- Click
ApplythenOKto save the settings.
Step 3: Use the Filter
Now you’re ready to use it just like in Kate:
- Select any text in KDevelop
- Press your shortcut (e.g., Ctrl+Alt+F)
- Enter your desired command in the dialog (like
sort,grep 'error', orperltidy) - Click OK, and your selected text will be replaced with the command's output!
Notes
- The script uses
kdialog(part of KDE) to show the input prompt—this should be pre-installed with KDevelop, but if not, you can install it via your package manager. - Be cautious with
evalin the script: since you're entering your own commands, make sure they're safe (avoid untrusted input, as always).
内容的提问来源于stack exchange,提问作者daxim
相关产品推荐
相关产品推荐

