How to Embed Code in Medium

Two easy methods for embedding code in your Medium article.

When writing an article about coding, writers often find it difficult to add code to the article. Code in normal text ruins the formatting and does not differentiate the text from the code. Some writers overcome this problem by uploading the screenshot of the code.

In this article I will explain two easy methods to embed code into your Medium article.

To embed code directly in medium, use the following shortcut key:

  • Mac : Command+ Option+6
  • Windows: Control + Alt + 6
  • Linux: Control + Alt + 6

When you press these keys, a grey box as below will appear, inside which you can write your code.

Check how the code formatting is retained . The code stands out with the help of typewriter font and highlighting.

Github Gist is a much more appealing and readable option compared to directly embedding using Medium.

First, you have to sign up to Github gist and login to your account.

On the top left of Github gist page, you can find a ‘+’ sign, you can press on it to create a new gist.

Image by author

You’ll be taken to the next page where you have to enter the following details:
i. gist description
ii. your code snippet’s title or filename
iii. your code snippet to be shared

Image by author

You can save it as a public or secret gist.
If you want to publish it on medium, then you should save your gist as public.

Image by author

Copy the link to your gist (url) or get the sharable link.
It can be embedded into Medium article by simply pasting it in a new line or using embed link option.

Image by author

This is how your gist will then look on medium.

This code is written for an article on quadrant analysis.

The advantage of using Git Gist is that when the code is edited in Gist, the changes will be reflected in the Medium article and wherever the code is shared.

Photo by Markus Spiske on Unsplash

Carpe Diem!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store