Adding Code Blocks

In this lesson, you'll learn how to add code blocks.

At times, you'll have longer snippets of code, or partial or complete programs or scripts, that don't fit in a sentence and may take several lines.

These are good cases for adding a code block.

A code block is a multi-line section of code.

To create one, start a new text area in your post, type three back ticks together, then the language your code block will use. "```bash" for a BASH code block, as an example.

Click on + then type ```bash and hit [Enter]. (I pasted in a BASH script I just wrote to teach readers about the Linux ls command.

#!/bin/bash

# This is a BASH code block.

# Comment lines start with a hash mark "#"

# Quick BASH script to demonstrate code blocks with an ls tutorial.
# The ls command lists the contents of the present working directory by 
# default.

# echo "" adds a blank line to output.

echo ""
echo "The ls command lists the contents of the current workind directory"
echo "by default."
echo ""
echo "ls command default output"
ls
echo ""
echo "Options can be added by putting a space after the command, a -, and a letter."
echo "-a will display all files in a directory, including hidden files."
echo "-l will list files in long format."
echo "-h will show the file sizes in human readable form."
echo "You can string several options together after the -, using no spaces."
echo ""
echo "ls command output with the -alh options applied"
ls -alh
echo ""

echo "Using the -A option will list all files including hidden ones, but will not"
echo "list current or up-level directory (. and ..)."
echo ""
echo "ls command output with the -Alh options applied"
ls -Alh
echo ""

echo "ls with the -al options is such a commonly run command, there's an alias of"
echo "ll defined on many modern Linux systems."

echo ""
echo "Contents of the ll shortcut command to list files as shown by running"
echo "cat ~/.bashrc | grep "alias ll""
echo ""
cat ~/.bashrc | grep "alias ll"
echo ""

echo "Note that the ll shortcut includes the -F option."
echo ""

echo "-F appends a symbol to the filename. Symbol meanings are:"
echo ""

echo "@ means this is a symbolic link."
echo "* means file is executable."
echo "= means it's a socket."
echo "| means it's a named pipe."
echo "> means it's a door."
echo "/ means it's a directory."
echo ""

echo "Output of ls -alF command."
ls -alF
echo ""

echo "That's it for a quick ls tutorial using BASH!"
echo ""

Many programming languages are supported in your code blocks. To use something other than bash, type ```<your language>.

For example, ```html to make a new html based code block.

Or, ```python for a python code block.

That's all there is to adding a code block to your Ghost blog posts!

Many modern Linux systems have color in their text editors by default to help developers see what's going on in the code they're writing.

It looks much more interesting with color, and you'll learn how to add color to your code blocks in the next lesson.