450

Google docs has a nice set of styles such as "Normal text" and "Heading 1".

Style dropdown in Google Docs: "Normal text", "Title"/"Subtitle", "Heading"s

How do I add my own? I want to add a style called "code" for text that is programming code.

10
  • 25
    I know the answer but I cannot add it because I don't have enough reputation. Basically you can utilize the Google Apps Scripts, here is what I did: pastebin.com/4pNBJ9pC. In order to enable it you need to create a new script: Tools >> Script Manager >> New and paste the code there. Then you will have to authorize the script and reload the document so that the menu appears on in the menu bar. Then select a patch of text and pick Extras >> Apply code style Commented Nov 5, 2013 at 5:46
  • 4
    I know my answer but my 101 reputation is, apparently, not enough. As of 3/14', once you update the existing headers via "Update Heading X to match", GDocs will add an additional Heading Y. So, it starts with just Heading 1,2,3, but once you update Heading 3, GDocs will introduce Heading 4. Once you update Heading 4, GDocs will introduce Heading 5, etc.
    – JJ Rohrer
    Commented Mar 3, 2014 at 15:29
  • 1
    @AlekseyBykov answer helped me with this one. I've updated and fixed some issues with it here github.com/cr8ivecodesmith/google_docs_scripts/blob/master/… Commented Jul 6, 2016 at 9:31
  • 1
    @ChrisMoschini I tried out that extension, but it doesn't allow me to use many of the fonts, including monospaced fonts, which is precisely what i need Commented May 10, 2017 at 16:04
  • 1
    I've learned to work backwards: format a load of empty newlines at the end of the document in your preferred code style and so you always write in monospace first. Then press alt-cmd-0 to format the NON-CODE paragraphs back to Normal. My ratio of text to code is probably equal anyway.
    – scipilot
    Commented Feb 11, 2018 at 3:29

6 Answers 6

182

Right now it's not possible to add more styles or rename the existed ones, but you can modify them to match your needs.

For example you could style your code as you wish and then by selecting one of the headers and choosing the Update Heading # to match selection, that particular style could become your new "Code" style.

Then select something else and apply that heading and you're done.

Styles in google docs

11
  • 150
    downside is this will include "code" in my TOC. :/ I found that using Subtitle style for code prevents it from showing in TOC.
    – Mark Nadig
    Commented Jul 30, 2012 at 23:40
  • 15
    +1 @digger69 for that tip. Using “Subtitle” for code is the single best tip I've ever gotten from this StackExchange. Great for having line-height 1.15 throughout the document, but 1.0 for code! :D Commented Jun 18, 2013 at 15:35
  • 14
    As I also mentioned in digger69's answer, another downside is that this does not allow for inline styles; it has to be on its own line. Of course, short code, or more likely, code-related terms (such as tag, function or variable names) often want to live inline within a paragraph of text.
    – Drewdavid
    Commented Nov 7, 2014 at 17:56
  • 4
    is it true that it has to be "block" meaning the whole line or paragraph must be that same style -- it cannot be a line with code mixed with normal text like this: the window object Commented Jan 24, 2016 at 6:00
  • 2
    Kludgey. Uses something not designed for the purpose. Commented Oct 28, 2018 at 0:00
77

As Lipis poins out, you still cannot create your own styles. So, you are left to repurpose an existing one. The downside to using Heading is this will result in "code" in my Table of Contents (TOC). :/ I found that using Title and Subtitle styles for code prevents those styles from showing in TOC.

3
  • 5
    The main downside of this approach is that while you can use ⌘+Alt+6 to use this layout if you override 6'th heading, there is no matching keyboard shortcut for the Subtitle style. Commented Jun 14, 2014 at 10:08
  • 13
    The other note is that this doesn't seem to work inline, has to be on its own line (true of all styles I imagine)
    – Drewdavid
    Commented Nov 7, 2014 at 17:54
  • 1
    If you also "Remove the space after paragraph" in "Line spacing" menu button you don't need to worry about paragraphs and using Shift + Enter as opposed to Enter anymore.
    – Aalex Gabi
    Commented Sep 11, 2020 at 8:02
64

A lot of the solutions don't work inline. This is a cleaned-up solution offered by @AlekseyBykov using Google App Scripts to add a custom menu action:

  1. Create a new script Extensions > Apps Script
  2. Copy the following code into the editor:
// Add new menu item
function onOpen() {
  DocumentApp.getUi()
  .createMenu('Styles')
  .addItem('Format Code', 'formatCode')
  .addToUi();
}

