What is the best way to insert "plain text" code (like C++) in my documents?

I am making “reminder text files” for different coding languages i am learning, so i use writer to do this and i save them as .odt files and it’s great as it enables me to have different styles for the text and also having pictures.

Howerver i want to be able to have text box or frames where i can insert plain text (as having rich text code is not a good idea, expecially if i want to copy and paste that code somewhere else). Is there a way to make the text of my text box or frame plain by default?

Or is there even a way to easily switch to a “style” that would have no formatting when i need to enter some code ?

Ps: i am new to LibreOffice.

I’m not quite sure to understand your question.

Everything in Writer is “rich text”, even if you don’t see it as such (as in your code case). Every sequence of text has always a style associated with it: paragraph style to define the base appearance for a whole paragraph, character style to override the local paragraph style for a few words or characters. By default, the applied style is Default Style (surprising?).

Apparently, the question is about formatting “code” as simply as possible. The traditional approach is to use a fixed-width font like Liberation Mono, TeX Gyre Cursor, Source Code Pro or any font with “mono” in its name. Note that Bjarne Stroustrup has another opinion about it (see his The C++ Programming Language).

By chance, you have a built-in style for that: it is called Preformatted Text. Personally, I prefer to define a derived style based on this one I call Code Sample so that my code snippets are clearly identified and not mistaken for other text needing fixed pitch alignment. I also put some specific background and indents to my Code Sample so that my code snippets stand out of the surrounding text.

Now when you copy and paste snippets from one location to another within a Writer document, the property Code Sample or Preformatted Text follows the block (if you took the precaution to also include the paragraph mark).

If your question is also about copy from Writer then paste into another application, this will be done as “plain text”: a clipboard entry is stored under several variants, at least ODF format and plain text for Writer. When this entry is pasted somewhere else, the receiving application selects among the formats the one which is understood. Thus, a text editor will use only the plain text format.

I urge you to learn the basics about styles in Writer. Start with the built-in help, make experiment. Download the free manual and read it. Refrain from applying stylistic variations with toolbar buttons or keyboard shortcuts, like Ctrl B for bold. This is called direct formatting, it is “sticky” and will always play nasty tricks on your back.

To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!

“I also put some specific background and indents to my Code Sample so that my code snippets stand out of the surrounding text.”

THANK YOU ! This is perfect it’s exactly what i was looking for and even beyond my expectation, i’m experimenting with all the available options to customize my own code style.

However one last thing, is there any way to assign a shortcut to my custom style? i know you can assign shortcuts to all the “Heading” styles but i didn’t figure out how to do it yet (i gues a work around is to define a Heading style as my custom code style but i wondered if there was a more elegant solution.

I will use the manual you linked, there is a lot to be learnt from it, more than i imagined.

To assign keyboard shortcuts to paragraph styles use:
Tools -> Customize -> Tab: Keyboard and expand in Category Styles and select Paragraph. Category Function will show all available paragraphs. Now select under Shorcut Keys your preferred shortcut and click on push button Modify and close the dialog with OK

Perfect, cheers Opaque !

BTW there is an extension Code highlihter