How to override default indentation in Vim - vim

It seems to be very simple. Just create custom .vimrc file in /home folder, and paste something like:
filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab
But there's still a problem. Pressing ENTER key after a bracket, I still have 8 spaces instead of desired 4. At the same time tab key gives me 4 spaces.
I also found a default vimrc file located in /etc/vim. There is a script:
" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
"if has("autocmd")
" filetype plugin indent on
"endif
According that if I uncomment these lines I'll eventually get desired four spaces, but I just can't save this because it's "readonly".
I use the "Huge version without GUI" version 8.0 which should support python3 out of the box. I really can't find out how to bypass such minor obstacle. I've killed whole day trying to fix it. Maybe I as a beginner chose wrong text editor? But I like Vim because of its minimalism and I want to have access to its power when I master my skills. At the same time I need a tool for work - now. And incorrect indentation is a small but very annoying snag on my way.

Related

Vim file misaligned when opened in text editor

I have a javascript file that I created in Vim and it looks fine, but when I open it in another program such as just a text editor, the indentations are much greater (like 10 times the space) and some lines are misaligned. The only thing I have relating to lines in my ~/.vimrc file are:
"set tab indentation to 2 spaces"
:set tabstop=2
filetype plugin indent on
Has anyone seen this behavior or know what could be the issue?
Your vimrc file is configured to show tabs as 2 spaces, but the way tabs are shown isn't the same in every program. Sometimes they're shown as 4, sometimes 8. A typical argument is to use multiple spaces instead of actual tabs, to make the file look consistent regardless of the text editor's tab settings, but at the cost of increased file size. A search for "tabs vs spaces" will yield plenty of arguments for both sides.
If you decide to use spaces, you could, for example, configure vim to insert spaces whenever you press the tab key:
:set tabstop=2
:set shiftwidth=2
:set expandtab
(from vim wiki)
The default tab spacing in many text editors is 8 spaces, not 2, so the indentations for any source code that contains tabs will appear much greater in those text editors.

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.

VIM loses syntax highlighting when using split command

So I have created my own syntax highlighting file and it works well if there is only one file opened. However, if I do :split otherFile, the other buffer that gets opened does not have syntax highlighting. I have tried various things, like :syntax on etc. What can be the problem?
I am running Ubuntu 11.04, 64-bit version.
VIM version: VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Mar 24 2011 07:07:34)
I have created a simple syntax highlighting file and put it in ~/.vim/plugin/syntax.vim
The last line of the syntax highlighting file is let b:current_syntax = "sth".
I haven't done any kind of wiring, like specifying the file location in .vimrc, the syntax works automatically (for one file opened).
I had this problem recently and much more generally. That is, the problem appeared for all filetypes. After debugging a bit, I discovered that the filetype was being properly recognized, but that for new splits, syntax was somehow becoming unset.
I'm still not 100% sure how this happened, but for future Google visitors, I'll write down what fixed the trouble for me: I moved set syntax = on much earlier in my .vimrc. After years of accretion, the line with set syntax = on had drifted down until it was below a number of other things. Moving it back up to (nearly) the top of the file fixed things for me. Here's what the start of my .vimrc looks like now:
" First two uncommented lines only useful if you use Tim Pope's vim-pathogen.
" https://github.com/tpope/vim-pathogen
execute pathogen#infect()
execute pathogen#helptags()
" Most general settings first
set nocompatible " Vim rather than Vi; implied by this file
syntax on " Syntax highlighting on
filetype plugin indent on " Filetype detection
" ... etc.
Syntax files belong into ~/.vim/syntax/sth.vim, not ~/.vim/plugin/syntax.vim. The latter is sourced only once on startup, that's probably why it only works for the first loaded file.
For your syntax to become active, you need to :setf sth, or insert a corresponding modeline into your files, or write a filetype detection for your syntax to automate that.
Resurrecting this, as I just observed the same behavior. The solution that I found was to add the filetype on setting to my .vimrc file. fwiw, I added it immediately after syntax on. syntax on happened to be at the top of my .vimrc file already. So, here are the first few lines of my .vimrc:
1 " Activates Syntax Highlighting
2 syntax on
3
4 " Filetype on
5 filetype on

