41

I often want to write filenames for code.

Normally I write like this:

Filename

string = "This is example code"

Is this okay? Is there a standard Stack Overflow way of Markdown formatting for writing a filename?

2
  • 4
    meta.stackexchange.com/questions/215472/…
    – random
    Commented Jan 10, 2015 at 21:06
  • 1
    File names can be long and contain dots that might be confused with punctuation, and spaces, as sometimes happens in MS Windows. (Whole file paths can even contain slashes and colons.) Inline code style can prevent the such file names from blending in with surrounding sentence and thereby ruining it. For example: This is a sentence containing a file named oh this name has spaces.and.dots which may be hard to read without the file in inline code style. Italics or bold don't solve the problem well since they don't "bind" the parts of the file name as closely as the code style box. Commented Dec 10, 2018 at 9:46

6 Answers 6

22

This is ok if you write it the way you shown (the filename on one line, then on other lines code following). But if you have something like this:

In my viewcontroller.h I have:

some code there

I personally would like to see it as

In my viewcontroller.h I have:

some code there
4
  • 2
    So viewcontroller.h is the name of the method here as indicated by the code formatting?
    – random
    Commented Jan 10, 2015 at 20:54
  • 1
    @rand no... it is just a bit unreadable without codformatting.
    – nicael
    Commented Jan 10, 2015 at 20:55
  • 1
    What is the actual markdown syntax to create example 2?
    – Kurt
    Commented Feb 15, 2017 at 18:04
  • 2
    Click edit and see yourself, @Kurt :)
    – nicael
    Commented Feb 15, 2017 at 18:25
30

Update

Since July 2021 (apparently) the previous method no longer works. Here's a new method based on tag stripping:

<h5 a><strong><code>hello_world.js</code></strong></h5>

``` js
alert("Hello world!");
```

Here the <h5 a> tag gets stripped leaving its content "naked". Note that an attribute in <h5 a> tag and a blank line after it are required!

Original answer

You can make a very nice-looking named code block using small headers and inline code, like so:

#### **`hello_world.js`**
``` js
alert("Hello world!");
```
hello_world.js
alert("Hello world!");

I've accidentally found it in this post on Formatting Sandbox and just don't want this great trick to get lost there as I was unsuccessfully looking for this some time before.


Though not as good as above, you could also specify a file name via language-specific comment before your code:

# hello_world.sh
echo "Hello world!"
8
  • it seems to produce <blockquote> <code>code.js</code> <pre>...</pre> </blockquote>
    – myrdd
    Commented Jul 28, 2019 at 5:19
  • @myrdd: Not sure what you mean. Blockquote was added by the author, the code produces what's inside blockquote.
    – EvgenKo423
    Commented Jul 28, 2019 at 8:12
  • 2
    Related question and an answer by me explaining, why this seems no be no good idea: meta.stackexchange.com/q/327657/364003 Commented Jul 28, 2019 at 16:03
  • @EvgenKo423 it's simply the HTML code corresponding to the Markdown code (#### + ```)
    – myrdd
    Commented Jul 29, 2019 at 7:09
  • love it, but would be great to have something easy to remember and fast to type :)
    – Kostanos
    Commented Apr 22, 2023 at 19:38
  • Please don't abuse header markup like this. The number of # symbols in front of a line should correspond with the overall structure of the answer (although there is an argument for never using a single # since that is for H1 formatting, and some believe this should only apply to the question title - personally, I think it equally makes sense for the N answers label that's part of the page template, but....), and not simply be chosen for aesthetic purposes. Commented Mar 14 at 22:15
  • @KarlKnechtel Don't worry, no headers have been abused during writing of this answer. =)) Both of these methods don't/didn't result in any headers being generated, they are/were just removed from the markup (level 4+ headers were not supported previously, with a # at least). This is needed to prevent the <code> tag from being wrapped with a <p> tag which has bottom padding. I would happily use the official method if there was one... [1/2]
    – EvgenKo423
    Commented Mar 15 at 6:57
  • Even if the headers would be generated, I think it would still correspond to the structure as they use small headers for small sections (IMO, if headers are needed, they should be added starting from smaller ones). Take a look at the link by MEE above as well for more of my arguments. [2/2]
    – EvgenKo423
    Commented Mar 15 at 6:57
6

I'd never thought about it until seeing nicael's answer, but I kind of like "quoting the file".

test.bat

@echo off
setlocal enableDelayedExpansion
set argument1=!%1!
echo %argument1%>test.txt

file2.bat

@echo off
echo %~1
2
  • 1
    And why posting a new answer, just to repeat what other user answered? Commented Feb 1, 2016 at 21:44
  • 7
    He wasn't literally recommending using quotes. I am. Commented Feb 1, 2016 at 21:45
