How do I make Kate indent with spaces on Python files but use tabs for text files and other files?
Solution 1
There are multiple ways to achieve what you want. In order, Kate is doing the following:
- Kate reads the settings that are configured globally in the config dialog in the Indentation tab.
- Kate reads optional session data, i.e. if you use sessions and manually chose settings in a file, these settings should be restored again when opening the file.
- Kate reads the "Filetype" configuration: The filetype, also called
mode
, can be configured inSettings
>Configure Kate
>Open/Save
>Modes & Filetypes
tab. Choose your filetype, e.g.Scripts/Python
and then add a modeline like this:kate: indent-pasted-text false; indent-width 4;
- Kate searches for document variables in
.kateconfig
files recursively upwards. If found, it will apply these settings - Kate reads document variables in the document itself. So in a Python file, you can simply add a comment in the first or last
10
lines of the file and write e.g.:#kate: indent-pasted-text false; indent-width 4;
All this is also described in the Kate Handbook.
Solution 2
[I just had a similar problem and have found the one answer here unsatisfying. There are five options given which may be nice to know about but may also be ovewhelmingly complex. Also, the two configuration variables given (indent-pasted-text and indent-width) do not answer the question.]
Use the global Kate settings to "use tabs for text files and other files".
To "make Kate indent with spaces on Python files", create a file .kateconfig
in your home directory and enter the line
kate-wildcard(*.py): replace-tabs on;
You might also want set (in the same line)
replace-tabs-save on; show-tabs on; indent-width 2;
replace-tabs works while typing, replace-tabs-save works when saving, show-tabs shows a visible glyph for each tab which may be helpful if you open a file from someone else, indent-width sets the width of a tab measured in blanks (or any character of your monospace font).
This only works for Python files in (a subdirectory of) your home directory. If necessary, place the .kateconfig
file somewhere else.
Related videos on Youtube
Jarek
You may be interested in the story of SE moderator Monica Cellio and how she was unfairly treated by the corporate management of this site. More info here. An update is available. Let's hope we can cultivate a more fair environment for content creators and moderators going forward.
Updated on September 18, 2022Comments
-
Jarek over 1 year
My goal is to set Kate up to work properly on Python files but to use different settings (tabs not spaces) on other documents. I'm sure others are doing this, but I can't figure out a convenient solution. I appreciate any advice.
Kate has settings for indentation here:
- Click the Settings menu
- Click "Configure - Kate"
- On the right expand "Editor"
- Click "Indentation"
One option is "Default indentation mode". One choice for that setting is Python. However, I cannot find where to set (or even display) the options used for the Python choice.
Furthermore, it is not clear what is the interaction between "Default indentation mode" and the explicit settings for indentation on that page. Does one override the other?
-
Scz almost 9 yearsThe link in you post does not seem to work. I found an alternative here
-
dhaumann almost 9 yearsThanks, I updated the link to the 'katepart' handbook, since the Kate handbook and KWrite handbook now share this part.
-
MichaelK about 3 yearsAs noted in this answer, under
Modes & Filetypes
, you can click the tool button next toVariables
to get a GUI popup for these settings. I found that quite helpful. Also worth noting that you may have to close and reopen the file before the new settings are applied to it. -
Jarek almost 3 yearsThank you for adding an answer.
-
Jarek almost 3 yearsI found a similar question here: unix.stackexchange.com/a/134424