Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying code from Visual Studio Code to OneNote loses tab indentations even though syntax highlighting is retained

This is how the code is copied to onenote with paste option : keep source formatting enter image description here

If I select paste option : "keep text only", the indentations are preserved (and syntax highlighting is gone as expected)

enter image description here

If I copy the code to word with paste option : keep source formatting, it is copied perfectly preserving both indentation and syntax highlight.enter image description here

Why is indentation lost in oneNote only?

Office version - 2013

VS code version - May 2017 (version 1.13)

OS - Windows 10 Pro

like image 850
Enzio Avatar asked Jun 11 '17 04:06

Enzio


People also ask

How do I fix the indentation code in Visual Studio?

How do I fix indentation in Visual Studio? On Windows Shift + Alt + F. On Mac Shift + Option + F. On Ubuntu Ctrl + Shift + I.

How do I paste a code into OneNote?

Add Code Blocks in OneNote Let's say you want to store a code snippet in OneNote you might want to use later on. Click the NoteHighlight tab and select a language. In the pop-up window that appears, paste the code and click OK. Highlighted source code will show up on the page.

How do you copy and paste indent codes?

A simple fix is to try Ctrl + Shift + V after copying the code. It will paste all codes correctly with indentations in it.


3 Answers

As mentioned by @rambler OneNote ignores pasted spaces, the solution is converting all spaces to tabs in your document using:

ctrl+shift+p

then enter

convert indentation to tabs

now you can copy your code without problem,

if you want to make the tab indentation default you can change the setting by going to preferences and adding the following line:

"editor.insertSpaces": false,

like image 113
Ali80 Avatar answered Nov 02 '22 07:11

Ali80


If you copy   fourspaces fourspaces from vscode to onenote

,onenote will show fourspaces fourspaces

The code   fourspaces fourspaces in vscode is stored at clipboard like this

<meta http-equiv="content-type" content="text/html; charset=utf-8"><div style="color: #bbbbbb;background-color: #282c34;font-family: Fira Code;font-weight: normal;font-size: 15px;line-height: 20px;white-space: pre;"><div><span style="color: #bbbbbb;">    fourspaces    fourspaces</span></div></div>

Onenote ignore spaces, but tab are formatted to &nbsp;&nbsp;&nbsp

One solution is convert all indentation to tabs in your file

Generally, you could add "editor.insertSpaces": false,in your user setting; VSCode - How do I set tab-space style?

Or there is a vscode plugin named S.T.O.N.E, you can use it.

like image 40
Voyager Avatar answered Nov 02 '22 05:11

Voyager


You can use Outlook email or Microsoft Word as a placeholder.

Steps:

  1. Copy and paste from VS Code into Outlook email or Microsoft Word document.
  2. Copy and paste from Outlook email or Microsoft Word into OneNote.
like image 3
FullStackCoder Avatar answered Nov 02 '22 07:11

FullStackCoder