// Define code styling
var style = {};
style[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.CONSOLAS;
style[DocumentApp.Attribute.FONT_SIZE] = 10;
style[DocumentApp.Attribute.BACKGROUND_COLOR] = "#DDDDDD";
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#333333";
style[DocumentApp.Attribute.BOLD] = false;

// Apply code formatting
function formatCode() {
  var selection = DocumentApp.getActiveDocument().getSelection();
  if (selection) {
    var elements = selection.getRangeElements();
    for (var i = 0; i < elements.length; i++) {
      var element = elements[i];

      // Only modify elements that can be edited as text; skip images and other non-text elements.
      if (element.getElement().editAsText) {
        var text = element.getElement().editAsText();

        // Style the selected part of the element, or the full element if it's completely selected.
        if (element.isPartial()) {
          text.setAttributes(element.getStartOffset(), element.getEndOffsetInclusive(), style);
        } else {
          text.setAttributes(style);
        }
      }
    }
  }
}
  1. Run the onOpen function once from the code editor to trigger the authorization prompts and approve them.
  2. Use the new menu in your document to format selected text Styles > Format Code
13
  • 3
    Excellent and worked perfectly. For a more 'Slack' backtick style: style[DocumentApp.Attribute.BACKGROUND_COLOR] = "#FFEEEE"; style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#880000"; Commented Jun 27, 2018 at 21:00
  • 9
    excellent stuff. I didn't like that the background was missing between words, so I modified it to put the content in a table cell with a solid background: gist.github.com/fatso83/ffb7871c537e04d9ce95f66fccc30495
    – oligofren
    Commented Sep 19, 2018 at 15:22
  • 2
    One limitation is that you can't easily change the style of all existing "code" strings in your text (eg you decide that all inline code should be 9-pt intsead of 10). You would need an "old style" style in the script, and a function that search all the text for style that matches the old code style, finds the end, and changes to new style... That being said, this is the only practical approach because 80% of time it's inline text you need to style.
    – Oliver
    Commented Feb 21, 2020 at 13:54
  • 6
    Great solution! I've figured out you can leave code formatting mode using the hotkeys for, e.g., normal text (CTRL + ALT + 0). But is there a way to get hotkeys to enable this? The only trigger option I have for this script is "OnOpen". Even better, could we do in-line markdown characters to trigger it? (e.g. backticks)
    – BenB
    Commented Apr 1, 2020 at 21:15
  • 1
    this should be the accepted answer!
    – Pierre D
    Commented Oct 21, 2020 at 14:13
17

The workaround I've come to rely on is the Copy/Paste Format feature shortcut. As the format "clipboard" is separate from the text clipboard, it is preserved for as long as you have the document open.

To use:

  • find a section you have formatted as "code", e.g. Consolas 9pt.
  • Use option + command + c or Ctrl + Alt + c or use the Paint Format icon
  • Wherever you need "code" select it and press option + command + v or Ctrl + Alt + v

If you only need one extra style, I find this very little extra work, with no drawbacks, compared to other options. The only work is to initially copy the style to the buffer when opening a new document.


The other technique I use (as I commented on the question) is to invert the problem: always type in "code" format at the end of the document then reset any non-code to normal using `command + option + 0".

When I'm writing technical documents, if 50% of the text is code, then this is also no extra work.

The only tip here is to format the very last linefeed character in the document as "code", else it keeps revering to normal. This is done by arrowing down to the end, then hold shift and right-arrow - it will select one more sneaky character. Then format that as code. Now the end of the document will always be in code by default.

1
  • This does not provide any of the benefits of defining styles. The main purpose of styles is that you don’t have to update every occurrence of styled text when you modify the style. Commented Sep 30, 2022 at 10:50
16

The gratis Google Docs extension Paragraph Styles+ allows to create custom paragraph styles (plus decimal system outline numbering for headings and a table of contents with page numbering).

I just tested it, and here's my first impression:

  • Good start. For small-ish documents, it should be good to use :-)
  • However it runs quite slow, even though recent versions improved on the performance already.
  • The UI does not integrate with the Google Docs style UI. Probably not possible otherwise in the Google Docs API.
  • The original Google Docs style feature does not know about custom styles, considering all text with custom styles as having the "Normal text" format. You can use both in parallel, but note that the "Clear formatting" button (looks like "Tx") will remove custom style formatting from selected text.
  • It seems to store your character styles on an external web service, means you have to entrust them access to your documents the way you entrust them to Google now …

Source: Acknowledging the comment by Chris Moschini above, who mentioned it first.

3
  • 6
    Are there any data security risks involved with installing this kind of docs extension, or has the code been verified by Google to be non-malicious?
    – Gruber
    Commented Aug 19, 2015 at 7:42
  • 13
    These permissions appear to enable, for example, your documents to be sent to or shared with others without further notice. That certainly scares me.
    – Reece
    Commented Mar 21, 2016 at 18:07
  • 3
    It looks like Google now longer allows this extension to be installed.
    – Daniel
    Commented May 19, 2022 at 16:11
10

Until recently, it was possible to edit the CSS (stylesheet) and HTML source of a document. It took some work, but if you knew HTML and CSS you could add a CSS class to your doc, and then edit the HTML to use it, e.g. in the HTML

<span class='booktitle'>Infinite Jest</span>

and in the CSS

.booktitle { font-style:italic; }

Google has more or less phased out this capability with their new format, forcing old docs to the new format. But, I've heard there are ways to keep docs in the old format, so there may be some hope.

3
  • 1
    I would love more info about how to do this.
    – Stew
    Commented Sep 21, 2012 at 16:38
  • 2
    Doesnt work, google now strips all css except inlines
    – vs4vijay
    Commented Jun 13, 2013 at 7:16
  • 2
    The script posted above is almost the same as this feature. After installing, you can simply edit the style object as needed. It's javascript, so slightly different than CSS, but almost identical. Note: it does not make any external calls, send or load data from elsewhere. Commented Jun 8, 2017 at 10:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.