9

Is there a way to set a default document type when saving a NEW FILE? I created several new files and I want to have a default value of .txt when saving a NEW FILE.

Ravi Ram
  • 24,078
  • 21
  • 82
  • 113

3 Answers3

7

Create a new plugin Tools > Developer > New Plugin...

Paste this in:

import sublime, sublime_plugin

class EverythingIsPowerShell(sublime_plugin.EventListener):
   def on_new(self, view):
      view.set_syntax_file('Packages/PowerShell/Support/PowershellSyntax.tmLanguage')

Save and call it NewTabSyntax.py. New tabs will now default to Powershell.

You can change the syntax to whatever you prefer. To find out the "path" of a particular syntax, simply open a file of that syntax, open the console (View > Show Console) and type:

view.settings().get('syntax')
CrazyTim
  • 6,695
  • 6
  • 34
  • 55
Jaykul
  • 15,370
  • 8
  • 61
  • 70
5

This plugin does it:

https://github.com/spadgos/sublime-DefaultFileType

seems pretty great.

Edit:

Ok, two things, there currently seems to be a small bug so the text file syntax is not being correctly picked up due to the whitespace in the filename. In addition you need to set the "use_current_file_syntax" to false, (otherwise the new file will default to whatever filetype you have open already when you hit Ctrl-N)... So the fix/workaround is this:

Put the following code in: Packages/User/default_file_type.sublime-settings

{ "default_new_file_syntax": "Packages/Text/Plain_text.tmLanguage",   
"use_current_file_syntax": false }

NOTE THE UNDERSCORE.

Next, find the "Plain text.tmLanguage" file and copy and rename it (in the same folder) as "Plain_text.tmLanguage". [be sure to copy/duplicate it, do not just rename it, as it may have dependancies]

Restart, just to be sure, and this should do the trick. Also note this plugin only works for new files created with Ctrl-N.

fraxel
  • 34,470
  • 11
  • 98
  • 102
  • I have installed and configured the settings to `{ "default_new_file_syntax": "Packages/Text/Plain text.tmLanguage", "use_current_file_syntax": true }` but it does NOT save the file as '.txt'. The file is saved without an extension at all. – Ravi Ram Mar 07 '12 at 13:58
  • @David K Egghead - seemingly small bug in ST2 for getting txt syntax - fix/workaround added above as edit. – fraxel Mar 08 '12 at 09:59
  • I have completed the above fixes. Unfortunately, after creating a `Ctrl-N (new file) > adding some text > Ctrl-S (save file) > the 'Save as type' is [All Files(*.*)] ` which is NOT Defaulting to '.txt'. . I did NOTICE that I now have (2) 'Plain Text (*.txt)' in the 'Save as type' dropdown. – Ravi Ram Mar 08 '12 at 15:13
  • @David K Egghead - hmm, works fine for me. Have you definetly put the `default_file_type.sublime-settings` in the `Packages/User` folder? And does it only have the above code in it? – fraxel Mar 08 '12 at 15:25
  • @David K Egghead - puzzling. Some further ideas: Check that the "DefaultFileType" plugin is installed and operating correctly/at all. Add `{ "keys": ["ctrl+n"], "command": "default_file_type" }` to your user key bindings file (to ensure that the plugin is being activated correctly, on ctrl-n press). Also you could try swapping to "Packages/Java/Java.tmLanguage" to see if other languages work at all.. you could also try changing the key binding just to the letter 'p', just to check the plugin is doing anything. – fraxel Mar 08 '12 at 16:04
1

Working after these steps:

1.Uninstalled

2.Installed using Package Control

3.Test using default install (type Jave) <-- worked

4.Copy and Renamed file Sublime Text 2\Packages\Text\Plain text.tmLanguage > Sublime Text 2\Packages\Text\Plain_text.tmLanguage

5.Changed file Sublime Text 2\Packages\Default File Type\default_file_type.sublime-settings >

`{ "default_new_file_syntax": "Packages/Text/Plain_text.tmLanguage", "use_current_file_syntax": true }`

-- All working.

I did not need to copy any files into the 'Packages/User' folder

@fraxel _ Thanks for all the help and quick response.

Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
  • 1
    Glad its working for you. Feel free to accept my answer, or up vote it if it was helpful to you (it was, wasn't it?). Also if you don't copy your settings to the 'Packages/User' folder, you will lose them if the plugin is ever upgraded. – fraxel Mar 08 '12 at 16:59