Possible to change length of tab depending on file extension? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Changing Vim indentation behavior by file type
Hello.
So, I switch between '2' and '4' spaces for tabs very often. Usually I use 2 spaces for a tab for HTML files and 4 spaces for a tab for programming. Is there anyway to configure VIM, so it will automatically adjust depending on the file extension?
Also, how come VIM indents 8 spaces sometimes, like after I enter an open brace? I have it set to 4 spaces.
Thanks.
set sw=4 ts=4 sts=4 " Defaults: four spaces per tab "
autocmd FileType html :setlocal sw=2 ts=2 sts=2 " Two spaces for HTML files "
Here are three different options: 'shiftwidth' ('sw') controls number of spaces for automatic indentation and some shifting commands (like << in normal mode), 'tabstop' ('ts') controls visual length of a real tab character, you may want to leave defaults (8 visual cells), 'softtabstop' ('sts') controls what is being inserted/removed when you press <Tab> and <CR>. I suggest you either set it to the value of 'tabstop' or set it alongside with 'expandtab' because in other cases it will produce ugly tabs+spaces indentation.
Type :help syntax in vim. This will open a help file giving an overview with subsequent pages/files showing you how to bind file extensions to syntax files where you can :set shiftwidth=2and :set tabstop=2for e. g. HTML files. I guess the syntax files of your installation are responsible for your brace indentation symptom as well.

How to prevent Vim indenting wrapped text in parentheses

This has bugged me for a long time, and try as I might I can't find a way round it.
When I'm editing text (specifically latex, but that doesn't matter) files, I want it to auto-wrap at 80 columns. It does this, except if I happen to be in the middle of a parenthetical clause, it indents the text which is very annoying. For example, this works fine
Here is some text... over
two lines.
but this doesn't
Here is some text... (over
two
lines
If anyone can tell me how to turn this off (just for text/latex files) I'd be really grateful. Presumably it has something to do with the fact that this is desired behaviour in C, but I still can't figure out what's wrong.
:set nocindent
The other options do nothing, and the filetype detection doesn't change it.
There are three options you may need to turn off: set noai, set nosi, and setnocin (autoindent, smartindent, and cindent).
This may be related, when pasting from gui into terminal window, vim cannot distinguish paste modes, so to stop any odd things from occuring:
set paste
then paste text
set nopaste
I had similar issues trying to paste xml text, it would just keep indenting. :)
gvim, the gui version of vim, can detect paste modes.
You can have a look at the autoindent option :
autoindent - ai
Copy indent from current line when starting a new line (typing
in Insert mode or when using the "o" or "O" command). If you do not
type anything on the new line except and then type or
, the indent is deleted again. When autoindent is on,
formatting (with the "gq" command or when you reach 'textwidth' in
Insert mode) uses the indentation of the first line. When
'smartindent' or 'cindent' is on the indent is changed in specific
cases. The 'autoindent' option is reset when the 'paste' option is
set. {small difference from Vi: After the indent is deleted when
typing or , the cursor position when moving up or down is
after the deleted indent; Vi puts the cursor somewhere in the deleted
indent}.
From the official Vim documentation
filetype plugin indent on
This switches on three very clever
mechanisms:
Filetype detection. Whenever you start editing a file, Vim will try to
figure out what kind of file this
is. When you edit "main.c", Vim will
see the ".c" extension and
recognize this as a "c" filetype.
When you edit a file that starts with
"#!/bin/sh", Vim will recognize it as
a "sh" filetype. The filetype
detection is used for syntax
highlighting and the other two
items below. See |filetypes|.
Using filetype plugin files Many different filetypes are edited with
different options. For example,
when you edit a "c" file, it's very
useful to set the 'cindent' option to
automatically indent the lines. These
commonly useful option settings are
included with Vim in filetype plugins.
You can also add your own, see
|write-filetype-plugin|.
Using indent files When editing programs, the indent of a line can
often be computed automatically.
Vim comes with these indent rules for
a number of filetypes. See
|:filetype-indent-on| and
'indentexpr'.
:set noai
sets no auto indent tt may be smartindent though. Check out the doc and see if you can find something more
http://www.vim.org/htmldoc/indent.html

Resources