I want to "set spell" automatically when i am editing the commit text in git. From the % I see that it is writing to a filename called .git/COMMIT_EDITMSG. How do I update my .vimrc to automatically set spell on when editing that file.
something on the lines
if ( filename has a word COMMIT)
set spell
fi
This line works for me:
autocmd FileType gitcommit setlocal spell
Ordinarily you could do this using an autocmd (au BufNewFile,BufRead COMMIT_EDITMSG setlocal spell) but recent versions of vim already have a filetype assigned for git commit messages, so what you can do instead is create a file ~/.vim/ftplugin/gitcommit.vim and put this in it:
if exists("b:did_ftplugin")
finish
endif
let b:did_ftplugin = 1 " Don't load twice in one buffer
setlocal spell
and make sure that you have filetype plugin on in your .vimrc. It's a little more work getting going but it makes it easier to add tweaks in the future. :)
au BufNewFile,BufRead COMMIT_EDITMSG setlocal spell
autocmd BufNewFile,BufRead COMMIT_EDITMSG set spell
in ~/.vimrc will do it
A handy way to do this cleanly is with a vim filetype plugin.
This will allow you to place file type dependant configurations/mappings in a seperate file (see my .vim/ftplugin/gitcommit.vim for example)
To do so, create a file at ~/.vim/ftplugin/gitcommit.vim and place your custom configurations there.
You can add 'set spell' to your .vimrc file to make Vim automatically spell check all documents including your git commit messages. Vim is smart enough to spell check comments and strings while ignoring your source code.
Depending on your colorscheme, this can be annoying though to see variable names in your comments and strings highlighted as misspelled words.
See this stackoverflow question for more details on spell checking.
Related
I have a copious amount of files opened in vim.
How can I set filetype to, let's say, fortran for each of them, so I won't have to do the command for every file?
If you are working with recognizable fortran files (extension *.f,*.for,*.fortran,*.fpp,*.ftn,*.f77,*.f90,*.f95,*.f03,*.f08) Vim should be able to set their filetype by himself. Just put this line in your ~/.vimrc:
filetype plugin indent on
But, to answer your question, you should do:
:bufdo setlocal filetype=fortran
I now realize that I asked prematurely.
Here is how:
:bufdo execute "setfiletype fortran"
I want Vim to change the filetype when I type a shebang line (eg. #!/bin/bash) on the first line of a new buffer.
Currently I'm using vim-shebang plugin for changing filetype, but it only works when opening a new buffer.
Clarification: I'm interested in achieving the desired result by mapping <CR> in insert mode. What I want is when I type #!/bin/bash<CR> on the first line of a buffer to automatically execute :filetype detect and return to editing.
You can use
:filetype detect
to re-trigger the filetype detection after you've written the shebang line.
This can be automated with :autocmd, e.g. on the BufWritePost when &filetype is still empty.
I'd recommend to read the (always great) documentation of Vim (which I'm quoting bellow):
:help new-filetype-scripts
This might help you.
If your filetype can only be detected by inspecting the contents of
the file.
Create your user runtime directory. You would normally use the first
item of the 'runtimepath' option. Example for Unix: :!mkdir ~/.vim
Create a vim script file for doing this. Example:
if did_filetype() " filetype already set..
finish " ..don't do these checks
endif
if getline(1) =~ '^#!.*\<mine\>'
setfiletype mine
elseif getline(1) =~? '\<drawing\>'
setfiletype drawing
endif
See $VIMRUNTIME/scripts.vim for more examples.
Write this file as "scripts.vim" in your user runtime directory. For
example, for Unix:
:w ~/.vim/scripts.vim
Update (after the edit of the original question): I'd recommend against the mapping of , but you can do it with
:inoremap <CR> YOUR_SPECIAL_FUNCTION_WHICH_DETECS_THE_CURRENT_LINE_AND_RUNS_FILETYPE_DETECT
The above code snippet (if getline(1)... is enough to get you started. We are not the writethecodeformeforfree.com community.
The one IDE feature that I always missed and invariably plug into vim is tab completion.
I'm a big fan of SuperTab, but one thing I can't stand is the fact that it treats the parts of CSS class names and IDs with dashes as individual words.
I've found a couple of possible solutions for camelCase and underscore_completion but I can't seem to find anything that supports plain-old-dashes.
This is not a CSS-specific problem: Vim uses the value of iskeyword to perform completion.
Type :set iskeyword? to see what characters are considered to be part of keywords. The default on a Mac is supposed to be #,48-57,_,192-255.
You can add the dash to the list with this command:
:set iskeyword+=-
Add this line to your ~/.vimrc to make this setting stick:
set iskeyword+=-
This seems to work for me:
autocmd FileType css,scss set iskeyword=#,48-57,_,-,?,!,192-255
Taken from here: VIM: How to autocomplete in a CSS file with tag ids and class names declared in HTML file
For future readers: if you want the benefits of dashes for edit/movement commands, but want full property autocompletion, try adding this to your .vimrc:
augroup css_dash_autocompletion
autocmd FileType scss,css autocmd! css_dash_autocompletion InsertEnter <buffer> set isk+=-
autocmd FileType scss,css autocmd css_dash_autocompletion InsertLeave <buffer> set isk-=-
augroup END
The first ! prevents duplicate event firing. Thanks to ZyX for the structure. If you re-source your .vimrc, you will need to :e any (S)CSS files you have open to pick up the change.
I frequently develop on Ruby on Rails. With the recent inclusion of Tilt in RoR 3, we have file extensions like .scss.erb. How can I make the filetype = scss.erb in this case automatically, and the same for every file that has multiple extensions?
Edit: It should be scss.eruby in this case, as erb extension defaults to eruby filetype.
Edit: If it wasn't clear, I'm looking for a way to make this work dynamically for all files with multiple extensions. For example, file foo.js.html should have a filetype of js.html.
Edit again: Prince Goulash's answer doesn't take the default filetype for a particular extension.
In your vimrc:
autocmd BufRead,BufNewFile *.scss.erb setlocal filetype=scss.eruby
(see :help ftdetect, section 2).
EDIT
To set the filetype dyanamically for multiple extensions, this seems to work for me:
autocmd BufRead,BufNewFile *.*.*
\ sil exe "setlocal filetype=" . substitute(expand("%"),"^[^.]*\.","",1)
The substitute command constructs the filtype by simply stripping all text from the filename before the first .. There may be a more sophisticated way...
EDIT AGAIN
Here's another attempt. MultiExtensionFiletype() is function that uses the default filetype of the last part of the extension and prefixes it with the first part of the extension (i.e. the part sandwiched between the dots).
function MultiExtensionFiletype()
let ft_default=&filetype
let ft_prefix=substitute(matchstr(expand('%'),'\..\+\.'),'\.','','g')
sil exe "set filetype=" . ft_prefix . "." . ft_default
endfunction
The function must be called on a BufReadPost event so the initial filetype is set by ignoring the multiple extensions.
autocmd BufReadPost *.*.* call MultiExtensionFiletype()
Hopefully this answer is converging on something useful!
I would like to have a higher number in my linespace setting for markdown files for easier reading. I thought I'd do in my .vimrc:
au FileType markdown setlocal linespace=4
But no luck. The locality of the setting is ignored and linespace is applied to all buffers (even Python and Javascript files).
I'm using Pope's markdown.vim plugin to provide the markdown filetype.
Ideas?
Actually linespace is a global option (see :h linespace). Your auto command
is working, but even though it's using :setlocal, the option can't be applied to
a specific buffer or window; acting as if you had used :set.
Update
If you want to change the linespace option based on your current buffer, basically
all you need to do is changing your current auto-command from
au FileType markdown setlocal linespace=4
To
au BufEnter *.markdown setlocal linespace=4
The difference is that now the command will be auto called when you enter a buffer
that has a file name ending with .markdown (change this pattern to match your
usual mardown extension).
To reset the option, add the following auto-command as well to your .vimrc:
au BufLeave *.markdown setlocal linespace=2
It will set the linespace option to 2 (or whatever value you want) once you leave
a buffer containing a markdown file.