How to copy code from vscode to OneNote without losing the formatting? - formatting

I want to keep a record of code I am working on by saving it in MicroSoft Office OneNote. When I copy and paste the code, all the indentations are gone.
def primeGenerator(primeList1, arr):
for i in range(2, len(arr)):
if arr[i]==0:
primeList1.append(i)
for j in range(i**2, len(arr), i):
arr[j] = 1
Code shown above becomes like this
def primeGenerator(primeList1, arr):
for i in range(2, len(arr)):
if arr[i]==0:
primeList1.append(i)
for j in range(i**2, len(arr), i):
arr[j] = 1
I tried the solutions I found on the internet like
convert indentations to tabs in vscode
copy the code first in MS Word then in OneNote.

It isn't supported natively, however there are open source workarounds such as:
https://github.com/elvirbrk/NoteHighlight2016

There is a OneNote Settings/Options/Paste Options/
Make sure that is set to Keep Source Formatting.
My pastes into OneNote retain indentation and syntax highlighting (I have Editor: Copy with Syntax Highlighting enabled).

I would advise against using Keep Source Formatting in OneNote under Settings. I just notice that when Copying from Visual Studio Code into OneNote, OneNote replaces all space charactes (0x20) with NonBreak Space (0xA0).
To recreate the problem, copy something from VSC, and paste it here. Then Paste it into OneNote. Then copy from OneNote, and paste it back in that same link. You will see that all the spaces (0x20) has been turn into NonBreak Space (0xA0), which Visual Studio Code does not like (CSS, JS, etc).
Instead use Keep Text Only

On Windows
in VS Code change the settings Editor: Insert Spaces to checked.
before copying JS code from VS Code to OneNote find replace using
regex:
Find: \n\n
replace: \n\n\n
replace all.
Cut and paste into OneNote.
Seems to be a bug in the copy paste of LF / CRLF defined text to OneNote.

Related

How to insert code in Word, export to pdf, and keep formatting?

I've written some Python code for my thesis, and wish to add this to my report in Word. I have to hand it in in pdf format. But when I convert the word document, the code cannot be copy pasted and used again, as the tabs are replaced by a single space.
Is there any solution for this?
found the answer: replace all spaces by _ , and possibly make them invisible (white).
If you want to copy paste and use the code, just replace all _ by spaces again.
Solved!

How to paste multiline text in pycharm?

Everytime i try to copy and paste something on my Pycharm editor it all paste it in single line. How to automatically paste in multiline?[1]
This is what i copied.....
[{"DateObserved":"2020-06-12 ","HourObserved":23,"LocalTimeZone":"EST","ReportingArea":"Central New York Region","StateCode":"NY","Latitude":42.8049,"Longitude":-76.3589,"ParameterName":"O3","AQI":35,"Category":{"Number":1,"Name":"Good"}},{"DateObserved":"2020-06-12 ","HourObserved":23,"LocalTimeZone":"EST","ReportingArea":"Central New York Region","StateCode":"NY","Latitude":42.8049,"Longitude":-76.3589,"ParameterName":"PM2.5","AQI":0,"Category":{"Number":1,"Name":"Good"}}]
and this all got pasted in a single line on my Pycharm editor. can anybody help with that, its pretty hard to read all this info on a single line?
It seems like you didn't copy anything containing newlines as you can see better after my edit (See the side-by-side-markdown diff so you can see that I didn't remove any line breaks).
If you just copy everything as a single line, PyCharm won't be able to split it into multiple lines (except with auto-format maybe).
As you haven't said how you copied the input, I can't say what exactly is wrong.
However, it seems like you copied something from a program that didn't display the text correctly so that you couldn't copy it correctly.
Another possibility is that the IDE interprets line breaks differently.
For example, windows uses CRLF(carriage return+line feed) as a line seperator while linux uses LF only.
If your file contains only LF line breaks and PyCharm is configured to use CRLF like breaks, it is possible that it ignores them because of that.
You can change that behaviour at the bottom of your file in PyCharm (button with CR/CRLF/LF).

VS2019 copy and paste changes tabs to spaces

I've just upgraded from VS2012 to VS2019. I have the tabs set up in Options|TextEditor|C#|Tabs as Smart, Tab Size 4, Indent Size 4, Keep Tabs (as OOTB). I have the same set in All Languages, except that it refuses to set Smart Indenting (which may or may not be relevant to the problem).
The problem is that if I copy and paste the following line in a .CS file:
private bool myPrivateField1; // Private fields should be PascalCase
the indenting is fine, but the tabs between the code and the comment get replaced by spaces in both the copied line and the pasted line. 2012 doesn't do this and it's really annoying.
try to deactive adaptive formatting. Tools->Options->Text Editor->Advanced than uncheck [Use Adaptive Formatting]

How to preserve spaces in the Visual Editor of WordPress?

My client is trying to be able to copy and paste formatting from a Word document into WordPress and have the spacing be preserved (he is copying/pasting a poem written by a 3rd party). The Visual Editor in WordPress is removing the spaces by default. How can I get the spacing to stay? I need to make sure my client is able to copy and paste his text from Word into the Visual Editor because he does not know code. I've tried the following, but it hasn't worked:
I've tried adding this to the functions:
function allow_nbsp_in_tinymce( $mceInit ) {
$mceInit['entities'] = '160,nbsp,38,amp,60,lt,62,gt';
return $mceInit;
}
add_filter( 'tiny_mce_before_init', 'allow_nbsp_in_tinymce' );
If there was something like this, but instead of removing the spaces, it preserved them, that would be good: https://wordpress.org/plugins/space-remover/#reviews
Any ideas?

