How to use code block formatting in gmail (or any other email)?
Asked Answered
R

4

27

This question is not very technical, but I am trying to do something specific with formatting code for use in emails. I am trying to email a colleague several command line prompts like the one below:

C:\Users\DOOKIE\Documents\research>hg branches
default                      476:7556c41ee089

I would like to send it in a codeblock, with a grey background similar to the format above. Does anyone know how to do this in Gmail?

Romanticism answered 17/6, 2013 at 19:26 Comment(4)
could you simply select a Courier or other monospace font? It won't change the background color selectively but it will help with readability / layout. Otherwise you're stuck with making a .png I thinkTuneful
Yes I can select different fonts, but how will that display the grey background 'codeblock' format?Romanticism
I usually just put it into a blockquote: i.imgur.com/cgGBidK.pngOxcart
I do like the blockquotes, but I would prefer the codeblock formatting if possible. Thanks for the input.Romanticism
R
15

I discovered a plug-in (at least for Google chrome) called Markdown that is a lightweight markup language used for formatting text. It is currently used for project communication on GitHub and BitBucket repositories. I am sure there are similar solutions out there, but a browser plug-in like Markdown seems like a good way to go.

Through the use of tags, I can quote code and scripts, like the one in the above question, into a desired format. Simply adding three backticks (```) before and after any code will create a code block.

https://markdown-here.com/

Romanticism answered 30/6, 2013 at 20:10 Comment(2)
markdown-here is a more recent alternative working with most browsers.Pilsudski
Thanks for the tip - the answer has been updated to reference markdown-hereRomanticism
M
12

A good option is to use VS Code. A simple Ctrl C and Ctrl V will grab the code along with highlighting and your editor theme (In my case dark). You'll get something like: Code sent via email with dark mode with highlighting

Another option is a Chrome extension called Markdown Here which supports GitHub flavored markdown. Using the extension, here is what I achieved by enclosing Python code with ```Python (some code)``` Code sent via email highlighted using Markdown Here.

I find the Vs code version more good looking though. Happy highlighting!

Ps: Usually to highlight code we enclose it in three backticks (```) and if you want the code highlighted in a certain language syntax you write the name of the language after the opening backticks then the code and finally the closing set. An example with Python would be opening backticks, the word 'Python', the code then closing backticks. You will end up with something like:

print is True
if print is True:
    print('sample python code')
else:
    pass

Meliorism answered 27/10, 2020 at 5:45 Comment(0)
K
0

I have used a google drive plugin Code Blocks https://workspace.google.com/u/0/marketplace/app/code_blocks/100740430168?hl=en&pann=docs_addon_widget. Only works in Google Drive docs but similar to the VS code answer you can copy and paste the code block formatting to Gmail.

Klong answered 1/11, 2021 at 9:52 Comment(0)
H
-2

Ensure you paste as plain text. On Windows, you can use Ctrl+Shift+V

Hennessy answered 20/3 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.