Advanced Search and Replace with the Kate Text Editor

13604

Learn how to use the Kate text editor in this tutorial from our archives.

The powerhouse Kate text editor has advanced search-and-replace, including support for escape sequences and regular expressions, so you can make complex corrections without leaving your document.

The Kate text editor is my favorite and has been my main workhorse for years. Kate has a lot of great features and is friendly to both touch-typing and pointy-clicky. It doesn’t quite have the eleventy-million features of Vim or Emacs, but then you don’t need the dexterity of a concert pianist to use it, either. I think it is the most user-friendly of the powerhouse text editors. Some of its noteworthy features are:

  • Configurable keyboard shortcuts
  • Zoom view (My dear friends with perfect vision, please think of us who are not so fortunate)
  • File > Open With, to open your document in any app you desire.
  • Bookmarks
  • Sessions
  • Word count
  • Integrated terminal
  • Excellent syntax highlighting
  • Plugins
  • Tabs
  • Projects
  • Code folding
  • Split views
  • Snippets, which is a multi-item clipboard

And a whole lot more excellent and useful features we don’t need to get bogged down in now. (You can read about some of them in Expert Tips and Tricks With Kate and Konsole.) Let’s get going with Kate’s wonderful search-and-replace.

Simple Searches

A quick way to launch a basic search is to highlight a word, and then Kate performs a case-insensitive search for all matching words and highlights them (Figure 1). You can highlight a word with your mouse cursor, or with your keyboard: place the cursor at the end of the word, and then press Shift+Ctrl+Left Arrow.

Figure 1: Kate highlights all matching words.
Navigate through your matches with the F3 key and press Shift+F3 to navigate backwards. You may also use Ctrl+H and Ctrl+Shift+H. If you don’t like the default keyboard shortcuts, go to Settings > Configure Shortcuts to change them. The highlight color and other colors are configured in Settings > Configure Kate > Fonts & Colors.

Ctrl+F opens a simple search form at the bottom, where you type your search term. Kate searches as you type, and highlights your search term in light green when it finds matches, and pink when it doesn’t. The default search is case-insensitive. Click the little button to the right with the “a” on it for a case-sensitive search. There are also little arrows for navigating your matches, and a button to open the search and replace bar (Figure 2).

Figure 2: The search and replace bar.

Search and Replace

Now we’re at the fun part. One of my regular tasks is cleaning up the horrid HTML created in documents by Drupal, LibreOffice Writer, and Microsoft Word. Here is a tiny snippet of the raw dreadfulness:

Figure 3: Raw text.
What IS all that gobbledygook? It’s as though our good friends Drupal et al. think every word needs its own special HTML tags. Kate helps me speed through deleting the junk with pleasing ease. I highlight one tag at a time and then delete it and all of its duplicates at once (Figure 4).

Highlight the text you want to delete and press Ctrl+R to open the search and replace form. To delete your search term, leave the Replace field blank, then click Replace to verify and delete each occurrence one at a time, or Replace All to nuke the whole lot. Kate tells you how many occurrences were deleted.

Sometimes I don’t need to delete the tags but just remove all the rubbish, and turn something like <p style="margin-bottom: 0in; line-height: 100%"> into a nice normal <p>.

Figure 4: Deleting all duplicates at once.
This is a great tool for inserting text, for example target="_blank" in URLs, so they look like this: <“https://example.com” target=”_blank”>example.com</a>. All I have to do is highlight ">, and then replace it with target="_blank"> (Figure 5).

Figure 5: Search and replace.
Another common task is replacing “smart” quotes, which really are not smart, with normal quotes.

Kate treats spaces as plain text, so you can replace double spaces with single spaces, or whatever numbers of spaces you want. To select the spaces you want to replace with the keyboard, position the cursor at the left of the spaces, and then press Shift+Ctrl+Left Arrow.

Kate is good at not making you do useless extra steps. Your text in the Find field is automatically replaced when you perform a new search, so you don’t have to manually clear it. When you want to manually clear it there is a nice “clear” button to the right, or use Ctrl+A and Ctrl+C. If you make a mistake, press Ctrl+Z to undo, or use the Undo button at the top.

Another slick time-saver is Kate saves your search history in the Find field, and you can navigate through it with the up and down arrows.

Using Escape Sequences

Note that there is a Mode dropdown menu beneath the Replace field with these options: Plain Text, Whole Words, Escape Sequences, and Regular Expressions. The default is Plain Text. This finds any match for your search term. Whole Words narrows your search to exact whole word matches. Escape Sequences are the standard C/C++ escape sequences.

Sometimes I get articles to edit that are already nicely formatted, but without line breaks between the paragraphs. Web browsers don’t care, but I do. Set the mode to Escape Sequences, find </p> and replace it with </p>n. This inserts a newline after every closing paragraph tag.

I haven’t found other escape sequences that are useful, but if you know of any please mention them in the comments.

Regular Expressions

You can really go nuts with using regular expressions for super-powered search and replace. Come back soon for a nice regular expression cheat sheet to help you on your way.

Search Other Files

When you click the Search and Replace button at the bottom, this opens the most advanced search form. (There is no default keyboard shortcut, so you can create your own.) This form searches all open documents, and other files (Figure 6).

Figure 6: Search all open files.

Too Much?

If you’re thinking this is a bewildering trove of riches, don’t worry about it. You’re not obligated to use every last bit of functionality, but rather use the bits that let you work the way you want to.

Learn more about system management in the Essentials of System Administration training course from The Linux Foundation.