Hi ST forum! I’m new here, so I apologize if this is the wrong section or something.
Previously, if you wanted to edit the same file in two different views simultaneously side-by-side, you would need to:
- Go to File -> New View Into File
- Split the view, i.e. alt+shift+2
- Drag the copied file into the new view
- (To undo it) Close the file (getting a save prompt for some reason), and reset the view layout to 1
So I wrote this plugin to do all that for me. I have the command mapped to f1:
*.sublime-keymap:
[
...
{ "keys": ["f1"], "command": "clone_file_to_new_view"},
...
]
So pressing f1 does one of the following, depending on the context:
- If only 1 group is open, set the layout to 2 views side-by-side and clone the active view to the second one.
- If 2 groups are open and the same file is active in both, remove the copy. If there are no other tabs in the second group, set the layout back to 1 group
- If 2 groups are open and the same file is in both, but not necessarily active, focuses on the correct tab in the second group
If 2 groups are open and the same file isn’t in both, copies the active view into the second group
Apologies for this nasty code this was the easiest way I knew how to get the functionality I wanted.
Tools -> Developer -> New Plugin … (save as clone_file_to_new_view.py):
import sublime
import sublime_plugin
class CloneFileToNewViewCommand(sublime_plugin.WindowCommand):
def run(self):
if (self.window.num_groups() < 2):
self.window.run_command('clone_file')
self.window.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})
else:
view0 = self.window.active_view_in_group(0)
view1 = self.window.active_view_in_group(1)
# Same file open in each of the two windows, cull to 1 if possible
if (view0.file_name() == view1.file_name()):
self.window.focus_view(view1)
view1.set_scratch(True)
view1.close()
self.window.focus_view(view0)
# just deleted the only window, fix layout
if (len(self.window.views_in_group(1)) == 0):
print("NO MORE")
self.window.set_layout({
"cols": [0.0, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1]]})
# Two views, but not the same file
else:
othergroup = 0
if (self.window.active_group() == 0): othergroup = 1
# Check if a copy already exists in 'othergroup' and just
# focus on it if so
otherviews = self.window.views_in_group(othergroup)
for view in otherviews:
if (view.file_name() == self.window.active_view().file_name()):
self.window.focus_view(view)
return
# No existing views found - make a new one
self.window.run_command('clone_file')
self.window.run_command('move_to_group', {'group': othergroup})