When you click on the Options menu next to the code block, or the Actions menu in the block itself, you’ll see a number of options you can set.
Set syntax…
You can set the syntax in your code block to any of the supported languages. This will enable syntax highlighting in that language, too.
We use Prism for syntax highlighting. You can use Test Drive Prism to check which languages Prism supports. If you notice a mismatch between GitBook and Prism, there’s a chance we’re a version or two behind. We’ll catch up soon!
With line numbers
This will toggle line numbers for your code on and off.
Showing line numbers is useful when the code represents the contents of a file as a whole, or when you have long code blocks with lots of lines. Hiding line numbers is useful for snippets, usage instructions for command line or terminal expressions and similar scenarios.
With caption
This will toggle a caption that sits at the top of the block, above your lines of code.
The caption is often the name of a file as shown in our example above, but you can also use it as a title, description, or anything else you’d like.
Wrap code
This will toggle code wrapping on and off, so long lines of code will wrap to all be visible on the page at once.
Wrapping lines is useful when your code is long and you want to avoid having the viewer scroll back and forth to read it. If you toggle Wrap code on, you may also want to show line numbers — this will make it easier to read the code and understand where new lines start.
Expandable
This will toggle showing the code in full (when the toggle is off) or a collapsed window of the code which the user can expand (when the toggle is on).
The collapsed view defaults to showing 10 lines of code with an button to expand to show the full code block. If there are less than 10 lines of code, all the content will be shown.
Code block actions
As well as the options above, you can also change the language the code block displays, and copy your code instantly.
Copy the code
Hover over a code block and a number of icons will appear. Click the middle icon to copy the contents of the code block to your clipboard.