3

After pounding my own head against my desk (and many others' desks) from wondering about this four years ago, I'm now writing all my Markdown with files as code because...

  1. We indeed need to know when we are working with code-related things or with ideas outside of code; files are part of code.
  2. In Unix & Linux, everything is a file.
  • If it is Unix/Linux code, then it is already a file.
  • Nearly all commands in Unix & Linux are actually files anyway (except cd and a few others).
  • There really isn't a difference between Unix/Linux files and commands except execution permissions.
  • If an executable command file is written with code, then all other non-executable files should also.
  1. Files outside code get messed-up with underscore (_), which many files have in their names.
  • Markdown renders more than one underscore _ as an italics-string (literally _italics-string_ in Markdown).
  • This never causes a problem if file names are in backticks.
1
  • 1
    See this question as well for more arguments on formatting file names and using inline code in general.
    – EvgenKo423
    Commented Mar 17 at 7:12
2

I tend to do something like:

requirements.txt:

elasticsearch==7.0.0
elasticsearch-dsl==7.0.0
-5

Bolding is fine, though I think that the header markup you've used may be a little too forceful. Large text could help, though, if you had a lot of (hopefully small!) snippets in your post. I'm not sure we need a standard for this: use what makes your post most easily read and understood.

I will say, however, that code markup is definitely not the proper choice. The name of a file is not an element of a program; marking it as such is semantically incorrect.

14
  • 2
    "code markup is definitely not the proper choice" - it is hard to read completely unformatted complex file names with extensions when embedded inline, not the file name on one line and nothing else on this line
    – nicael
    Commented Jan 10, 2015 at 20:22
  • 2
    The name of the files are definitely elements of the program - If ArrayUtils.java isn't the name for a class of utility methods for arrays, the asker is doing it wrong. main.js, static.js, etc.
    – Unihedron
    Commented Jan 10, 2015 at 20:41
  • 2
    The currently-chosen formatting is not a justification for markup abuse, @nicael.
    – jscs
    Commented Jan 10, 2015 at 20:42
  • 1
    They are not executable code in themselves, @Unihedro.
    – jscs
    Commented Jan 10, 2015 at 20:43
  • 3
    @JoshCaswell Should XML files of an Android project not be wrapped in code formatting then? They may be part of the program (an essential one), but are not executable.
    – Unihedron
    Commented Jan 10, 2015 at 20:45
  • 2
    What part of a filename is code unless it's used in the code? @uni
    – random
    Commented Jan 10, 2015 at 20:55
  • @random: I never said or implied that they are code, just that they are indeed elements of a program, contrary to what Josh attempted to bring otherwise.
    – Unihedron
    Commented Jan 10, 2015 at 20:58
  • So are we or are we not talking about semantics? @uni
    – random
    Commented Jan 10, 2015 at 21:00
  • 1
    XML is parsed by another program and interpreted, @Unihedro. A bare filename is not.
    – jscs
    Commented Jan 10, 2015 at 21:03
  • 2
    @random: Elements of a program are formatted with `inline code`, so includes standard input, test cases, relevant directories and file names and executables and constants and related configurations. I do it, and I see everyone else do it, and I teach new users to use the site by leaving a comment about using inline styles instead of italics, and they agree. Not sure what being parsed takes significance here, @Josh. Perhaps python2 myprogram.py foo bar...
    – Unihedron
    Commented Jan 10, 2015 at 21:07
  • 1
    Context matters. help.github.com/articles/generating-ssh-keys But why would anyone think italics, which are used for titles or section headings, is first before bold on filenames? @uni
    – random
    Commented Jan 10, 2015 at 21:11
  • 1
    @random Your linked article formatted "Keep in mind that your key may also be named id_dsa.pub, id_ecdsa.pub or id_ed25519.pub" inline with code formatting, that's what I meant because it's related to a keyfile. As for using italics for filenames, I guess they used a "traditional" BBCode forum with no code formatting ever, but I can't be sure.
    – Unihedron
    Commented Jan 10, 2015 at 21:16
  • 1
    The link shows two ways to format filenames. Unformatted when used in text, formatted as code when shown as part of an output or parameter. @uni
    – random
    Commented Jan 10, 2015 at 21:39
  • @jscs "XML is parsed by another program and interpreted, Unihedro. A bare filename is not". - No, it is parsed. When you compile a program, the build system goes through all files in the project and does whatever is appropriate for them, and uses their names to identify the files. So, for example, in a C# WPF program the build system will go through .cs files and compile them, and go through the resource files and copy them to the output folder.
    – KulaGGin
    Commented Jan 12, 2022 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .