how to prevent textwidth from reseting on txt files - vim

Whenever I open txt files (and some others) I get my textwidth set to 80. I think this is coming from syntax or ftplugin. I'd like to fix this in my _vimrc so I don't have to call "set tw=0" every time I open a file that has this setting.

My guess is that you are getting the defaults, not a setting from an ftplugin. Check
:verbose set tw? ft?
to confirm. See the examples under
:help autocmd-patterns
for one way to set your own defaults for *.txt files.
On second thought, the default for 'tw' is zero, so you are not getting defaults. Perhaps some ftplugin used :set instead of :setlocal and you are getting the global value of the option. I think the rest of what I wrote is still on target.

The problem was in vimrc_example.vim the line
autocmd FileType text setlocal textwidth=78
sets the textwidth on txt files. Also my formatoptions get reset and no longer have l or lv (verbose doesn't give any detail who did it)

/etc/vimrc can also contain a set tw command - it certainly did in my case, thereby forcing all my files to a text width of 78 until I changed it manually.

Related

How to set a special tabstop when open a special file with vim?

Here is my vimfiles.
I write
se tabstop=4
in my vimrc. But sometimes I read some project source codes, they use TAB to indent and set tab equals 8 space. So I must
:set tabstop=8
to make the code indent right.
If I can make sure the path of source code (eg. /home/foo/erlang/),
is there a method to make my life better?
I mean to write some code in vimrc, like
if file in path "home/foo/erlang"
then set tabstop=8
end
or some other vim settings.
Thanks!
If you want to add that stuff to your own .vimrc file then one thing you can do is just autocmd file opens to call a function to set things up like you'd like... The docs talk about doing this, see :help autocmd-patterns
autocmd BufRead */home/foo/erlang/* set tabstop=8
I actually have a few autocmds which do "big" setup on filetypes depending on their directory and other attributes. One such might look like this:
autocmd BufRead *.txt call SetUpTextFileBuffer(expand("<afile>:p"))
Then that function does path matching and so forth to configure a whole range of options (autoformatting, text width, etc.). Modelines are certainly better if you can insert them but sometimes you're working with others' code and you don't want to leave a huge footprint.
You can create a .vimrc file in the directory where you want the settings to apply. It would contain your tabstop and other Erlang-specific settings.
% cd ~/erlang
% ls
... .vimrc
% vim somefile.erl
Vim will automatically see and use the .vimrc in $PWD. (For this to work, you should have set exrc in your ~/.vimrc). See :help 'exrc' for details.
If you don’t want to cd and open files from that directory, you can always invoke Vim with an explicit startup file:
vim -u /path/to/erlang.vimrc somefile.erl
You can use a modeline in your file.
(You need to have set modeline in your .vimrc for this to work.)
In the first or last lines of your file, put the following comment;
% vim: tabstop=8:
By default, the first and last 5 lines are scanned for modelines.

How to find out, in which plugin a Vim setting gets changed?

Today I had some obscure behavior. I have
set autoindent
in my vimrc, but for some reason, this gets turned off, whenever i open a PHP file.
I've fixed it now by adding a line like
autocmd FileType php set autoindent
But I'm still trying to figure out, where this setting is disabled. So is there some way to find out, where in the vim config a setting gets changed?
For reference here's my full vimrc.local that I'm using on Ubuntu:
https://gist.github.com/mikehaertl/1612035/5fa149468006577d193858bbc8cefcd3a413e017
EDIT:
The problem was caused by a filetype indent on that I added some time ago to my config. No idea, why that affects autoindent, though.
The :verbose command will tell you where an option was last changed:
:verbose set autoindent?
If that alone doesn't help, you can inspect all executed commands, preferably with the output redirected into a logfile:
:set verbosefile=vim.log
:20verbose edit foo.php
Also note that there are several options controlling indentation, e.g. 'cindent', 'smartindent', 'indentexpr', etc.
PS: To avoid that the changed option value spills into other buffers, it's recommended to use :setlocal instead.
Isnt that line the problem?
autocmd FileType php set cindent|set cinkeys-=0#

Vim ignores shiftwidth specified in .vimrc

I am using Vim 7.3.154 cli on Linux Mint 12 "Lisa"(x64).
I prefer using a tabstop and shiftwidth of 2 columns.
My .vimrc has
:set ts=2
:set sw=2
to accomplish the same.
Whenever I open a new instance of Vim, tabstop value is retained, so existing tabs are rendered according to .vimrc. But somehow the shiftwidth value gets changed to 3. This results in 3 column indention while auto-indenting.
Running :set sw=2 on the new instance of Vim fixes this issue.
Can anyone tell me why Vim is ignoring/changing shiftwidth value from .vimrc?
I tried disabling all plugins to no avail.
Vim compiled options | .vimrc
This answer just summarizes what we discussed in the comments. :)
This is most likely caused by the fact that you have file specific settings enabled. Check :h modeline for more info on that. Make sure that those files you have the problems with don't have this line in them.
Instead of only setting tabstop and shiftwidth you should also setup a couple more settings regarding whitespace and tabs.
set noexpandtab " Make sure that every file uses real tabs, not spaces
set shiftround " Round indent to multiple of 'shiftwidth'
set smartindent " Do smart indenting when starting a new line
set autoindent " Copy indent from current line, over to the new line
" Set the tab width
let s:tabwidth=4
exec 'set tabstop=' .s:tabwidth
exec 'set shiftwidth=' .s:tabwidth
exec 'set softtabstop='.s:tabwidth
Check this video for some extra info: http://vimcasts.org/episodes/tabs-and-spaces/
So this is the answer to the actual problem that you were able to solve. That php.php file was in the /plugin directory. That directory gets loaded once, each and every time Vim starts up. Check this: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html#vimplugin
If what you want is that file to load only on PHP files then you should put it in the /ftplugin folder: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html#vimftplugin
Read the documentation there, it should be a .vim file, in other words in this case it would be called php.vim.
What Pathogen or Vundle do is modify the runtimepath (:h runtimepath), nothing more, nothing less.
So you can now accept this answer, by clicking the little green arrow to the left of this answer. :)
You can run:
verbose set shiftwidth ?
to check what is setting the value of shiftwidth. It might coming from a plugin. You can also choose to modify the value there.

How to impose hard wrap against my soft wrap wishes?

Well, I'm a newb at Vim, so I'm guessing there's a 99% chance it's a user error!
Vim was soft wrapping long lines very nicely thank you, then a couple of days ago it started to insert hard wraps but only when I had saved the file.
I have been through wrap, nolinebreak, textwidth, nolist, and all combinations thereof to try to get softwrap back but to no avail. Heck, I even read the help pages. All of 'em.
Here's the relevant bits from my .vimrc (as you can tell, I'm getting desperate):
" Editing
set aw ai
set et ts=8 sts=2 sw=2 nu
set fo+=tcrqw fo-=o
set showmatch matchtime=5
set whichwrap=<,>,h,l,[,]
set cursorline
set nofoldenable
set wrap
set linebreak
let mapleader = ","
I picked up this .vimrc from using Vundle.
I found the culprit, Tim Pope's Vim Markdown plugin. Lovely plugin but personally prefer soft wraps, will have to find how to change it!
but only when I had saved the file.
This should hint to you that some plugin is touching the buffer Pre-Write.
Find out which it is by doing
:au BufWrite,BufWritePre,BufWriteCmd
:au FileWriteCmd,FileWritePre
To see where the trigger was installed from:
:verbose au BufWrite,BufWritePre,BufWriteCmd
:verbose au FileWriteCmd,FileWritePre
I have a suspicion this is probably caused by your fo line. Having "t" in the formatoptions option means that if a textwidth is set for the current buffer then vim will break lines at this width. You may notice that this only happens for certain filetypes because different ftplugins may be setting textwidth without you knowing.
The next time you see this happening, I'd suggest running :verbose set textwidth? (with the question mark) and seeing if the value is set. This command will also point you to where it was last set.
Another test would be to just remove "t" from your fo line and see if the problem goes away.
Janus is another Vim plugin that fiddles with linewrap/linebreak and textwidth.
:verbose set tw?
told me:
textwidth=72
Last set from ~/.vim/janus/vim/core/before/plugin/filetypes.vim
Now I just need to figure out the right incantation to disable that... for now, I just added set textwidth=99 to my ~/.vimrc.after file, but there may be a better way...

VIM: set filetype=txt for every new file [No Name]

I tried all possible things to let vim set filetype to 'txt' to all new files I create (in a new tab) but it doesn't work.
This is p.e. what I've read on the web a few times:
au BufRead,BufNewFile *.txt setlocal ft=txt
(to put in _vimrc)
However it doesn't work.
Can anyone help me?
The following line, added to your .vimrc, will set the filetype to text if it is not already set.
autocmd BufEnter * if &filetype == "" | setlocal ft=text | endif
All files are considered plain text unless you have file-type detection turned on or explicitly set the file-type. However, Vim lets you set the file-type to any old text, so are you absolutely sure it is not working?
:set filetype=banana
:set filetype?
filetype=banana
Setting the filetype is not going to have any noticable effect unless there is a corresponding file in the ftplugin Vim directory and Vim does not ship with a txt.vim file-type file. You could, of couse, add a txt.vim here but I am not sure what this will gain you over default settings — what special behaviour would you want for text files that you would not want for the default behaviour?
(If you want syntax highlighting (whatever that may mean for text file!) then you will also have to create a txt.vim file in the syntax Vim directory.)
What effect are you trying to achieve?
It's actually way simpler than all this. Just put this as one of the first lines in your .vimrc.
set ft=txt
Whenever opening a new file, the filetype will be set to txt. But if you open a file with an known existing type it will still be overridden no problem.

Resources