Sublime Forum

Shortcut to open file with extension from current folder

#1

Hi!

First post here.
I am working on a project which consists of folders with generally three files in each. One .cpp, one .hpp and one xx_params.c file. To switch from the .cpp to .hpp file I use alt+O, is there a way to configure Sublime to open the xx_params.c file using another shortcut?

0 Likes

#2

The switch_file command is only designed to handle files with the same base name but a different extension. You could copy it/edit it to do what you want - it is implemented in Packages/Default/switch_file.py

0 Likes

#3

Okey great thx! Will have a look :slight_smile:

0 Likes

#4

You can use a plugin to do what you want

import os
import sublime
import sublime_plugin

class OpenCfileCommand(sublime_plugin.TextCommand):

	def run(self, edit):
		base_path = os.path.split(self.view.file_name())[0]
		for file in os.listdir(base_path):
			if file.endswith(".c"):
				c_file_path = os.path.join(base_path, file)
			else:
				self.view.window().status_message("No c file found")
		self.view.window().open_file(c_file_path, flags=sublime.FORCE_GROUP)

and have a keybinding to it (keybinding is upto you)

{
	"keys": ["ctrl+alt+4"],
	"command": "open_cfile",
}

So if you now press ctrl+alt+4 when you have one of your .cpp or .hpp files open, it should open the corresponding .c file located in the same directory (provided thats the only .c file you have otherwise it will open the .c file(s) last in the alphabetical order).

Hopefully this helps.

0 Likes

#5

@UltraInstinct05
Wow yes that worked perfectly, thanks a lot!

0 Likes