Vimrc: how to replace tabs with two spaces? - vim

This is weird that I used to make it work with my vimrc setting to replace every tab with two spaces, but it doesn't work now.
My old setting was like this:
syntax on
filetype on
set softtabstop=2
set shiftwidth=2
set tabstop=2
set expandtab
set smartindent
But it suddenly didn't work so I have tried this:
syntax on
filetype plugin indent on
set softtabstop=2
set shiftwidth=2
set tabstop=2
set expandtab
set smartindent
with no success(it forces 4 spaces).
How can I make it work to replace tabs with 2 spaces?

Just do this much
syntax on
filetype plugin indent on
set smarttab
set shiftwidth=2
set tabstop=2
set shiftround
" converting tabs to space before writing hte a file
set expandtab
autocmd! bufwritepre * set expandtab | retab! 2

Related

vim indention not preserving spacing

Below is example text I put in vim, only spaces used no tabs.
dog;
dogcat
dogcatmoose
box
box car
woop
woopadoop
Lets say I highlight all code, and use indention > what happens is the following:
dog;
dogcat
dogcatmoose
box
box car
woop
woopadoop
Original spacing is not preserved. I dont want that, I want spacing to be preserved so it looks like this:
dog;
dogcat
dogcatmoose
box
box car
woop
woopadoop
I tested with tabs instead of spaces. When using tabs, indention preserves the tabs.
Here is my vimrc. IS there some value in here causing this problem? Is there a value I can add to fix the problem?
set number
set nowrap
set linebreak
set tabstop=4
set softtabstop=4
set shiftwidth=4
set shiftround
set expandtab
set title
set mouse=a
set hlsearch
set smarttab
set autoindent
set smartindent
set background=dark
filetype indent on
set filetype=html
set smartindent
set nocompatible
filetype off
set nobackup noswapfile
set clipboard=unnamed
let &colorcolumn=join(range(81,999),",")
highlight ColorColumn ctermbg=235 guibg=#2c2d27
"Stop vm from indenting on comment out sections starting with "#"
set indentkeys-=0#
My suggestion is to get rid of set shiftround.
It seems like set shiftround coupled with set shiftwidth=4 is causing the behaviour. shiftround tries to indent the text to multiples of 4 (which is set by shiftwidth), while your text is indented with 2 spaces. So when you do >, shiftround makes all the lines have the same 4-space indent. You can experiment this with the same text but with 4-space indentation; your setting will preserve the indentation.

Check variable change location in VIM? Or how to set shfitwidth?

This is not a very simple question.
I found that the "shiftwidth" for python files are set to 4, while I have these at the bottom of my .vimrc:
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
The shiftwidth are 2 for all other filetypes; but it's 4 only for python.
I want to know if it's possible to see where a variable (in my case, the shiftwidth) has been changed; if this is not possible when what are the possible locations that this variable is changed..? And is there any way go make the shiftwidth=2 universally... even for the python files? (Although it's also recommended of using 4 spaces for python, but I personally prefer 2)
:verbose set shiftwidth
tells you what's the current value and where it was defined. See :help :verbose.
Filetype plugins are sourced after the corresponding FileType event was triggered so they will override the options in your vimrc.
The dirty way to add filetype-specific settings to your config is to add something like this to your vimrc:
augroup Python
autocmd!
autocmd fileType python setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
augroup END
See :help :autocmd.
The clean way is to put your desired settings into a custom ftplugin.
The file:
~/.vim/after/ftplugin/python.vim
Its content:
setlocal tabstop=2
setlocal shiftwidth=2
setlocal softtabstop=2
setlocal expandtab
Note the use of setlocal instead of set because you don't want those settings to be applied everywhere.
This is probably being set by the built-in ftplugin file for Python. If you
:e $VIMRUNTIME/ftplugin/python.vim
and search for shiftwidth, you’ll probably find it set there to 4.

Autoindenting not working in vim?

I am currently using vim and wish to autoindent every time I need to (such as after a brace in javascript or after a colon in Python). For some reason, I tried autoindent and smartindent, but every new line gave me 2 tabs instead of one. Why is it doing this? I thought it would only insert one tab.
My current ~/.vimrc file has:
set ts=4
set autoindent
set smartindent
filetype plugin indent on
you need to set up as well:
:set shiftwidth=4 softtabstop=4
tabstop is only for the number of columns a real "\t" tab takes:
:he shiftwidth
Number of spaces to use for each step of (auto)indent. Used for
|'cindent'|, |>>|, |<<|, etc.
When zero the 'ts' value will be used.
:he softtabstop
Number of spaces that a <Tab> counts for while performing editing
operations, like inserting a <Tab> or using <BS>. It "feels" like
<Tab>s are being inserted, while in fact a mix of spaces and <Tab>s is
used.
whereas tabstop:
:he tabstop
Number of spaces that a <Tab> in the file counts for. Also see
|:retab| command, and 'softtabstop' option.
As a bonus, here's a few mappings I've set up for that, when I need to work on projects that do not use my favorite default of expand tabs with 4 spaces indent:
nmap <Leader>t2 :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
nmap <Leader>t4 :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
nmap <Leader>t8 :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
nmap <Leader>T2 :set noexpandtab tabstop=2 softtabstop=2 shiftwidth=2<CR>
nmap <Leader>T4 :set noexpandtab tabstop=4 softtabstop=4 shiftwidth=4<CR>
nmap <Leader>T8 :set noexpandtab tabstop=8 softtabstop=8 shiftwidth=8<CR>
I use like this in my .vimrc
filetype plugin indent on
set smartindent
set autoindent
set shiftwidth=4
set expandtab
set tabstop=4
set softtabstop=4
The expandtab will replace the tabs with spaces, I think this will be perfect for coding.

Automatic Hard vs Soft tab indentation based on type of file?

I'd like to default to soft tabs, indented two spaces (but hard tabs displayed as two spaces for Makefiles), and for vim to reindent appropriately on save.
See this question. For your case, you would want this in your .vimrc:
set tabstop=2
set softtabstop=2
set expandtab
And this in ~/.vim/after/ftplugin/make.vim:
setlocal noexpandtab
You can use autocmd for this, so it pretty much becomes a oneliner in your.vimrc
autocmd Filetype jade setlocal ts=2 sw=2 expandtab
autocmd Filetype yaml setlocal ts=2 sw=2 expandtab
Everything else will still use the global defaults.

Can someone help fix my vimrc config?

set nohlsearch
set ai
set bg=dark
set showmatch
highlight SpecialKey ctermfg=DarkGray
set listchars=tab:>-,trail:~
set list
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
syntax on
set listchars=tab:>-
set listchars+=trail:.
set ignorecase
set smartcase
map <C-t><up> :tabr<cr>
map <C-t><down> :tabl<cr>
map <C-t><left> :tabp<cr>
map <C-t><right> :tabn<cr>
map <F1> <Esc>
imap <F1> <Esc>
set pastetoggle=<F5>
This is my vimrc. I want to fix it so that it doesn't show >---- >---- when other people tab. Thank.
Remove these lines:
set listchars=tab:>-,trail:~
set listchars=tab:>-
set list
is doing this. You can
:set nolist
if it is bothering you but I recommend keeping it for python files (you might be coding in python looking at that autocommand). ie. ... Learn to love it. create a mapping if it is a regular issue.
Also there is
:retab
to remove tabs if you have an expandtab setting.

Resources