Sublime Forum

Keyboard friendly way to create new files and folders

#1

Hello,

I’m new to ST2. One thing I love about Sublime (and TextMate) is the keyboard friendly goto file functionality to easily open up any file in my project. I just wish it was equally as easy to create new files and folders in my project. I know command+n will create a new tab and command+s will bring up the file save dialogue and that I can navigate to a folder and save the file there, but on OSX this requires using the mouse/trackpad. At the moment I prefer to navigate through the folders in the sidebar and and Option + Click the folder name where I’d want to place my new file or folder in. However, this isn’t so intuitive when compared to the ease and fluidness of command+p to goto and open a specific file throughout the project.

Perhaps a “Goto Folder” function that will navigate the sidebar to the specified folder and activate/highlight it would help. Along with a “Create File/Folder” function.

Example usage:

  1. I press some shortcut to trigger Goto-Folder
  2. I start typing in ‘classes/controllers’ to bring up the controllers folder in the search results
  3. I hit enter to reveal it in the sidebar
  4. I press some shortcut to trigger a Create New File or Folder function
  5. I type in home.php and hit enter which creates a new file in my classes/controller folder and opens it in a new tab. Perhaps the same can be done to create a new sub folder - instead of typing home.php I could type home/ and after hitting enter in this case it will activate that folder in the sidebar after it’s created.

I would repeat the process to create a home.php file in my ‘classes/views’ folder and a home.mustache file in my ‘templates/pages’ folder.

Just my 2 cents.

1 Like

How to get visibility and focus of sidebar?
#2

Although it’s not what your asking for, there is a trick you can usually use to create new files without using the mouse: the save dialog will always start at the same directory as the current file, so if you open a file in the directory of interest, then create a new file and save it, all you’ll need to do is type the file name and press return.

0 Likes

#3

+1 for the ability to go to the project pane, via keyboard, and navigate around.

Actually, I guess its a +0.5 because really I should be using the Go To to get to files, but one habit I have from VIM + NERDTree was to switch to the folder list, since its just a regular buffer, and navigate with the keyboard to look into folders, track down specific files that I might not know the name of, etc.

Just a thought.

0 Likes

#4

@Avenue80dave:

pressing ctrl+0 will add focus to the sidebar.

0 Likes

#5

Hmm, that doesn’t seem to work for me. nor Cmd+0 on mac

0 Likes

#6

I’m on a Mac too. In your global keybindings there should be: { "keys": "ctrl+0"], "command": "focus_side_bar" }

If there’s not, maybe you’re not using the latest version of ST2…

After you give the sidebar focus, navigate with the arrow keys.

0 Likes

#7

I use the following WindowCommand for creating a new file in the folder of the currently open file:

import sublime, sublime_plugin
import os.path
import platform

base_path = None
window = None

def touch(fname, times = None):
  with file(fname, 'a'):
    os.utime(fname, times)

def create_and_open_new_file(filename):
  new_file = os.path.join(base_path, filename)
  touch(new_file)
  window.open_file(new_file)


class NewFileWithCurrentCommand(sublime_plugin.WindowCommand):
  # create a new file in current directory, querying for a filename
  def run(self, extenstions=]):
    global base_path, window

    if not self.window.active_view():
      print "no view"
      return

    fname = self.window.active_view().file_name()
    if not fname:
      print "no fname"
      return

    base_path = os.path.dirname(fname)
    filename = os.path.basename(fname)
    window = self.window

    self.window.show_input_panel("Filename for new file", filename, create_and_open_new_file, None, None)

and the following as keybinding:

  // new file command
  { "keys": "ctrl+shift+n"], "command": "new_file_with_current" },

This is not perfect, but works for me.

0 Likes

#8

Hi jerrybotticelli,

I’ve made a plugin that uses a quick panel with fuzzy matching to locate the directory where you want to create a new file or subdirectory. It’s called Quick File Creator and can be installed from Package Control or directly from here:

github.com/noklesta/SublimeQuickFileCreator.

Unfortunately, ST2 doesn’t have an API to show the contents of a folder in the sidebar (that would be a very nice addition!), but it’s still close to what you ask for.

Anders

0 Likes

#9

[quote=“noklesta”]Hi jerrybotticelli,

I’ve made a plugin that uses a quick panel with fuzzy matching to locate the directory where you want to create a new file or subdirectory. It’s called Quick File Creator and can be installed from Package Control or directly from here:

github.com/noklesta/SublimeQuickFileCreator.

Unfortunately, ST2 doesn’t have an API to show the contents of a folder in the sidebar (that would be a very nice addition!), but it’s still close to what you ask for.

Anders[/quote]

Wow this is a game changer for me. Until now I’ve been using Terminal (via Total Terminal) as a keyboard shortcut to navigate to my projects directories to create files and folders. Yeah it would be nice if the sidebar could expand the directory the file/folder was created in. Oh well, this is still awesome. Thanks bro, you’re the man.

0 Likes

#10

what’s that file format?

0 Likes