Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indicate programming language in a file without extension

When writing executable scripts, and declarative configuration files that use a common language (eg. Python), I often find it undesirable to add an extension to the file name. Many syntax-highlighting text editor (eg. Geany) are subsequently unable to automatically determine the filetype.

Is there any standard method for indicating to editors the type of source in the file?

like image 581
Matt Joiner Avatar asked Dec 31 '09 05:12

Matt Joiner


People also ask

How to identify file without extension?

You can check the file extension from the Type column in Windows file explorer. Alternatively, you could right-click on the file and select Properties. You'll see the Type of file in the General tab of file properties. If it says File, you know that the file has no extension.

What is a file with no extension?

What about files with no extension? Unlike the Macintosh which embeds creator information into files so they can have just about any name, a PC still mostly uses file extensions to associate programs with files. But, what do you do with a file that has no extension? The simple answer is: punt.

How to select code language in visual studio code?

Press Ctrl+Shift+P to bring up the Command Palette then start typing "display" to filter and display the Configure Display Language command. Press Enter and a list of installed languages by locale is displayed, with the current locale highlighted.

What is programming language extension?

An extension language is a programming language interpreter offered by an application program, so that users can write macros or even full-fledged programs to extend the original application.


2 Answers

Vim

Vim has a concept called a modeline. A modeline is a specially formatted line either withinin the first or last 5 lines of the textfile, which allows you to :setlocal local variables. For example, for C:

 /* vi: set filetype=c fileencoding=UTF-8 shiftwidth=4 tabstop=4 expandtab */

or Ruby:

 # vi: set filetype=ruby fileencoding=UTF-8 shiftwidth=2 tabstop=2 expandtab

Some more documentation.

Emacs

Emacs has a similar concept, called File Variables.

File Variables are either specified at the beginning of the file (in the first line, or if there is a shebang line, then in the second) in this form:

/* *-* mode: cc c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil *-* */

or at the end:

# Local Variables:
# mode: ruby
# coding: utf-8
# c-basic-offset: 2
# tab-width: 2
# indent-tabs-mode: nil
# End:

jEdit

jEdit calls this buffer-local properties. The have to sit within the first or last 10 lines and look like this:

# :mode=ruby:indentSize=2:tabSize=2:noTabs=true:

jEdit also uses the shebang line as a fallback for mode detection.

Komodo Edit

There is a plugin called Komode (pun intended) which adds modeline support to Komodo Edit:

# komode: le=unix language=ruby codepage=utf8 tab=2 notabs indent=2

It also understands a limited subset of Vim modelines.

Others

A lot of other editors also have either their own variants of this, or support one of the above (usually Vim).

Python / Ruby encoding

Both Ruby 1.9 and Python require that the encoding for non-ASCII source files be explicitly specified. Fortunately, they do this in a way that is compatible with both Emacs and Vim modelines. (Basically, they look for the string coding followed by a non-word character followed by whitespace followed by a valid encoding name. Both Vim's fileencoding= and Emacs' coding: satisfy these requirements.)

Modeline Generator

Here is a simple modeline generator, which generates modelines for Vim, Emacs and jEdit.

like image 153
Jörg W Mittag Avatar answered Nov 15 '22 20:11

Jörg W Mittag


Typically the shebang line is used as a fall-back.

For example, a Ruby script without an extension would begin with:

#!/usr/bin/env ruby
like image 30
Bob Aman Avatar answered Nov 15 '22 20:11

Bob Aman