CodeProject

Git: Setting Sublime Text as the Default Editor for Git (Linux Mint/Ubuntu)


Setting up Sublime Text 2 (or the new Beta Release of version 3)  as the default editor used by Git is not overly challenging, but not necessarily obvious either. Really, we’re still simply setting up the .gitconfig file with a path to sublime text. However, given that there is not, presently, a standard installation directory for Sublime Text, we first need to know where to point our .gitconfig file. Also, there are some non-obvious flags which need to be set as part of the configuration, or Sublime Text will not work properly as the Git editor.

Installing Sublime Text 2, or installing the beta release of Sublime Text 3 is not too challenging either, but again, to this point neither Linux Mint nor Ubuntu offer Sublime as part of the Synaptic Package Manager for either distro. For additional help on this, refer to my previous posts:

If you used one of the methods above, you should now have a bash script or alias named subl in your /usr/bin/ directory which allows you to refer to Sublime Text as subl from the terminal. We will point the .gitconfig file to this.

Tell Git Where Sublime Text Lives

To set the default editor in our .gitconfig file from the terminal, we can use the following command:

$ git config --global core.editor "subl -n -w"

Notice the –n and –w flags at the end? These arguments are passed to Sublime Text, and essentially tell it to run without loading any previously open windows (the –n flag), and to wait until the user exits to pass execution back to the calling process (the –w flag, for “wait”). In this case, the calling process is Git.

Trouble Shooting

If the above doesn’t work for you, then you may not have the alias or Bash script in your /usr/bin/ directory, or you may have installed Sublime Text to a different directory altogether. In this case, the command is the same, but you need to change it to include either the script name you chase, or the the full-path to Sublime Text itself:

$ git config --global code.editor "<Full Path or Script> -n -w"

Of course, you can always open the .gitconfig file in Sublime Text itself (or any other editor on your system), and edit the file directly, but if you are learning Linux, where’s the fun in THAT? Smile

If you have other issues, feel free to mention them in the comments or email me at the address in the “Author” section of this blog.

That’s it. The biggest issue most people run into with this is not knowing about the –n and –w flags, which must be included in the quotes. Without these flags, Sublime Text will open in response to a prompt from Git, but will immediately return execution to Git. Therefore, your merge edits, interactive rebase, or other such activity will fail, because Git will never see your changes.

Other Articles of Interest to Git or Linux Newcomers

Linux
Find The IP Address of Your Computer on the Home or Local Area Network (LAN) -Linux (Debian-Based Systems)
C#
C#: A Better Date-Masked Text Box
CodeProject
A More Useful Port of the Chinook Database to Postgresql
  • htrahdis

    htrahdishtrahdis

    Author Reply

    Excellent,
    especially the coverage of ‘subl -w -n’