EN | RU
This is the macros for LibreOffice to paste copied code from VS Code with preserve syntax highlighting and indents.
VS Code implemented very useful feature - preserve code formatting when copying text. When pasting into Microsoft Word - it will keep the colors and indents of the code:
But when pasting into LibreOffice Writer, the situation is different - we have two choices:
-
HyperText Markup Language (HTML): This option will preserve colors, but not the indents: -
Unformatted text: This option will preserve indents, but not the colors:
(On the LibreOffice screenshots above, background color Dark Grey 4 was added to the paragraphs with code)
So why this is happening and how to make pasting work properly in LibreOffice too?
At first I thought it was some LibreOffice setting. But found nothing except maybe autocorrect (Tools - AutoCorrect - AutoCorrect Options...):
But disabling settings about spaces did not change anything.
Next I found unanswered question here and then here. On the second post the pawel229 user posted his solution, which became the base of my.
The solution is simple - because we have both styling and indents info in clipboard, we simply need to paste our code with style, and then insert indents for each line. The source code for the macros to do that is in file indentation_macro_code.vb.
You need:
-
Copy contents of the file
indentation_macro_code.vb; -
Open LibreOffice Writer;
-
Select
Tools-Macros-Edit Macros...; -
Open
My Macros & Dialog-Standard-Module1and paste code fromindentation_macro_code.vb. Result should look like this: -
Now open
Tools-Customize...-Keyboard. SelectKeyboardtab. ChooseLibreOfficeradio-button in the right-top corner. Now inCategorychooseApplication Macros-My Macros-Standard-Module1. SelectCopyIndentationinFunction. Now in theShortcut Keysselect, for example,Alt+Vand pressAssign: -
Press
OK; -
Now you can copy some code from VS Code and paste in using
Alt + Vhotkey. Result:(I had to insert 2 spaces in the first line and apply
Dark Grey 4color to the background of the paragraphs)
Feel free to contribute via pull requests or issues!








