Can Kate export a file excluding comments?

Since Kate can identify and style comments for multiple code languages, is it possible to export a copy of the file from Kate with the comments removed? Thank you.

The ability of kate to identify the programming or scripting language does not come from kate itself, but from the package syntax-highlighting. It is this package which visually identifies comments in code as indeed being comments.

I don’t see any way by which you could force kate to export a file with the identified-as-comments text automatically removed. I think you had better use another tool for that, such as a sed script with regular expressions. :thinking:

I believe the only way you can effectively do this is manually. I looked in to it and Kate does have an option to remove comment markers (Tools → Uncomment), although this does not seem to be what you are looking for.

It of course may depend on the programming language(s) regarding the formatting of such. :thinking:

I suppose if you are really brave, an AI could do that, but of course, you will need to thoroughly check the results. :wink:

Yes, there is a way. This is what I might do:

  • Open the file in Kate and hit Search (opens dialog).
  • Make sure “Enable regular expressions” {} is selected.
  • Paste ^[#;].* into the Find box – Leave the Replace box empty.
  • Click Search (next to Find) → selects all lines starting with #.
  • Click Replace or Replace Checked (next to Replace) → Done!

Presuming you wish to keep the original commented version, select Save As → to whichever destination you choose. It may be a little clunky, but it gets the job done.


Please read:

  • ^ indicates the very beginning of a line.
  • [#;] are the characters that will be matched (between the brackets).
    :point_right: Caution: there are two kinds of escaping characters here – remove the one you don’t wish to replace, otherwise you might lose more than you bargained for.
  • .* matches whatever characters remain on that line.

Well, a lot depends on the programming languages the OP is interested in - they did not say.

In C++, for instance, comments can start with // anywhere in a line, or be contained across any number of lines bracketed by /* and */. But when the comment delimiters are in strings ("/* this is not a // comment */") then things are trickier.

And even for the type of file you are maybe thinking of, I think you’d at least want to consider if white space is allowed before the # or ;.

Thank you. I’m using multiple languages that include comments as # ;# // /*...*/ <!--...--> I think that’s all.

Thought it worth asking since Kate (or a package it uses) seems to identfy them all, whether a single line, multiple lines, or a partial line along side uncommented code. Under the Tools menu there are several options to filter or run scripts againt the file but nothing to remove comments that I could see.

Perhaps I can write something in Tcl.The files are too big to have to do manually for every update and I don’t want to maintain a commented and uncommented version of each file and update both.

@soundofthunder Thanks for the links. I am especially dense when it comes to regular expressions. I use them every so often but find them more challenging than most things.

I’ve not tried any, but you could take a look at some other people’s attempts, like: GitHub - rhythmcache/comment-remover: Strip comments from your code · GitHub