Set textwidth on hg commit messages? - vim

I use vim to write my Mercurial commit messages and I would like it to automatically wrap my text when it hits a specific limit, say 80 characters. My git setup already does this, but I'm not sure how to make hg behave in the same way.
How do you set the textwidth used in vim when writing hg commit messages?

You can define the command to run when you do a commit by setting the editor option inside the [ui] section of your .hgrc config file. To set the textwidth in vim you could do:
[ui]
...
editor = vim -c ":set textwidth=80"

Vim should already recognize hg commit messages. To set specific options for that filetype, create a file $HOME/.vim/after/ftplugin/hgcommit.vim (create non-existing directories) and set your option there:
setlocal textwidth=80
Then Vim should pick up your new settings, provided that your .vimrc enables filetype plugins by e.g. filetype plugin on or similar.
See also the faq Question 26.3 and Question 26.6

I like to enable spell check and set my text width to 72 for hg commit messages.
Modify your .vimrc file to include a line like the following:
autocmd BufNewFile,BufRead hg-editor-*.txt setlocal spell textwidth=72
This works with pre-7.4 versions of Vim (I'm currently on 7.2).

Most modern Vim installations will know about the hgcommit filetype. In that case, you can simply add the following to your .vimrc file:
autocmd Filetype hgcommit setlocal textwidth=72
Note
This solution supports other Vim settings as well. For example, I use the following line, which also turns on spellchecking:
autocmd Filetype hgcommit setlocal spell textwidth=72

Related

How can I apply vimrc conf file in .py

when I run
vim good.html
and
:verbose set et?
expandtab
Last set from ~/.vimrc
but when I run
vim good.py
and
:verbose set et?
expandtab
Last set from /usr/share/vim/vim74/ftplugin/python.vim
I want apply ~/.vimrc file in .py, not python.vim
Yesterday I all is fine but today suddenly path was changed
please someone teach me how can I change the path
Put this into your .vimrc:
autocmd VimEnter *.py set expandtab
or if you want to have the configuration of .vimrc to be executed after all plugins being loaded - in case they have changed some settings -, you can add this line:
autocmd VimEnter * source ~/.vimrc
Note: It could have a side-effect depending on the content of your .vimrc because the latter actually will be executed twice (at the begining and at the end of vim startup) so you need to consider that.
Concerning Plugins now, if they are logged in some specific folders like .vim or vim installation path they will be loaded automatically unless you removed them or run some specific commands to be ignored.
Vim has also the ability to detect the type of file which is being edited, and this occurs when the option filetype is activated and probably this is what happened to you.
So typing :filetype will confirm that. Maybe you can desactivate it for some specific files if you wish. It is up to you !
:help VimEnter
VimEnter
VimEnter After doing all the startup stuff, including
loading .vimrc files, executing the "-c cmd"
arguments, creating all windows and loading
the buffers in them.

Creating multiple .vimrc for multiple languages?

I am starting to learn programming and saw that it is possible to configure vim through the .vimrc file. But I'm having trouble creating more than one setting. I want a configuration for java and another for C, but I do not know how to only enable a configuration when programming in a particular language.
I found the following solution to my problem:
If it's okay to configure the local exceptions centrally, you can put
such autocmds into your ~/.vimrc:
:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4
(How to load different .vimrc file for different working directory?)
But I did not understand that solution.
I figured I should do the following:
I wanted to create a C language setting on the Desktop. So I put in
Shell (I'm using Mac OS) the following command vim
~/Desktop/.vimrc and put the desired configuration in this file.
Then I put the following command in the file ~/.vimrc: autocmd
BufRead,BufNewFile ~/Desktop/.vimrc setlocal ts=4 sw=4
Then I went into the Shell and created a C file vim ~/Desktop/myprogram.c, but I realized that my setup had not worked.
Obviously I did something wrong, but I could not find the error, because I'm still noob.
For that you need 3 files: ~/.vimrc and two scripts let's say C-settings.vim and java-settings.vim
In the first file, the ~/.vimrc, you need to include these autocommands:
"To enable file type detection"
filetype on
augroup Java_C_Settings
"the command below execute the script for the specific filetype C
autocmd FileType c source /path-for-C-settings/C-settings.vim
"the command below execute the script for the specific filetype Java
autocmd FileType java source /path-for-Java-settings/Java-settings.vim
augroup END
In the other files (C-settings.vim & Java-settings.vim) you put the settings that you need for every type of file *.c and *.java
Example:
C-settings.vim
set number
colorscheme blue
Java-settings.vim
set number
colorscheme evening
Whenever you open a file with vim the latter will check the filetype first and the settings will be automatically configured.
Note: if the setting files are not in the same directory you can rename them .exrc.
Use ftplugins they are meant for this.
Drop your C config in ~/.vim/ftplugin/c and so on. And don't forget to activate their support.
(This is actually a duplicate question)

What exactly happens when you change a file type in vim?

If I have an apache file in /etc/apache2/sites-available/www.example.com and I set its filetype like so
:set filetype=apache
What does that do? Does that change the file at all? Is it only reflected in the instance of vim? The session of vim? I can manually set the filetype, but then vim warns me that I am in read only mode (/etc/apache2 needs root access). If I open vim as root, I won't get the warning, but if I leave and open it again (as normal or root), the filetype is gone. How do I make this more permanent, at least when called from the same session file
set filetype changes the way vim handles the file, by invoking all the FileType autocommands. It does not persist. If you want to always open that file with filetype=apache, try adding this into your .vimrc:
au BufRead,BufNewFile /etc/apache2/sites-available/www.example.com set filetype=apache
You can read more about it in:
:help 'filetype'
:help filetypes
:help :autocmd
:help .vimrc
EDIT: as found in my /usr/share/vim/vim73/filetype.vim:
au BufNewFile,BufRead access.conf*,apache.conf*,apache2.conf*,httpd.conf*,srm.conf* call s:StarSetf('apache')
au BufNewFile,BufRead */etc/apache2/*.conf*,*/etc/apache2/conf.*/*,*/etc/apache2/mods-*/*,*/etc/apache2/sites-*/*,*/etc/httpd/conf.d/*.conf* call s:StarSetf('apache')
s:StarSetf will setfiletype to apache if the filetype doesn't match an ignored pattern. On my system, :echo g:ft_ignore_pat will show only archive file extensions as ignored. setfiletype does set filetype, but only once.
So, at least on my system, the pattern */etc/apache2/sites-*/* would catch your filename and make it an apache file.
The filetype basically lets Vim change settings for 'types of files'. The way it does this is by firing auto command for the FileType category when you change the filetype. This could potentially change your file if an auto command for FileType is applicable for your file (but generally plugin developers use it for r/o type changes that affect highlighting, and not the contents of the file).
If you are worried that setting the filetype is mucking with your file you can see what FileType autocommands exist by issuing the following command:
:au FileType
To setup your apache files to be apache filetypes you can put something like the following into your ~/.vimrc:
:au BufRead /etc/apache2/sites-available/* set ft=apache

How to set a different colorscheme for each file type in Vim?

In Vim, I want to use a different colorscheme for each file type.
e.g. I want to use desert256 colorscheme for Python & JavaScript files, and use jellybeans colorscheme for HTML & CSS files.
I've tried putting the following code in my .vimrc, but the colorscheme change happens only when changing buffers for the first time.
i.e. If I open a new Python file, Python's colorscheme is used, and when I open a new CSS *buffer*, indeed the colorscheme changes to CSS's colorscheme. However, Changing back to Python's buffer does not change the colorscheme back.
I've used autocmd WinEnter to try and make this rule happen when changing windows (and buffers), but it doesn't help:
autocmd WinEnter,FileType python,javascript colorscheme desert256
autocmd WinEnter,FileType *,html,css colorscheme jellybeans " This includes default filetype colorscheme.
How can I fix this? In addition, a bonus would be to not change a colorscheme when not needed - i.e. Changing from a Python to a JavaScript buffer won't change the colorscheme to "itself".
EDIT:
If anyone's interested, here is my .vimrc repo in github.com. I'll update it with the solution I find here once given.
I've been looking for the same thing. This inside your .vimrc works reasonably well although not perfect.
autocmd BufEnter * colorscheme default
autocmd BufEnter *.php colorscheme Tomorrow-Night
autocmd BufEnter *.py colorscheme Tomorrow
(Note if you're looking for a good dark color theme Tomorrow-Night looks pretty good. Very similar to theme used on Code Academy.)
What you want are filetype plugins, rather than the autocmds. Run help: ftplugin in vim for more info.
From the vim help page:
A filetype plugin is like a global plugin, except that it sets options and
defines mappings for the current buffer only.
In order to use filetype plugins, first put the line filetype plugin on in your vimrc. Then create the folder ftplugin in your vim folder (on unix it's ~/.vim/, I'm not familiar with windows). Then create a script file for each file type you want to customize. These files must be named a specific way. From the vim help page:
The generic names for the filetype plugins are:
ftplugin/filetype.vim
ftplugin/filetype_name.vim
ftplugin/filetype/name.vim
So, for example, if I wanted to create a script for a python file, I would have three options:
Create a file named python.vim in ftplugin
Create a file named python_whatever.vim in ftplugin
Create a file named whatever.vim in ftplugin/python
This script will then be loaded anytime I open a file that vim recognizes as a python file.
So, in order to accomplish what you want:
Create a file named filetype.vim in the ftplugin directory for every filetype you want.
In each of these files, add the line colorscheme name_of_colorscheme
Add filetype plugin on to your vimrc.
In order to set a default colorscheme, just set it in your vimrc file. If I remember correctly, filetype plugins are loaded after your vimrc.
Edit:
The OP indicated that he had a good reason to avoid using the ftplugin directory. After a bit more diggin, I found this script. It can be placed in the global vimrc and seems intended to solve the same problem as the OP.
I have a hack you may like. It is far from perfect, and it doesn't use a .vimrc, but it works for me. It requires you to type a different command to edit different files. It works using the -c parameter when you call gvim. This argument allows you to run vim commands after loading the file. Add this to your ~/.bashrc ( I guess you are using bash ) :
alias gpy="gvim -c 'colorscheme desert'"
alias gcs="gvim -c 'colorscheme jellybeans'"
Hope this helps
Use BufWinEnter instead of WinEnter, like this:
autocmd BufWinEnter,FileType javascript colorscheme jellybeans

Disabling autocommenting for all filetypes

I turned on filetype plugin for some rails vim plugins I added, but a side effect of this seems to be that now autocommenting has been enabled in all filetypes (for instance, if I start a line with #, the next line, either by Enter in insert mode or O, etc. to enter insert mode, will also get a #).
I found a guide to disabling the auto-commenting formatoptions, and added the following to my .vimrc:
au FileType * setlocal formatoptions-=cro
However, I am still running into problems -- unless I explicitly :source .vimrc, (or enter the setlocal ... directly), it is not taking effect. I determined that this is the case because vim's ftplugins are overriding my options with their own.
I then found a second guide which talks about using an after ftplugin script to make changes after the ftplugin scripts have run, however their solution is to create symlinks for every single filetype in ~/.vim/after/ftplugin to a central file, and this seems to be kludgy to me.
Is there any way to create a generic after-ftplugin script or am I approaching this problem incorrectly? Any help would be appreciated.
How about an "after" plugin? Create a file in ~/.vim/after/plugin/ called noAutoComments.vim (or whatever) and place your autocmd in that?
Edit:
The reason this works? I'm only guessing here, but I have a feeling that the autocmd in the ~/.vimrc file is getting removed by some other file (but before the "after" files are getting sourced).
I ended up removing my ~/.vim directory and replaced my ~/.vimrc with the following 3 lines:
filetype plugin on
syntax on
au FileType * setlocal formatoptions-=cro
With only these lines in my ~/.vimrc and no ~/.vim/ directory, the autocmd seems to work as expected (Vim 7.1).
For any file that I edit:
:verbose set formatoptions?
formatoptions=ql
Last set from ~/.vimrc
I have yet to determine what file (plugin) is causing this issue however.
I've done some more investigation and it seems that the location of my autocmd within my .vimrc file determines if formatoptions will be overridden by vim's ftplugins or not. Using vim --noplugin to disable all external plugins, I found the following results:
If my vimrc looks like:
au FileType * setl fo-=cro
filetype plugin indent on
The result of :verbose set fo? is:
formatoptions=croql
Last set from /usr/share/vim/vim72/ftplugin/ruby.vim
However, if the lines in my vimrc are reversed:
filetype plugin indent on
au FileType * setl fo-=cro
The result of :verbose set fo? is:
formatoptions=ql
Last set from ~/.vimrc
... which is the desired result. So it seems that the autocmd needs to be specified after filetype plugins are enabled.
Another reason this might not be taking effect...
From :he :set-=:
When the option is a list of flags, {value} must be
exactly as they appear in the option. Remove flags
one by one to avoid problems.
I have
" Turn off auto-commenting
au FileType * setlocal formatoptions-=c
au FileType * setlocal formatoptions-=r
au FileType * setlocal formatoptions-=o
because I've run into this.
Using one of the various autocmd events to set the configuration option should work if you find the right one, but I'd start by running:
:verbose set formatoptions?
This will tell you where the option was set, which may make it easier to determine which autocmd to use. Alternatively, if you don't mind a bit of minor hacking, the way I'd probably do it is just to find out where it's set in the plugin and comment out that line (and make a note of it in case you ever upgrade the plugin). You could also contact the plugin's author and ask them to make it a configurable option.
For the available autocmd events, read this:
:help {event}
I have tried solutions proposed by many, but none of them worked for me, but I found one very simple workaround, namely, in your ~/.bash_aliases:
# vim without auto comment
alias vi="vi +'set fo-=cro'"
I was struggling with this issue and I finally works with the following lines:
syntax on
filetype on
filetype plugin on
au FileType * setlocal formatoptions-=cro
I think the key here is that the autocmd is place after the filetype plugin on.

Resources