How to preserve formatting from rstudio when copy/pasting to Word?

I want to reproduce my code in Word 2010. The scripts were written in rstudio, and I would like to preserve rstudio's formatting when pasting into Word. Principally, I like the font colors and spacing that rstudio uses. I find that when I paste from SAS to Word, the formatting is preserved, but no dice here.
I would usually look for copy special / paste special options to do this, but I can't find any. When I try to paste special into word, only unformatted text options are presented. I would rather not reformat the text line-by-line, because I think it looks pretty nice in rstudio.
I thought of trying to save the script in rstudio to some format that would preserve its formatting, but I couldn't find any way to do this. How can it be done?
It's not totally clear whether you are pasting from RStudio's script editor (which has some 4 or 5 colors) or from the R console (script + output) within RStudio (which only has 2 colors).
If you are pasting from the console--please check "Paste special" again. There should be an option for "HTML Format" that will do what you need (though you may need to resize the font to make everything fit properly depending on your page margins).
If you are pasting from the script editor, then you're out of luck with a direct copy-and-paste solution. But there is a copy-and-paste-and-copy-and-paste solution...
One solution could be to use Notepad++. From RStudio, save your script (with a ".R" extension) then open the script in Notepad++. (Or copy and paste from RStudio to Notepad++, but make sure you set the file's language--from the "Language" menu--to R). When your script is correctly highlighted in Notepad++ go to the "Plugins > NppExport > Copy HTML to clipboard" menu to copy the open file. This can then be pasted into MS Word with HTML format.
Just in case someone else looks for this question...
Another way to have all the source code in a word document with a good-looking format using RStudio is to use the File/Compile Notebook option, choosing MS Word as the output format.
Using this option, a .docx document will be generated with the output of your script as well as the original source code. The script will be executed, though.
If you don't want your code to be evaluated (you just want a simple copy-paste), you can add #+eval=FALSE at the beginning of your script and then the source code will be reproduced in the word document without being evaluated.
This approach relies on knitr. Here is an example if anyone wants to start playing with this.
#' ---
#' title: "My homework"
#' author: John Doe
#' date: June 15, 2015
#' output: word_document
#' ---
# The header above sets some metadata used in the knitr output
# Conventional comments are formatted as regular comments
# Comments starting with "#+" control different knitr options.
#+echo=FALSE,message=FALSE,warning=FALSE
library(ggplot2)
#+echo=TRUE
#' Comments with a "+" sign are used to tell knitr what should be
#' done with the chunk of code:
#'
#' - echo: Show the original code or not
#' - eval: Run the original code or not
#' - message: Print messages
#' - warning: Print warnings
#' - error: Print errors
#' ...
#' Comments with an apostrophe "'" will be printed as regular text.
#' This is very useful to explain what you are actually doing!
# Regular comments can be used to document the code as usual
# Figures are printed:
ggplot(mpg, aes(x=cty, y=hwy)) + geom_point(aes(color=class))
#' Formatting **options** are possible.
#' Even [links](http://stackoverflow.com/questions/10128702/how-to-preserve-formatting-from-rstudio-when-copy-pasting-to-word)
#'
#' This will show all the packages and versions used to generate this document.
#' It can be used to make sure that your teacher has all he needs to run your script
#' if he/she wants to.
sessionInfo()
Assuming you have internet access
Copy and paste to gist.gisthub.com
Select 'R' as the language - this should provide colours
Hit create (secret or public) gist
Copy and paste from the gist to your word processor.
Compared with the notepad++ solution:
An online backup to your code, with a recording of the time when you clipped it.
You don't have to install any other software, useful if you're a student using a public computer.
If you just need the code as formatted:
Step1: Just add #+eval=FALSE at the beginning of your code.
Step2: Then go to File -> Knit Document. Compile the file in msword/PDF/Html.
OR
Just add #+eval=FALSE at the beginning of your code.
Press CTRL+SHIFT+K and then compile the file in msword/PDF/Html.
If you need the code with output do not enter add #+eval=FALSE at the beginning of your code and perform step 2 directly.
I agree with zeehio that using Knitr is probably the best option. But another way is to use the Pretty R tool and the "open document text" steps here. Basically just copy and paste your code into pretty R, and copy and paste the output (not the html) into the open document.
After you copy from the Rstudio Console window and paste into a Word document, you need to highlight all the the just copied text and change the font into Courier New. This will give you the same spacing and lineup as you had in the Rstudio Console window.
Copy paste the code from Rstudio editor to 'visual studio code' & then again copy from there into a word processor.
For this to happen you must first install R extension in visual studio code.
'Visual studio code' is itself an IDE which can potentially be used for R language as well, but right now I'm emphasizing on using it to answer the above question.
In R I use the Monaco editor font. To copy paste the output of the R consol in Microsoft Word, I select the output of the consol, right click and copy and paste in my Word document. Once I have pasted the output in word, I select it and put it in Word's Monaco font and reduce the size of the font if necessary.
This does the job very nicely and perfectly preserves the output style from the R consol, as well as written chunks of code.
If you want to retain the formatting when coping a selection from the R Console you will need to install an older version of R Studio. Version 1.2.5042. it will not work in the newer versions