Search code examples
replaceindentationstylinglibreofficeopenoffice-writer

Proper code indentation for tabs in LibreOffice/OpenOffice and Ms Word


I have some text documents (.doc and .odf) with portions of colored code appearing inside.

This code was copied as RTF from Notepad++, that's how it got colored.

However, in Notepad++ (and in many IDEs as well), the line wrap function works makes the indented code look better when in does not fit and goes to the next line.

In LibreOffice/OpenOffice and Ms Word it's possible to achieve a similar line wrap with the "increase indent" button.

So, what I'd like to do, is to automatically replace the tabs (or 4 spaces, if you like) with proper indents. Or make the tabs behave like I expect them to. Is it possible? Thanks.

Here's a visualization of the problem

LibreOffice Writer code indentation


Solution

  • I achieved what I want through some (not that many) manual steps.

    1. find out what is the maximum number of tabs (or sets of 4 spaces), say it's 3
    2. open the search and replace window, input 3 tabs (or 12 spaces), and click "Find All"
      • now all groups of 3 tabs are selected, and you are working on all the lines with maximum indentation
      • park or close the search and replace window, click 3 times on the "Increase indent" button (or set the left indentation in the paragraph style menu)
      • delete the selected groups of 3 tabs
    3. open the search and replace window, input 2 tabs (or 8 spaces), and click "Find All"
      • park or close the search and replace window, click 2 times on the "Increase indent" button
      • delete the selected groups of 2 tabs
    4. open the search and replace window, input 1 tabs (or 4 spaces), and click "Find All"
      • park or close the search and replace window, click 1 time on the "Increase indent" button
      • delete the selected single tabs

    Now you have a nice code indentation.

    If you are using Microsoft Word, then there's no "find all", but there is a way to apply paragraph styles directly from the search and replace menu. But the steps are a little different.

    First decide how wide a single indentation should be (e.g. 0.5 cm)

    • open the find and replace window, input 3 tabs (or 12 spaces) in the Find bar
    • leave the Replace bar empty, but click on it
    • if you don't see the Search options group, click More
    • click on Format
    • click on Paragraph
    • set a left indentation of 3 * the indentation width you want (e.g. 1.5 cm)
    • click Replace All, the paragraph style will be applied but the tabs/spaces will NOT be removed
    • click on the empty Replace bar (again)
    • click No Formatting
    • click on Replace All (again)
    • now the tabs/spaces will be deleted

    Rinse and repeat until you get a nice indentation.

    If you are using Python (or if you want to keep your white spaces) then instead of deleting the tabs (or spaces), you can replace them with a placeholder character you don't use in the rest of the code, say £ and replace them back in one pass when you are done. However, you'll get a skewed indentation.

    I guess there's a way to do this with macros, but this was good enough for me.