Vim settings constantly getting reset - vim

I am using vim inside tmux. For some reason, my vim settings are getting constantly reset. --EDIT-- more detail: specifically, tabstop and autoindent are being set to default values, namely tabstop=8 and noautoindent. I don't think its something in my settings that is setting them to that, because when I type :so $MYVIMRC it resets to the proper values from my vimrc. I think vim is somehow "forgetting" my settings?
I haven't been able to figure out exactly what is causing it, but it happens pretty frequently, almost every couple of minutes. It seems to happen most often when I focus on another window, or switch panes in tmux. But it doesn't happen every time, and sometimes it just happens while typing. I have no idea what the problem is but its very frustrating. Also, it seems to happen most with python, slightly less with javascript, and even less frequently with PHP or other languages. Though this could be that I spend most of my time working in python and javascript...
I was having a problem earlier where I was getting gibberish entered into my status bar: Vim inside Tmux: <C-w>l (swapping between vim splits) enters ^]lfilename^] into vim. That fixed that issue, but seems to have caused this new one.
Here are what I think are the relevant parts of my .vimrc, .tmux.con and .bashrc. These are all of my settings, I didn't include keybindings.
.vimrc
set nocompatible
set showmatch
execute pathogen#infect()
syntax enable
filetype plugin indent on
colorscheme desert
set t_Co=256
set shiftwidth=4
set softtabstop=4
set backspace=indent,eol,start " consume expanded tabs if possible
set expandtab
set shiftround
set autoindent
set relativenumber
set showmode
set showcmd
set hidden
set autoread
set ignorecase
set smartcase
set incsearch
set autochdir
set laststatus=2
set statusline=%<%F\ %h%m%r%=%-14.(%l,%c%V%)\ %13.L\ %P
set titlestring=%F
set splitbelow
.bashrc:
export TERM=screen-256color
.tmux.conf
export TERM=screen-256color

Some settings are local to a buffer or window. Indent settings, e.g. 'shiftwidth', 'softtabstop', and 'expandtab', are local to a buffer and not global. This makes sense because different filetypes have different needs. A good example of types that need completely different indent settings would be python and makefile.
Setting up indent setting per filetype are usually done one of the following ways:
Use modelines for each file. Gross! (:h modeline)
Use autocmd's in your ~/.vimrc. e.g. autocmd FileType c,cpp,cs,java setlocal shiftwidt=4 softtabstop=4 expandtab
Put these setting in ~/.vim/after/ftplugin/python.vim. Replace python with any filetype you want to have specific settings for.
Note: You can find a buffer's file type via :set ft?
Personally I like the after directory structure as it is nice and neat and keeps the clutter out of my ~/.vimrc file.
For more help see:
:h local-options
:h 'sw
:h 'rtp
:h after-directory
:h ftplugin-overrule

You said you work in javascript and python and that you notice the difference when changing between them. Are you sure this is changing and not that you get different behavior for javascript and python?
Note the pathogen#infect(). You probably have something like syntastic installed which in turn will have lint tools for javascript and python. Those tools may have file type specific indentation settings. If you have something following PEP8 for python it's probably defaulting to spacing instead of tabs for indentation.
Check .vim/ftplugin and .vim/ftdetect, filetype specific settings can be put there which will override the default behavior specified in your .vimrc.

Related

set nospell in .vimrc has no effect

I am trying to disable spellcheck in vim.
I have tried adding set nospell to my .vimrc but that has no effect. If I type :set nospell while editing a file it works - but I want to disable it globally. Any suggestions?
If you follow :help 'spell', you'll see that this option is local to window. If you use :set during Vim startup (e.g. in your ~/.vimrc), you'll set the global default and the first window's option, but this doesn't mean that filetype plugins or other :autocmd cannot change the option again later.
Vim can usually tell you who set an option last, via
:verbose set spell?
This often gives you a hint how to undo or prevent an undesired configuration change. Plugins usually have configuration, and filetype plugins can be overridden via the after-directory.

Does anybody get these weird repeated ()()()()()() in Vim?

Couple of times a day I get this, usually when typing in a browser() statement while debugging.
It repeats a whole bunch of brackets, when I only typed a pair. Usually I very quickly type "browser() esc :w enter" (without the quotes) so that I can save and go back to my R environment, and it pauses for a second, and then sticks all these brackets in. It's not really a problem, a quick Undo takes it out, just wondering why it does this. Am I moving too fast for Vim (touch typist).
I'm using Vim 7.3 on Windows 7. Does this on more than one machine. In all cases I'm also mapping caps lock to escape using sharpkeys, I don't know if that might be a factor. Anybody seeing this? Any remedy?
Thanks.
For completeness, here is my vimrc file:
colorscheme clarity
syntax enable
set guifont=Consolas:h10:cANSI
set number
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set autoindent
set smarttab
set backspace=indent,eol,start
set nocompatible
set expandtab
filetype plugin on
filetype indent on
set ruler
set cursorline
set noerrorbells
set visualbell
"set guioptions-=m
set guioptions-=T
I am guessing on line 32 you had:
browser
Then you typed:
90a()<esc>
The mistake here being that typed "90", thinking that you were in insert/append mode, realized you weren't went into that mode, typed your parenthesis, then exited. What you actually told vim was "Append this text 90 times", and it gladly did it for you, so you ended up with:
browser()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
It's ok, it happens to the best of us.

