FAQ Desk: To Add Extensions Automatically or Not To Add Extensions Automatically



  • Hello, and welcome to the FAQ Desk.

    You have been directed here because you have asked a question about automatically adding extensions or a related subject: perhaps something like “How do I always save my text files with .txt extension?”

    As described in the official documents, there is a setting Settings > Preferences > Default Directory > ☐ Use new style dialog (without file extension feature & Unix style path capacity):

    If you want to have the extension auto-applied, you have to disable this option.

    Details

    The Microsoft Windows API allows for two styles of open/save dialogs (derived from the docs):

    • “old style” has “Quick Access”, “Desktop”, and others as icon-buttons along the left; it will auto-apply the selected file-type’s extension to the file (so typing a filename of blah when a file type of “Normal text file (.txt)” is selected will save blah.txt); it will also allow using Unix-style backslashes as a path separator, rather than Windows-style forward slashes.
    • “new style” does not have the icon-buttons (instead, Windows presents those in the tree, similar to other drives and folders); even when a file-type is selected, the user is required to specify the desired file extension (thus the file-type selector is primarily for filtering what existing files are listed in the directory listing); and it will not understand Unix-style backslashes as path separators.

    Warning

    If you are using the old-style dialog, be wary of that “feature” from Windows… because whatever happens to come first in the list of extensions for a given dropdown is what Windows will apply; hence, if you type blah and apply c++ source file, there’s a good chance that you’ll save blah.h rather than blah.cpp, which is probably not what you want.

    Note that Microsoft Windows introduced the “new style” dialogs with Windows Vista in 2006, and Windows XP was thus the last time that Microsoft recommended the “old style”.

    I always use the new-style, because the old-style was obsoleted by Windows’s new-style dialog many versions of Windows ago (and could theoretically be eliminated from the Windows codebase in the future, at which point this option will cease to work in Notepad++), and (more importantly) because the new-style encourages you to take control of your destiny by controlling extension yourself rather than relying on Windows to fill in the extension for you, and (most importantly to me) because it enables the tree-view to the left rather than just having the four or five icons for windows-preferred locations, and making it more clicks to find the location you want.

    You are allowed to use the “old style” if that’s what you want, but please be aware of the risks.

    Version 7.8.7 And Newer

    As of Notepad++ v7.8.7, both the “old style” and “new style” apply default extensions. The default extension will be the first extension in the list for the active file type (lexer language). (For example, if you create a new file and assign Language > Python, then it will default to extension .py.)

    If you have an extension that you usually want applied to Normal Text files, you can edit %AppData%\Notepad++\langs.xml, go to the
    <language name="normal"...> entry, and edit it. The ext="..." parameter uses a space-separated list of extensions (without the dot). So, for example, suppose you usually want new text files to be something.main, and occasionally something.xyzzy or something.txt, you could set that line to

            <Language name="normal" ext="main txt xyzzy" />
    

    (To edit this and other config files, follow the steps in the official docs at https://npp-user-manual.org/docs/config-files/#editing-configuration-files, otherwise your changes might be overwritten.)

    After saving langs.xml and restarting Notepad++, a new Normal Text file will default to the extension .main, but you can manually type an extension of .xyzzy or .txt and it will still accept that as a valid extension (and not try to also add .main after it, as sometimes happens when typing non-standard extensions in Windows’ Save-As dialogs).

    Alternately, you could use a macro which temporarily changes the new file’s type (lexer language) for the active file to a dummy UDL, then saves the file (prompting you for a name, with All Files (*.*) selected), then changes the file type back to Normal Text. This macro can be added to shortcuts.xml in the <Macros> section, and should be a good starting spot for you; you can change the keyboard shortcut before saving the config file, or after reloading Notepad++ by using the Shortcut Mapper.

            <Macro name="SaveAsAnyExtension" Ctrl="yes" Alt="yes"
    Shift="yes" Key="38"><!-- Ctrl+Alt+Shift+Up -->
                <Action type="2" message="0" wParam="46180" lParam="0"
    sParam="" /><!-- Language > User-Defined -->
                <Action type="2" message="0" wParam="41006" lParam="0"
    sParam="" /><!-- file | save -->
                <Action type="1" message="2170" wParam="0" lParam="0"
    sParam=" " /><!-- type a space -->
                <Action type="0" message="2326" wParam="0" lParam="0"
    sParam="" /><!-- backspace: these make it so "save" will work" -->
                <Action type="2" message="0" wParam="46016" lParam="0"
    sParam="" /><!-- Language > Normal (IDM_LANG_TEXT) -->
                <Action type="2" message="0" wParam="41006" lParam="0"
    sParam="" /><!-- file | save -->
            </Macro>
    

Log in to reply