Git is a very popular version control system and Sublime Text is very popular text editor tool. If you are new to sublime text editor then I recommend you to read Sublime Text Plugins, Tips and Settings.This post explains step by step on how to use Git plugin into Sublime Text editor to increase development productivity.
When I do a git diff in my branch I see that line endings have the characters ^M:
I want to replace these with Unix line endings before committing my changes.
I am using Ubuntu 14.04, git and sublime text 3.
Thanks for your help.
EDIT:
I am looking for a way to change line endings in sublime text 3 not using other utilities or commands.
5 Answers
You can use dos2unix
command in linux (package dos2unix) or change line endings in Sublime Text under menu item View / Line Endings.
This can be done without a plugin via the menu at the top or though the statusbar (but you need to enable the feature).
Sublime Text Git Integration
To change it through the menu at the top click View -> Line Endings -> Select desired line ending type.
To enable this through the status bar:Click Preferences -> SettingsThen add 'show_line_endings': true,
somewhere between the {}
brackets on the right-hand side.Finally, save your changes.
This enables a small section on the right of the status bar that shows up at the bottom. It tells you the current EOL encoding and by clicking the box and selecting a different type it changes the files EOL character for you.
Another helpful option is file encoding which can be enabled in the statusbar as well by following the same setups above but also adding 'show_encoding': true,
between the {}
marks.
^M
is r
(Control-M, or carriage return).
This is likely because the user who committed the file was using windows, which uses rn
for its line endings. Linux uses only n
, hence you see the ^M
at the end of the line.
You probably want to do one of the following:
- 'Fix' the file to have
n
line endings (using a tool such asdos2unix
) and commit. In which case you'll be fighting with the other user unless you can get them to do the below. - Configure both of your git installations to handle line endings correctly for each environment - e.g: https://stackoverflow.com/questions/10418975/how-to-change-line-ending-settings
- Configure Sublime to open the file with 'Windows line endings'
I found a sublime text plugin that converts line endings in all open files to Unix/Windows/Mac OS 9.
Installation
- Open Sublime and go to Preferences > Browse Packages. This will opena window.
- Download or clone the contents of this repository to a folder namedexactly as the package name into the Packages/ folder you opened in the previous step:
/home/YOUR_USERNAME/.config/sublime-text-3/Packages
Usage
- Go back to sublime and open all the files that you want to convert.
- Once all of the files are open hit CTRL + SHIFT + P, and search andselect “Line Ending to all view: Unix”.
- Hit CTRL + ALT + S to save all of the open files.
To whom may not want to install additional plugin to remove ^M new lines,
In sublime text setting, add
'trim_trailing_white_space_on_save': true,
to auto remove space when saving. (This step might be optional)On the file you are opening, click right bottom corner of sublime text. Change
Windows
toUnix
and save.
- ^M is gone. Enjoy.
Not the answer you're looking for? Browse other questions tagged gitubuntu-14.04sublime-text-3 or ask your own question.
I have a problem setting Sublime Text 2 as the core.editor
with git
.
I've read through every post I could find addressing the problem, but still nothing is working for me. I am running Windows.
I have done:
and tried that with various arguments like -m
. When I open my .gitconfig
, this is what is in there:
If I go to Git and type:
the README file opens in Notepad, not Sublime Text.
Does anyone have any idea what I'm doing wrong or how I could fix this?
Spencer MoranSpencer Moran15 Answers
Windows
Sublime Text 2 (Build 2181)
The latest Build 2181 just added support for the -w
(wait) command line argument. The following configuration will allow ST2 to work as your default git editor on Windows. This will allow git to open ST2 for commit messages and such.
Sublime Text 3 (Build 3065)
Sublime Text 3 (Build 3065) added the subl.exe
command line helper. Use subl.exe -h
for the options available to you. I have hot_exit: true
and remember_open_files: true
set in my Sublime Text user settings. I have found the following to git config to work well for me.
Mac and Linux
Set Sublime as your editor for Git by typing the following command in the terminal:
git config --global core.editor 'subl -n -w'
With this Git config, the new tab is opened in my editor. I edit my commit message, save the tab (Ctrl+S) and close it (Ctrl+W).
Git will wait until the tab is closed to continue its work.
chicksFor Mac OS X in the file ~/.gitconfig under [core] I had to put this code to solve the issue on my end.
This was when subl was working fine but git was unable to access it. And was displaying these errors
Ahmad AwaisAhmad AwaisFor Mac & Sublime Text 3:
If git config --global core.editor 'subl -n -w'
doesn't work, use this git config
command:
Note: it does not contain escaping backslashes.
what worked for me in cygwin/zsh:
in /usr/local/bin create subl_git file
in gitconfig:
With Sublime Text 2 and the following configuration, if nothing happens when you close the git commit's comment file :
If the git commit doesn't finish when you close the COMMIT_EDITMSG comment file in Sublime, I've found this is probably because you are running CMD in administrator mode (and have a Sublime Text already opened in non admin mode). I had the same problem and it worked for me when I started my CMD in normal mode.
Ps : just wanted to answer to the problem @david pointed out in the accepted answer's comments, but I couldn't post comment since I'm new here... so I've made a new answer, because I think this can be useful to some people encountering the problem.
I just successfully made it with the help of PowerShell (command line tools for windows, which is also be use as the default command line on GithubforWindows)
Just make a folder 'WindowsPowerShell' on 'C:Users%username%My Documents'.Then, create a file called 'profile.ps1'.Make sure that you have the correct file extension, .ps1 and not .txt.inside the profile.ps1, put this line to set an alias command:
Make sure the sublime directory on your machine is correct.You can also see the configuration details in here.
Now, I can edit files with Sublime Text 2 via PowerShell command just by typing:
Setting Sublime Text For Git In Machine
Hopefully this is usefull...
I've faced the same problem with git editor in Windows this evening, and finally I've got it.
You'll need to set your editor path with the bash syntax and using the DOS 8.3 format to avoid spaces:
This doesn't work (for me):
This do:
You can get the DOS 8.3 name for a directory with dir /x
I was just struggling with this for some reason, none of the examples above quite worked for me. This was my solution however:
You specifically need the ' '
around the path and the $*
at the end of the args, all in ' '
.
well, i had some trouble invoking notepad++ from gitbash even after setting it as my core.editor.
i got it all solved by appending the below line to my .bashrc file in my gitbash installation directory.
so, i can invoke notepad++ with the alias 'npp'. i feel this can help you if you change the directory to the respective editor you want to use.
For MAC Users I can say for Sublime use the following:
First Check the .gitconfig file using command:
cd ~
ls -la
cat .gitconfig
Now one can add this configuration:
Where l1
will tell to start at line 1.
For Windows 7 I have found the following solution works:
On Windows, if you are installing git, you can select other editor and give the following command line.C:Program FilesSublime Text 3sublime_text.exe -w
Xavier JohnXavier JohnI haven't seen any instances of updates regarding Windows allowing Git installation to actively pick Sublime Text as your default text editor without all the sturm und drang of the original override codes...