How do I remove the right margin in Vim? It seems to be adding a carriage return and line feed (column 80?) whenever I save a file as .txt or .md. This doesn't happen if I save the file with no extension, but I want to be able to edit .txt files.
I've tried :set nowrap but this doesn't have the desired effect. I also commented out everything in my .gvimrc file in case I inadvertently did something there but no luck with that approach.
The text filetype in Vim sets the textwidth option to 78 by default, which has the effect of splitting lines. If you remove the textwidth option by setting it to zero, Vim will stop splitting lines. You can remove textwidth by hand with
:set textwidth=0
or you could add something like
autocmd FileType text setlocal textwidth=0
to your .vimrc.
I was able to resolve this by commenting out the following line in the vimrc_example.vim file:
" autocmd FileType text setlocal textwidth=78
Here's the path to the file on my machine:
C:\Program Files (x86)\Vim\vim80\vimrc_example.vim
I'm using gVim.
With the current monitor sizes (4K and bigger) the 78 character rule is not valid anymore (valid in the era before the 21" CRT monitors)
I tried the solution by Jon, but it did not work for git commit messages.
I had to change the .vimrc line to
autocmd FileType * setlocal textwidth=0
Now it works for any file type.
Related
Using a minimal vimrc to remove trailing whitespace.
set list " show invisible characters
set listchars=trail:· " display extra whitespace
autocmd BufWritePost <buffer> :%s/\s\+$//e
When I open a project and start working on a file with trailing white space when I save the file I expect it to be removed, but it doesn't get removed.
When I source .vimrc manually and save the file it suddenly works.
What is causing this and how do I solve it?
BufWritePost is executed after writing. You don't need to source .vimrc — you can just write the second time.
But to really fix the problem use BufWritePre.
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
I want to change the filetype based on file extension in vim.
I have the following code in the my .vimrc
autocmd BufNew,BufNewFile,BufRead *.txt,*.text,*.md,*.markdown setlocal ft=markdown
But when I open a file with the extention .md file, the filetype is not changed. I run :set ft command and it shows the output as filetype=modula2.
Am I doing anything wrong?
Edit:
I started to debug by renaming my old .vimrc file and created a new one with just this line. It was working properly. Then I replaced my old .vimrc file and everything seems to be working fine. Guess it was because of some issues in some addon which I am using.
But accepting ZyX's answer, since it thought me an alternate way to do this.
I created a ~/vim/ftdetect/markdown.vim file with this line
autocmd BufNewFile,BufRead *.md,*.mkdn,*.markdown :set filetype=markdown
Reading the docs for filetype, setfiletype only sets if filetype is unset. So you need to use set for an unconditional change to a filetype.
Wondering whether this line goes before or after filetype … on. In the former case you should try putting it (your autocommand) after this line. Better if you put it into ~/.vim/ftdetect/markdown.vim and use setfiletype markdown instead of setlocal ft=markdown:
augroup filetypedetect
autocmd BufNew,BufNewFile,BufRead *.txt,*.text,*.md,*.markdown :setfiletype markdown
augroup END
: it is the default way of doing such things. ~/.vim must go before /usr/share/vim/* paths in 'runtimepath' option in this case (it does by default).
I was able to get syntax highlighting for alternate file extensions by creating renamed copies of the target syntax file in the Vim\vim74\syntax directory.
To make *.md open as a .markdown:
copy markdown.vim md.vim
or paste a copy of markdown.vim to the syntax folder, then rename the copy md.vim.
(Running vim74 on win7)
I'm using netrw to read directory listings in Vim, and I would like to display line numbers in my netrw tabs (so I can use :24 to navigate through directory listings faster). I'm using "set number" in my vimrc to enable line numbers when editing files, but this does not display line numbers in netrw.
When in netrw, if I type the command ":set number", the line numbers display, but as soon as I change directories the line numbers go away.
Is there a configuration option I can put in .vimrc that will make line numbers show up in netrw windows?
From autoload/netrw.vim in the runtime:
call s:NetrwInit("g:netrw_bufsettings" , "noma nomod nonu nobl nowrap ro")
s:NetrwInit overrides a variable only if it is not defined.
Therefore put let g:netrw_bufsettings = 'noma nomod nu nobl nowrap ro' into your vimrc and it should be ok.
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.