vim: autoindent not working

my autoindent is not working, any diagnostic tests to figure it out?
my ":set" is:
:set
--- Options --- cindent laststatus=2 scroll=17
tabstop=4 window=36
filetype=cpp number
smartindent ttyfast
helplang=en paste
syntax=cpp ttymouse=xterm2
backspace=indent,eol,start
fileencoding=utf-8
fileencodings=ucs-bom,utf-8,default,latin1
printoptions=paper:letter
runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim72,/usr/share/vim/vimfiles/af
ter,/var/lib/vim/addons/after,~/.vim/after
suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
try:
:set ai
or:
:set autoindent
find more about auto-indent:
:h ai
Otherwise, it's might be something with file type detection.
I had a stale function in indentexpr which persisted after changing the filetype. This eventually fixed it for me:
:set indentexpr=
In case someone else face the same issue, I had a similar issue that none of the above fixed.
What was wrong for me was the tab interpretations. here is the set up that made it work:
set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
And to check when indenting if the indentation was correct, I added the following, still in my vimrc file:
" helper for indent mistake
set list listchars=tab:»·,trail:·
Which display a "»" instead of the regular "·" if my indent is wrong. Very handy.
Hope it helps.
I had the same issue and these settings fixed it.
filetype on
filetype plugin on
filetype indent on
You should probably turn off smartindent and use :filetype indent on and cindent (which seems to be also set) instead.
Here's one way to test out whether you have the configuration correct, then persist your configuration so Vim always operates thusly. This font indicates text which should be typed in literally, except <CR> means press the "Enter" or "Return" key.
Create a new system user, with a new home directory.
Start Vim. All settings should be set however they ship with Vim by default.
Open a file, say, test.txt.
Make sure autoindent is enabled (:set ai?<CR>)
Prove that autoindent doesn't happen:
Type a space or two, then hit enter.
When the cursor advances to the next line, it should return to column 1, the far-left column.
Turn on autoindent (:set ai<CR>)
Make sure autoindent is enabled (:set ai?<CR>)
Prove that autoindent happens:
Type a space or two, then hit enter.
When the cursor advances to the next line, it should still be in the same column.
Persist autoindent with :mkvimrc<CR>.
Hope that helps! Here are some other notes:
These instructions might be specific to left-to-right locales.
Here's my vimrc
"The 'autoindent' option is reset when the 'paste' option is set". So try to remove 'paste' from your settings (vim-options).
:set smartindent? showd: nosmartindent
Then activating it with :set smartindent solved problem for me.
I had the same problem, and I have tried many commands, all failed.
At last, I use the following command, and it works:
autocmd VimEnter * set autoindent
It's not a elegant method, however, it does works.

How to use only tab (not space) in vim

