Sublime Forum

What happened to "clone_file" in Sublime text 3?

#1

This command used to work fine in Sublime Text 2:

self.window.run_command('clone_file')

Somehow it doesn’t work anymore. Is there a way to make it work or an alternative?

Thank you.

0 Likes

#2

Update: The below code is for a different functionality of creating a duplicate of the current file and opening it for you. I believe the command “clone_file” still exists in ST3 but now with the more clear menu caption “File->New view into File”.
Use the below code only if you would like “Duplicate File” functionality.

Create a file SublimeDataDir\Packages\User\Main.sublime-menu:

	{
		"caption": "File",
		"mnemonic": "F",
		"id": "file",
		"children":
		
			{ "command": "duplicate_file", "caption": "Duplicate File"},
		]
	}
][/code]
Create a file SublimeDataDir\Packages\User\DuplicateFile.py:
[code]import sublime, sublime_plugin
import os
import shutil

class DuplicateFile(sublime_plugin.WindowCommand):
	def run(self):
		self.window.run_command("save")
		self.window.show_input_panel("Duplicate File Name:", "", self.on_done, None, None)
		pass

	def on_done(self, userinput):
		file1 = self.window.active_view().file_name()
		file2 = os.path.dirname(file1) + "\\" + userinput
		shutil.copy2(file1, file2)
		self.window.open_file(file2)
		sublime.status_message("Created Duplicate File: " + file2)
0 Likes

#3

Submit clone view works fine.

sublime.active_window().run_command(‘clone_file’)

@robertcollier4, clone is not duplicate. Clone will open the same file in another view. so you can have many tabs with the same content. Useful for multi monitor set-ups and people that use different layouts.

If you can paste the code maybe we can catch why window is not set in self.

0 Likes

#4

Hm… This doesn’t work for me. Here is the full code:

class clone_tab_in_new_window(sublime_plugin.WindowCommand):
    def run(self):

        if self.window.num_groups() == 1:
            self.window.run_command('set_layout',
                            {
                                "cols": [0.0, 0.5, 1.0],
                                "rows": [0.0, 1.0],
                                "cells": [0, 0, 1, 1], [1, 0, 2, 1]]
                            })

            sublime.active_window().run_command('clone_file')            
            sublime.active_window().run_command('move_to_group', {"group": 1})

Any idea why it doesn’t work?

0 Likes

#5

I think set_layout command change the active group, so after this command you are on the group 1.
You need to focus the group 0 before the move_to_group command.
This can be done using the api or using a command:

class ExampleCommand(sublime_plugin.WindowCommand): def run(self): if self.window.num_groups() == 1: self.window.run_command('clone_file') self.window.run_command('set_layout', { "cols": [0.0, 0.5, 1.0], "rows": [0.0, 1.0], "cells": [0, 0, 1, 1], [1, 0, 2, 1]] }) self.window.run_command('focus_group', {"group": 0}) self.window.run_command('move_to_group', {"group": 1})

0 Likes

#6

See also [BUG] window.set_layout changes active group.

0 Likes

#7

This will move the current tab to dual view.

class DualViewMoveTo(sublime_plugin.WindowCommand):
	def run(self):
			self.window.run_command('set_layout', { "cols": [0.0, 0.5, 1.0], "rows": [0.0, 1.0], "cells": [[0, 0, 1, 1], [1, 0, 2, 1]] })
			self.window.run_command('focus_group', { "group": 0 })
			self.window.run_command('move_to_group', { "group": 1 })
0 Likes