Embed vim settings in file - vim

In some files I can see a commented line, usually the last, with vim settings. Does vim read these settings? If it does, are any limitations of what kind of settings man can put there?

They're called modelines and while I'm not sure the extent you can go with them, here's a link to the vimtips wiki with some examples:
http://vim.wikia.com/wiki/Modeline_magic
help modeline
from within vim to check out the official docs.

It's this line of code:
[other chars]<spaces>vim:<spaces>settings
Put it in the first or last few lines of the file, note it needs < spaces >. For example:
# vim: tabstop=2 shiftwidth=2 expandtab
In short version:
# vim: ts=2 sw=2 et
Put one of the above line in the file, in top or bottom lines, done. For more information, use vim help:
:h modeline

You can check out in the online manual: http://vimdoc.sourceforge.net/htmldoc/options.html#modeline
And this faq item also refers to it: http://vimdoc.sourceforge.net/htmldoc/vimfaq.html#19.5

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.

Vim-go - Indent with tabs not whitespaces

I'd like to have my go files indented with tabs not whitespaces.
When I run :set expandtab then :retab I get what I want but it automatically switches back to an indentation with whitespaces everytime I save the file.
I am using the vim-go plugin but I couldn't find anything in the doc/code which could lead to this issue. I have tried changing to setlocal noexpandtab in ftplugin/go.vim but it didn't work
How can I change this?
Many thanks
Put that in the ~/.vimrc file to make the settings permanent.
Here's a wiki article on this - http://vim.wikia.com/wiki/Indent_with_tabs,_align_with_spaces

Quickest Way to Revert Spaces to TABs in VIM

I have always been one to replace TABs in VIM with x amount of spaces (usually 4).
Four lines I almost always use in my .vimrc config file are:
set tabstop=4
set shiftwidth=4
set expandtab
syntax on
Basically, there are moments when I NEED to use a single TAB (such as Makefiles), and I don't know how else to work around that other than leaving the file, editing my .vimrc, and then reloading the file of interest.
That said, what is the quickest way (from within VIM) to revert it back to using TABS, and then reverting back to my original settings? I'm looking for the least-hassle, least-keystrokes solution.
VIM will automatically enable the TAB for a makefile, assuming you name it "makefile," as opposed to "Makefile." Not sure why VIM still doesn't detect the type with a lower-uppercase difference, but such is life. (#Sedrik)
That aside, other alternative solutions are:
Filetype Binding (#ThorstenS #tungd):
autocmd FileType make setlocal noexpandtab
RealTime Switch (#ThorstenS):
Assuming the .vimrc configuration mentioned in the question, do:
:set noet (to switch from spaces to TAB)
and :set et (to switch back)
Just use the magical escape key available in insert mode.
On the *NIX's it is ^V by default when you are in insert mode. On Windows, you need to find out what the magical escape character is - ^V is taken for something else; I think it may be ^Q or ^S?
So! In your makefile:
this: this.c
<C-V><Tab>cc this.c
where the usual meanings apply:
means hit ctrl-V (you should see a ^ hiding away under the cursor)
- hit the tab key. Bingo.
Works for me.
Note: if you use vim settings or startup code which smashes tabs as you read a file, this obviously is a short-term fix. I prefer to learn how to use the retab command to ensure a file is tab-clean, because I don't like a file to be touched unless I consciously choose to do so.
Just type set noexpandtab . Perhaps you bind this to a function key.
Only this configuration helped me solve this problem.
filetype plugin indent on
filetype detect
autocmd FileType make set noexpandtab
Vim defaults to tabstop=8 and noexpandtab, so the defaults are well suited to working with Makefiles. If your .vimrc uses custom options for tabstop, expandtab, etc., one simple solution is to bypass your .vimrc while working with Makefiles.
From the manpage (emphasis mine):
-u {vimrc} Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this to edit a special kind of files. It can also be used to skip all initializations by giving the name "NONE". See ":help initialization" within vim for more details.
Since I can never remember the necessary flag/value/formatting, I've created a Bash alias which remembers for me: alias vimnone='vim -u NONE'
You can create a custom configuration per file type.
mkdir -p ~/.vim/after/indent
echo 'set noexpandtab' >> ~/.vim/after/indent/make.vim
Basically, here we created a new indent configuration for makefiles that will be loaded after other scripts.
source

How can I disable code folding in vim with vim-latex?

I have tried the usual approaches, and have read :help tex.vim
(see : http://vimdoc.sourceforge.net/htmldoc/syntax.html )
I've taken a brief look at syntax/tex.vim, but can't see how to disable it without rebuilding vim without folding. I'm sick of hitting 'zE'.
Lines I've tried in my .vimrc:
set foldlevel=manual
set foldlevelstart=99
let g:tex_fold_enabled=0
Just noticed that there are variables to control folding in vim-latex-suite, at least as of v1.6 of the plugin. The functionality is documented here:
http://vim-latex.sourceforge.net/documentation/latex-suite.html#latex-folding
In short you should be able to change three global variables to get rid of all folding:
:let Tex_FoldedSections=""
:let Tex_FoldedEnvironments=""
:let Tex_FoldedMisc=""
That should get rid of all folding. If you want to disable some folding but not all then you can control things by setting the appropriate values for each variable, as described in the documentation link above. Hope that helps.
What about
autocmd Filetype tex setlocal nofoldenable
The folding functionality all seems to located in folding.vim file of latex-suite distribution. This file is referenced in line 825 of my main.vim file in the latex-suite folder of the ftplugin folder. That line reads:
exe 'source '.fnameescape(s:path.'/folding.vim')
Comment out that line and, as far as I can tell, it strips out all the folding in latex-suite plugin. I don't think it affects anything else, but I haven't checked.

How do you enable file specific tab indent settings in VIM?

I believe there is a method to write a comment in a file that vim will use to override default tabbing and indent values.
Can someone point me to information about this feature and how to use it?
Per-file settings can be done using "modeline magic".
The basic idea is that you can add a comment to an individual file like this:
/* vim: set tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab */
Within vim, you should review:
:help auto-setting
:help modeline
:help modelines

Resources