Skip to content

Instantly share code, notes, and snippets.

@giladshanan
Last active February 20, 2019 20:54
Show Gist options
  • Save giladshanan/35ab5d693915adb6002ff06fb06204e7 to your computer and use it in GitHub Desktop.
Save giladshanan/35ab5d693915adb6002ff06fb06204e7 to your computer and use it in GitHub Desktop.
Commands and keybindings for improved git workflow in Sublime using SublimeGit (https://github.com/SublimeGit/SublimeGit).

Installation:

  1. Install SublimeGit
  2. Select Preferences > Key Bindings and paste the contents of sublime-keymap into Packages/User/Default (OSX).sublime-keymap
  3. Save the rest of the python command files to a new directory, Packages/SublimeGitColumnView

Usage:

Ctrl+C -> Open the Git Status view.

L (from the Git Status view) -> Open the Git Log view for the 10 most recent commits in the bottom panel.

D (from the Git Status view) -> Open the Git Diff view in the right column.

N or P (from the Git Diff view) -> Easily switch from one Diff view to the next.

Return (from the Git Diff view) -> Open Diff'ed file in the left column for editing.

Ctrl+C (from the Git Status view) -> Close the git status view, git commit panel, and all diffs. Return to single column view.

Custom Git Commands:

SublimeGit makes it really easy to add your own custom git commands and see the output in a new tab or panel in Sublime. See GitLogColumn.py for an example, and check out the docs for more info.

import sublime
import sublime_plugin
class CloseGitStatusCommand(sublime_plugin.WindowCommand):
def run(self):
# close all tabs in the right hand column
for v in self.window.views_in_group(1):
g, view_index = self.window.get_view_index(v)
self.window.run_command("close_by_index", { "group": g, "index": view_index})
# close the git status view
self.window.run_command("close")
# return to single column layout
self.window.set_layout({
"cells": [[0, 0, 1, 1]],
"cols": [0.0, 1.0],
"rows": [0.0, 1.0]
})
# close the git commit panel
self.window.run_command("hide_panel")
import sublime
import sublime_plugin
class GitDiffInNewPaneCommand(sublime_plugin.WindowCommand):
def run(self):
# set 2 column view
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]]
})
# open the diff
self.window.run_command("git_status_diff")
# move the diff tab to the right hand column
self.window.run_command("move_to_group", {"group": 1})
import sublime
import sublime_plugin
class GitLogColumnCommand(sublime_plugin.TextCommand):
def run(self, edit):
# open log file in panel
command = "log --pretty=format:'%h - %s (%cr) by %an' -10"
self.view.window().run_command("git_custom", { "cmd": command, "output": "panel", "async": False})
import sublime
import sublime_plugin
class NextInGroupCommand(sublime_plugin.WindowCommand):
def run(self):
# get all tabs in the right column
views_in_group = self.window.views_in_group(1)
next_index = self.window.get_view_index(self.window.active_view())[1] + 1
if next_index > len(self.window.views_in_group(1)) - 1:
next_index = 0
next_tab = views_in_group[next_index]
self.window.focus_view(next_tab)
import sublime
import sublime_plugin
class OpenFromDiffCommand(sublime_plugin.TextCommand):
def run(self, edit):
# build filename
repo = self.view.settings().get('git_repo')
path = self.view.settings().get('git_diff_path')
filename = "/".join([repo, path])
# switch to left column
self.view.window().focus_group(0)
# open file
self.view.window().open_file(filename)
import sublime
import sublime_plugin
class PrevInGroupCommand(sublime_plugin.WindowCommand):
def run(self):
# get all tabs in the right column
views_in_group = self.window.views_in_group(1)
prev_index = self.window.get_view_index(self.window.active_view())[1] - 1
if prev_index < 0:
prev_index = len(self.window.views_in_group(1)) - 1
prev_tab = views_in_group[prev_index]
self.window.focus_view(prev_tab)
import sublime
import sublime_plugin
class ShowGitStatusInNewPaneCommand(sublime_plugin.WindowCommand):
def run(self):
# switch to left column and run git status
self.window.focus_group(0)
self.window.run_command("git_status")
// Open and close Git Status
{ "keys": ["ctrl+c"], "command": "show_git_status_in_new_pane"},
{ "keys": ["ctrl+c"], "command": "close_git_status",
"context":
[
{ "key": "selector", "operator": "equal", "operand": "text.git-status" }
]
},
// Open "git_status_diff" in right column
{ "keys": ["d"], "command": "git_diff_in_new_pane",
"context": [
{ "key": "selector", "operator": "equal", "operand": "meta.git-status.line"}
]
},
// Open file from diff command
{ "keys": ["enter"], "command": "open_from_diff",
"context": [
{ "key": "selector", "operator": "equal", "operand": "source.git-diff"}
]
},
// Navigate between open diff tabs in the right column
{ "keys": ["n"], "command": "next_in_group",
"context": [
{ "key": "selector", "operator": "equal", "operand": "source.git-diff"}
]
},
{ "keys": ["p"], "command": "prev_in_group",
"context": [
{ "key": "selector", "operator": "equal", "operand": "source.git-diff"}
]
},
// Git Log
{ "keys": ["l"], "command": "git_log_column",
"context": [
{ "key": "selector", "operator": "equal", "operand": "meta.git-status"}
]
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment