Atulya Jha
Atulya Jha

Reputation: 193

How to copy code from vscode to OneNote without losing the 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

  1. convert indentations to tabs in vscode
  2. copy the code first in MS Word then in OneNote.

Upvotes: 3

Views: 5946

Answers (6)

Peter H.
Peter H.

Reputation: 11

There is an extension for Onenote where you can copy your code into onenote and format it in onenote: NoteHighlight -> C# for my example enter image description here

It can be found here: https://github.com/elvirbrk/NoteHighlight2016

Upvotes: 0

Euclid.H
Euclid.H

Reputation: 184

One and only easy solution.

Copy the code from VSCode and Paste it in Word file

Next copy the code from the Word file to ONENOTE.

And you will keep the code format.

Upvotes: 2

cliffclof
cliffclof

Reputation: 334

On Windows

  1. in VS Code change the settings Editor: Insert Spaces to checked.
  2. before copying JS code from VS Code to OneNote find replace using regex:
    • Find: \n\n
    • replace: \n\n\n
    • replace all.
  3. Cut and paste into OneNote.

Seems to be a bug in the copy paste of LF / CRLF defined text to OneNote.

Upvotes: 0

Al-Framingham
Al-Framingham

Reputation: 11

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

Upvotes: 1

Mark
Mark

Reputation: 182076

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).

Upvotes: 2

M.McMoran
M.McMoran

Reputation: 31

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

Upvotes: 2

Related Questions