how to specify indentation for a file? - vim

I heard that we can specify VIM indent for a file by embedding the indentation commands into the file to be edited.
Can somebody let me know how to do this?

The thing you are looking for is called a modeline.
Here is a modeline that I commonly use for vim:
/* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: */
A similar thing for emacs would look like this:
/* -*- Mode: tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
This question has been covered before though.

Something like
// vim: set ts=4 sw=4 et:
where // is your language's comment marker.

You can also specify indentation for a given filetype.
For example for python files, you can add this line to your .vimrc file:
autocmd FileType python set sw=4 sta
The advantage compared to modelines is that you don't have to write it everytime. Then for a specific python file, if you want to change the indentation, you can write this modeline:
# vim: set sw=2:

Related

Am I writing Vim modelines incorrectly?

I've appended the following Vim modeline to the end of a .md file:
# vim: ts=4:sw=4:softtabstop=4:expandtab:autoindent
I've also tried this:
# vim: ts=4 sw=4 softtabstop=4 expandtab autoindent:
However, if I check a setting like this for either case, I see that the tabstop is still set to 2:
:set ts? " returns 2
Will Vim give greater precedence to any settings in my .vimrc or to ftplugin/markdown.vim? I would think that a modeline inside a file would have the greatest precedence.

Why am I having trouble to embed "set syntax" into a file?

I have read the help and examples, but still don't know what I'm doing wrong.
When I manually enter :set syntax=javascript, I get the syntax highlighting I want.
But when I edit the first line of my file to read:
/* vim: set syntax=javascript: */
nothing happens.
When I split that line into 3 lines:
/*
# vim: set syntax=javascript:
*/
I get some limited syntax highlighting, not as good as with manual command. I can write syntax=anything there and it makes no difference.
(Vim version 7.4.160, Centos 7)
Lines of the format:
/* vim: set syntax=javascript: */
are called modelines in Vim. In order for Vim to process them, you must have the modeline option toggled on.
To see if modeline is enabled, run this ex command:
:set modeline?
If it returns nomodeline, you can enable it by adding the following in your ~/.vimrc:
set modeline
Vim will look for a modeline in the first 5 lines of the file, by default. You can set the number of lines that will be searched with e.g.:
set modelines=10
See :help modeline for details.

Vim autocommand not triggering on search?

I use Vim 7.3, and have this in my .vimrc (and nothing else):
filetype plugin indent on
autocmd FileType java :setlocal sw=4 ts=4 sts=2 noet
autocmd BufNewFile,BufReadPost ~/testdir/* setlocal sw=2 ts=2 sts=2 et
I have a directory ~/testdir which only contains a subdirectory p and the file ~/testdir/p/A.java which contains this:
class A {
}
If I open A.java by saying :e ~/testdir/p/A.java then :set et? reports that expandtab is currently ON.
On the other hand, if I start a new instance of Vim and go :vim A ~/testdir/** then this search will open A.java, but now :set et? tells me expandtab is OFF.
How do I make my intended indentation settings always apply for files under ~/testdir/?
The event BufReadPost should be being fired. Check that writing a message. This is:
autocmd BufNewFile,BufReadPost $HOME/git/repo/* echom "Reading buffer " . expand("<afile>")
Look for the messages with:
:messages
If the messages are there, then you know you are setting those options for the buffer. So, if the options have changed when the buffer is displayed, perhaps a plugin is setting them to some other thing. Check who is the culprit by showing who last changed the option with
:verbose set sw?
If that doesn't lead you to a solution, try using BufWinEnter instead of BufReadPost. That would probably work.
Vim explicitly recommends using ~ for the user home.
The help at :h autocmd-patterns says that you can use environment variables in the event patterns ...
... [a]nd ~ can be used for the home directory (if $HOME is defined):
:autocmd BufWritePost ~/.vimrc so ~/.vimrc
:autocmd BufRead ~archive/* set readonly
The environment variable is expanded when the autocommand is defined, not when
the autocommand is executed. This is different from the command!
So, change $HOME to ~ in your autocommand.
Even if your problem turns out to be unrelated it's good to follow the practice recommended in the docs.
The autocmd does work but probably not the way you expect since you did not set softtabstop.
autocmd BufNewFile,BufReadPost ~/git/repo/* setlocal sw=2 ts=2 sts=2 et
If you do not set softtabstop it will keep its old value. If this value is not 0 (which seems to be the case) when you press tab you will get the number of softtabstop spaces inserted into the file. And it will look like its not working.
It is generally a good idea to set shiftwidth, tabstop, and softtabstop to the same value if you ever change it.
Its also probably a good idea to make the set command local to the buffer by using setlocal.
Take a look at :h softtabstop
You can see that the autocmd "works" when softtabstop is set to its default value (of 0) if you run the command like this. And then run your :vim command
vim -u NONE -N -c'autocmd BufNewFile,BufReadPost $HOME/git/repo/* set sw=2 ts=2 et'

Auto-indent the whole text file shortcut?

What's the shortcut for autointenting the whole text file (like .c files or .java files). It should be equivalent to the tab in emacs but I couldn't find a correct way to do this online.
To reindent the whole file can be done with.
gg=G
gg moves to the top of the file.
=G reindents from the cursor position to the end of the file.
In addition to #FDinoff's suggestion, you could also use autocmd/au for user-defines filetype indentation settings. Like so-
au FileType c setl sw=8 ts=8 noet (For C);
au FileType python setl sw=4 ts=4 et (for python)
and so on..

Vim: How do I automatically set textwidth=60 when editing a specific file?

In my .vimrc I have the following line:
set textwidth=80
However, when editing the files: README-SETUP and README-INSTALL I would like vim to have textwidth set to 60.
I think this can be done for specific file types using autocmd, but how would I do it for specific files?
You could do this like so:
autocmd BufReadPre README*.txt setlocal textwidth=60
Or you could list the files one by one:
autocmd BufReadPre README-SETUP setlocal textwidth=60
autocmd BufReadPre README-INSTALL setlocal textwidth=60
EDIT: As ZyX points out, prefer setlocal over set for options like this you really don't want all buffers having that textwidth for the duration of the session.
You can also add a comment to the top of the file:
# vim: textwidth=80
You can replace the # by whatever character signifies a comment in your context.

Resources