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.
Related
I would like to set up VIM for Cobol development and wanted to have the lines form column 7 to 11 marked so as to indicate the code areas. However, when I added this line of code in my vimrc file it colorized the NERDTree too.
set colorcolumn=7,11,73,80
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
How can I make the NERDTree columns not colorized and keep colorization only on the working file?
With the following line in your vimrc:
set colorcolumn=7,11,73,80
you define a global value for a window-local option which is reused for every new window. It is set for the 1st window, which passes it on to the 2nd window, etc.
Since that specific value for that specific option only is to be applied to Cobol buffers, you are supposed to use Vim's built-in filetype plugin support:
Make sure you have either of those lines in your vimrc:
filetype plugin on
filetype plugin indent on
filetype indent plugin on
See :help :filetype.
Create after/ftplugin/cobol.vim under your Vim runtime. On a typical Unix system, it should look like this:
$HOME/.vim/after/ftplugin/cobol.vim
And add the line below:
setlocal colorcolumn=7,11,73,80
We use :help :setlocal to make sure that the option won't be passed on to other windows.
Lets say I have two group of settings one for writing and one for programming. How do I modify my .vimrc so that the first set load while working on files with the .md extension and the latter with html/js/css ones?
Though :autocmds based on FileType are a quick and easy way for your ~/.vimrc (as suggested by #andrewdotn), Vim has a proper abstraction for that: filetype plugins.
Put settings and buffer-local mappings into ~/.vim/after/ftplugin/{filetype}.vim. (This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/{filetype}.vim.)
Use autocommands. For example, here are the settings from my ~/.vimrc:
autocmd BufRead,BufNewFile *.js,*.html
\ setlocal indentkeys=!^F,o
autocmd BufRead,BufNewFile *.md
\ setlocal filetype=markdown | syntax clear
setlocal is used instead of set so that only the buffer matching the filename pattern is affected. Otherwise, the change to an option like shiftwidth will affect all buffers every time you load a *.foo file.
The \ is the line-continuation character, which lets you split a single command over multiple lines to increase readability.
The | character is the bar which is used to chain together multiple commands in one line.
See autocmd.txt in the vim documentation for more.
I would like to have a default textwidth of 80 characters except for a few file extensions like txt. The following lines appear to work, except for the first time when I edit (and create) a txt file.
setlocal textwidth=80
autocmd bufreadpre *.txt set textwidth=0
What is the correct way to do this?
First, you've got the scopes the wrong way; use :set for the global default and :setlocal for the buffer-local override in the :autocmd.
Second, BufReadPre is only for reading existing files, not new ones; that's why it doesn't work the first time. Instead, you should use BufNew,BufRead; this captures both cases, and only applies after the file was read, so it will still work when you use modelines or have a setting in an filetype plugin.
Third, the :autocmd solution tends to become unwieldy once you have many customizations. If you only want to enable a setting for certain filetypes, put the corresponding :setlocal commands into ~/.vim/after/ftplugin/<filetype>.vim, where <filetype> is the actual filetype (e.g. java). (This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/<filetype>.vim.)
use setlocal
autocmd bufreadpre *.txt setlocal textwidth=0
instead of set.
With setlocal you make sure that the value you're setting is set in the current buffer, not for all buffers.
I have turned off line wrap in Vim by adding this in my vimrc:
set nowrap
But, I would like to have line wrap turned on automatically when I am editing *.tex files. So, I added this to my vimrc:
augroup WrapLineInTeXFile
autocmd!
autocmd FileType tex set wrap
augroup END
This should enable line wrap when the filetype is detected to be TeX. This works as expected for tex files, but if I open a non-tex file in the same Vim session, it has line wrap turned on!
Enabling and disabling word wrap automatically on different file extensions on Vim suggests using BufRead instead. But, even that has the same effect: if I first open a TeX file, followed by a non-TeX file, the non-TeX file has line wrap turned on.
How do I correctly enable line wrapping only for a certain filetype?
You can use setlocal for this. It'll only affect the current buffer.
augroup WrapLineInTeXFile
autocmd!
autocmd FileType tex setlocal wrap
augroup END
That'll be applied to every new TeX buffer.
The 'wrap' option is local to window. When you use :set, it will also apply to any newly opened windows. You want to use :setlocal.
Also, though your :augroup WrapLineInTeXFile works, it is cumbersome and doesn't scale to many settings. If you have :filetype plugin on in your ~/.vimrc, you can place filetype-specific settings (like :setlocal wrap) in ~/.vim/after/ftplugin/tex.vim (use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/tex.vim).
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.