36

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
  • 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. Dec 10, 2018 at 9:46

4 Answers 4

19

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
    Jan 10, 2015 at 20:54
  • 1
    @rand no... it is just a bit unreadable without codformatting.
    – nicael
    Jan 10, 2015 at 20:55
  • 1
    What is the actual markdown syntax to create example 2?
    – Kurt
    Feb 15, 2017 at 18:04
  • 2
    Click edit and see yourself, @Kurt :)
    – nicael
    Feb 15, 2017 at 18:25
25

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!"
4
  • it seems to produce <blockquote> <code>code.js</code> <pre>...</pre> </blockquote>
    – myrdd
    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
    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
    – MEE
    Jul 28, 2019 at 16:03
  • @EvgenKo423 it's simply the HTML code corresponding to the Markdown code (#### + ```)
    – myrdd
    Jul 29, 2019 at 7:09
5

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? Feb 1, 2016 at 21:44
  • 4
    He wasn't literally recommending using quotes. I am. Feb 1, 2016 at 21:45
-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
    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
    Jan 10, 2015 at 20:41
  • 2
    The currently-chosen formatting is not a justification for markup abuse, @nicael.
    – jscs
    Jan 10, 2015 at 20:42
  • 1
    They are not executable code in themselves, @Unihedro.
    – jscs
    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
    Jan 10, 2015 at 20:45
  • 2
    What part of a filename is code unless it's used in the code? @uni
    – random
    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
    Jan 10, 2015 at 20:58
  • So are we or are we not talking about semantics? @uni
    – random
    Jan 10, 2015 at 21:00
  • 1
    XML is parsed by another program and interpreted, @Unihedro. A bare filename is not.
    – jscs
    Jan 10, 2015 at 21:03
  • 1
    @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
    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
    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
    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
    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
    Jan 12 at 22:00

You must log in to answer this question.

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