Nerd For Tech
Published in

Nerd For Tech

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.

Method 1: Embed directly in medium

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.

<Enter your code here>

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

plt.title(f"G 20 Countries : {abbr['gni_pc']} vs {abbr['life_ex']}")
# x and y axis labels plt.xlabel(abbr['gni_pc']) plt.ylabel(abbr['life_ex']) #Country names
for i in range(hdi_df.shape[0]):
plt.text(df.gni_pc[i], y=df.life_ex[i], s=df.Country[i]))

Method 2: Embed using Github Gist

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.

Video Format

If you prefer the video format of content, you can check out the following YouTube Video.




NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

Making Money on the App Store

Challenges of micro-service deployments

What is API Governance? 8 Best Practices for API Governance Success

7 Life Lessons That A Coding Bootcamp Teaches New Programmers: Lesson 7 — Teach What You Know

Pip installing Unidecode Python 2.7 — A Non-Developer Guide

Building My Data Science Foundation

Track your infrastructure events with AWS SNS and Notify17

Swift: Common mistakes no one bothers about — Optional force unwrap, vars and force cast

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
Abhijith Chandradas

Abhijith Chandradas

Data Analyst | Hacker | Financial Analyst | Freelancer | IIM MBA | Opensource | Democratize Knowledge |

More from Medium

Building a Txt-to-Speech Telegram bot in python

How to optimize selector browser by inject JavaScript in UiPath (Step by Step)

What is MERN Stack

MERN stack

Extracting significant content from a web page using Arc90 Readability algorithm