Vim doesnt respect vimrc indentation settings - vim

I am new to using Vim, and have been using "The Ultimate Vim configuration" on GitHub. It uses 4 spaces wide tabs as default, so to change that, I changed my .vimrc file to the following:
set runtimepath+=~/.vim_runtime
source ~/.vim_runtime/vimrcs/basic.vim
source ~/.vim_runtime/vimrcs/filetypes.vim
source ~/.vim_runtime/vimrcs/plugins_config.vim
source ~/.vim_runtime/vimrcs/extended.vim
" Own settings
set number
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab
try
source ~/.vim_runtime/my_configs.vim
catch
endtry
The my_configs.vim file is deleted, so I know that the indentation settings are not being overwritten there.
The problem is, if i source the vimrc file from vim with :source ~/.vimrc The indentation works perfectly, but if I close Vim and open again, then the indentation is back to 4 spaces wide instead of 2 as specified in the vimrc... All other settings from the Ultimate Vim Configuration get loaded from vimrc but not the indentation settings, which is really frustrating, as I need to source the vimrc file every time i want to use vim.
Any help is greatly appreciated, I really would like to exclusively use Vim but if this cannot be solved then I need to keep using Visual Studio Code :(
Thank you in advance.
EDIT: Here is the output of running command :verbose set tabstop? softtabstop? shiftwidth?:
tabstop=2, Last set from ~/.vimrc line 10
softtabstop=2, Last set from ~/.vimrc line 11
shiftwidth=2, Last set from ~/.vimrc line 12

When you're editing a Python file in Vim, it will auto-detect the file type and apply Python specific settings. Amongst those, it will set the soft tab stop recommended by PEP 8.
You can find these settings in ftplugin/python.vim in your Vim runtime:
if !exists("g:python_recommended_style") || g:python_recommended_style != 0
" As suggested by PEP8.
setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
endif
You can override those settings with a global variable, which you can set in your vimrc. To disable the Python tab stop automatically set by Vim, include the following somewhere in your vimrc:
let g:python_recommended_style = 0
Note that those settings are there for a reason, they're pretty standard for Python and it's highly unusual to see Python code indented by a different number of spaces... But you have the knob to override it if you really want to.

Related

How to disable linting in vim (no, it's not ALE)

Recently, the vim built into my Mac has started to show me linting feedback visually whenever I save. This is annoying and I'd rather it not happen. (Previous 20 years of using vi/vim did not have this behavior.)
My web searching suggests perhaps it's ALE, so I tried to disable it with :ALEToggle, but vim replied with "E492: Not an editor command: ALEToggle." So I don't think it's ALE.
The gutter shows many instances of S> as the indicator of where linting feedback resides, and then highlights characters within those lines as points of concern. See examples in screenshot below.
What linter uses S> and how do I disable it globally, for all files? I don't need or want this feature.
The relevant parts of my ~/.exrc file are here. (Note that things like ^H had to be typed out, because trying to paste them here failed for obvious reasons.) There is a little more than this in my file, but everything not shown here I've tried commenting out and it doesn't help, so it's not the problem.
execute pathogen#infect()
set ai
set ts=4
syntax on
set expandtab
set nu
filetype indent off
set softtabstop=4
set shiftwidth=4
set smarttab
set hlsearch
set ignorecase
set t_Co=256
colorscheme wombat256mod
map ^H :nohlsearch<Enter>
set mouse=a
set relativenumber
It looks like you're getting style warnings from Syntastic.
Either you can remove the plugin by deleting ~/.vim/bundle/syntastic or you could try adding this to your Vim config:
let g:syntastic_mode_map = {"mode": "passive"}

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 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 can I find out why vim keeps changing my expandtab setting

I use vim. Specifically I am using Janus. I have expandtab set. However, during the course of using vim, for some reason, my expandtab setting gets set to noexpandtab, and my files start to gain hard tabs. I have tried typing :verbose set expandtab? but this does not show me anything (specifically, it shows me that noexpandtab is set, but it doesn't show a file that is responsible for setting it).
So I would like to find out:
Why my expandtab setting might be changing
How I can track down the culprit and prevent it from happening
Thanks
Try this
:verb set expandtab?
:verb set et?
:verb set invexpandtab?
expandtab can really be set in a number of ways :/
I should add that in addition to the above if you tried the following:
:verb set expandtab?
:verb set et?
:verb set invexpandtab?
And you get back with no line number or file:
noexpandtab
It's more than likely that you have the following and need to change the order:
set expandtab
set binary
set noeol
Change to (note the order)
set binary
set noeol
set expandtab
The reason for this is due to set binary command has a few defaults it executes after it runs. Including the following:
'textwidth' will be set to 0
'wrapmargin' will be set to 0
'modeline' will be off
'expandtab' will be off
I noted DavidWinterbottom called this out in his comment, but that seemed to be the only comment that was hidden by default and this caused me to do a lot more reading that was necessary, so hopefully this post helps another poor soul from wasting countless hours tracking this down.
VIM settings can either be set in a configuration file, or in a modeline inside the file you're editing. Note that expandtab can be shortened in VIM to et, so be sure to look for that as well.
Possible configuration files I'd look for:
/etc/vim/vimrc and other files in that directory (sometime vimrc.local and such)
$HOME/.vimrc
As for modelines, they are simply configuration options for VIM, located as comments in the file itself. If this only happens with some files but not others, look for comments that look something like:
/* vim: set noet ai tw=75: */
And try to remove them and see if it helps.

Vim - indent like Emacs

I use vim (primary so that I can work on plain ssh terminal - still uncomfortable with Emacs non-gui version) but most of my colleagues in the organization use emacs. So using CVS, we face indentation inconsistency issues (tabs becoming spaces, number of tabs/spaces, code layout etc).
Is there a way I can make VIM indent EXACTLY as EMACS. (similar to the default emacs profile my colleagues use).
(Most importantly, I want vim's C++ and TCL indentation schemes to match that of emacs).
regards,
JP
I don't know if there's a way to directly import Emacs indentation settings into vim, but you can probably configure the same behavior in vim itself:
set expandtab will convert tabs to spaces
set autoindent will keep indentation level from previous line
set shiftwidth=4 will affect block indentation with >> and <<
set softtabstop=4 sets the length of soft tab in spaces
set tabstop=8 sets the width of tab character
This is properly explained in vim wiki.
When you need filetype-specific indentation you have two options:
Set autocmd to change indentation on file read and file creation:
au BufRead,BufNewFile *.py,*pyw,*.html,*.js set shiftwidth=4 will set shiftwidth for *.py files.
Configure filetype plugin, create name.vim scripts inside .vim/ftplugin folder for specific file types and set the described variables there. This is also described in proper detail in vim wiki.
Regarding specialized indenting for c++ and TCL there is some special stuff that applies in aditon to all the other setting info that's been suggested. Vim has special indenting rules defined in code for different languages. Some of this is found is found in the /indent directory of the vim installation, where there is a separate file for each filetype. For more information on how this works read the help for 'indentexpr'.
The c indenting -- and I think also the indenting for c++ -- is mostly defined in Vim source-code, and has a zillion options that you can set, plus is specially configurable in c.vim or c++.vim indent file. Read help for 'cindent' and 'c-indenting' for more help on that.
In short, the tcl.vim file controls special indenting for tcl files. If you want to revise how indenting works with tcl you would want to alter the main function in that file. The c/c++ indenting is largely controlled by Vim internals but with lots of different option flags. You can control c/c++ indenting by configuring the options the way you want them and/or by writing a function for the indent file in /indent directory. (I believe there is no c++ file in /indent directory, not sure if c.vim is file to edit there, or whether you need to create new c++.vim file. I think it's the c.vim file that would be used. which is basically an empty shell in standard Vim install, but you can read other *.vim indent files to get the idea of how they work.
Here's as extract of some options concerning indentation from .vimrc:
set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
All options are nicely described in vim help:
:help smartindent
:help autoindent
UPD: also for C-like languages you may consider :help C-indenting

Resources