I prefer to use tab than white space(may be a little different from most of others)
But I found, when I hit Enter at the end of line, it will add some white spaces, but not tab. So, I have to delete them and press tab.
I want to know how to set vim as:
use only tab to indent the lines
a tab looks like 4-spaces, but actually is a tab
when hit enter at the end of a line, the new line is started with only tabs
I've googled for this for a while, but not found a good answer. Thank you in advance
UPDATE
The answer #Alok has provided works well in most of cases. But I just found, sometimes, it depends on the file type. For example, if you are editing a haml file, and there is a haml.vim in your vimfiles/indent/, then all the tabs will be converted to space. So if you want it to be tab only, you should modify(or delete) the corresponding indent file.
The settings you are looking for are:
set autoindent
set noexpandtab
set tabstop=4
set shiftwidth=4
As single line:
set autoindent noexpandtab tabstop=4 shiftwidth=4
autoindent can be replaced with smartindent or cindent, depending upon your tastes. Also look at filetype plugin indent on.
http://vim.wikia.com/wiki/Indenting_source_code
Late to the discussion, just for anyone who struggled to force the use of tabs. With the current version of vim (>8) it was required to set:
:set noet ci pi sts=0 sw=4 ts=4
which expands to:
:set noexpandtab
:set copyindent
:set preserveindent
:set softtabstop=0
:set shiftwidth=4
:set tabstop=4
We can also use this setup with only specific file types, taking advantage of setlocal:
autocmd FileType c,cpp,java,python setlocal noet ci pi sts=0 sw=4 ts=4
For further reference, see: https://vim.fandom.com/wiki/Indent_with_tabs,_align_with_spaces
In my case set indentexpr= did the trick.
I have found the config files which set the annoying behavior with fd python.vim /. In /usr/share/vim/vim90/indent/python.vim there is the line setlocal indentexpr=python#GetIndent(v:lnum). The function python#GetIndent is defined in /usr/share/vim/vim90/autoload/python.vim. This function returned a weird value which was not a multiple of shiftwidth and tabstop. Therefore vim inserted a tab followed by some spaces.
Now, that I have set indentexpr to an empty value, I think vim falls back to autoindent. smartindent and cindent are turned off. (https://vimhelp.org/indent.txt.html)
Of course set noexpandtab is required, too. I am not sure if any other settings are relevant here.
EDIT: autoindent is not perfect, it does nothing more than indent the new line as much as the previous line.
set smartindent
set cinwords=if,elif,else,try,except,finally,with,for,while,class,def
does not work as expected and cindent does not seem suitable for python, so using indentexpr is the way to go after all.
Luckily the indentation function can be configured, see help ft-python-indent or look at the code – that was more informative regarding the disable_parentheses_indenting feature. It seems this is the solution for me:
if !exists('g:python_indent')
let g:python_indent = {}
endif
let g:python_indent.disable_parentheses_indenting = 1
(https://vi.stackexchange.com/a/38824/43863)

How do I change tab size in Vim?

Every time I add a selector in CSS and I press Enter to define the properties it ends up like this:
#selector {
property: value;
}
(8-space tabs)
How can I configure Vim to make it like this:
#selector {
property: value;
}
(4-space tabs)
:set tabstop=4
:set shiftwidth=4
:set expandtab
This will insert four spaces instead of a tab character. Spaces are a bit more “stable”, meaning that text indented with spaces will show up the same in the browser and any other application.
To make the change for one session, use this command:
:set tabstop=4
To make the change permanent, add it to ~/.vimrc or ~/.vim/vimrc:
set tabstop=4
This will affect all files, not just css. To only affect css files:
autocmd Filetype css setlocal tabstop=4
as stated in Michał's answer.
Expanding on zoul's answer:
If you want to setup Vim to use specific settings when editing a particular filetype, you'll want to use autocommands:
autocmd Filetype css setlocal tabstop=4
This will make it so that tabs are displayed as 4 spaces. Setting expandtab will cause Vim to actually insert spaces (the number of them being controlled by tabstop) when you press tab; you might want to use softtabstop to make backspace work properly (that is, reduce indentation when that's what would happen should tabs be used, rather than always delete one char at a time).
To make a fully educated decision as to how to set things up, you'll need to read Vim docs on tabstop, shiftwidth, softtabstop and expandtab. The most interesting bit is found under expandtab (:help 'expandtab):
There are four main ways to use tabs in Vim:
Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters.
Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will always insert spaces. The formatting will never be messed up when 'tabstop' is changed.
Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values when editing the file again. Only works when using Vim to edit the file.
Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then work (for initial indents only) for any tabstop setting that people use. It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' is changed.
As a one-liner into vim:
:set tabstop=4 shiftwidth=4
For permanent setup, add these lines to ~/.vimrc:
set tabstop=4
set shiftwidth=4
set expandtab <-- (optional) 4-spaces instead of Tab indentation
Several of the answers on this page are 'single use' fixes to the described problem. Meaning, the next time you open a document with vim, the previous tab settings will return.
If anyone is interested in permanently changing the tab settings:
find/open your .vimrc - instructions here
add the following lines: (more info here)
set tabstop=4
set shiftwidth=4
set expandtab
then save file and test
UPDATE
If you are working in a particular project I highly recommend using editorconfig.
It lets you define an .editorconfig file at the root of your repository defining the indentation you want to use for each file type across your repository.
For example:
root = true
[*.css]
charset = utf-8
indent_style = space
indent_size = 4
[*.js]
charset = utf-8
indent_style = space
indent_size = 2
There is a vim plugin that automatically configures vim according to the config file for file you open.
On top of that the .editorconfig file is automatically supported on many other IDEs and editors so it is the best option for collaborating between users with different environments.
ORIGINAL ANSWER
If you need to change sizes often and you don't want to bind this to a specific file type you can have predefined commands on your .vimrc file to quickly switch preferences:
nmap <leader>t :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
nmap <leader>m :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
This maps two different sets of sizes to keys \t and \m. You can rebind this to whatever keys you want.
in my .vim/vimrc (vim 8.0 under ubuntu bionic), I have
if has("autocmd")
filetype plugin indent on
endif
so added line like :
autocmd Filetype css setlocal tabstop=2
doesn't work.
I created .vim/indent folder and added in :
css.vim with
set tabstop=2
set shiftwidth=2
set softtabstop=2
and it works !
I tried to on another computer with ubuntu focal and vim 8.1, it doesn't work !-(
in vim command mode, write:
:set ts=X
where X is your new desired space length

Resources