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!