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

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.

Related

How to override default indentation in 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.

Vim varying indentations [duplicate]

This question already has answers here:
Redefine tab as 4 spaces
(13 answers)
Closed 8 years ago.
I have recently started using vim. I added :set tabstop=4 in my .vimrc to comply with the indentation scheme, but when I opened the file in another editor (gedit) it still uses 8 tab spaces. The formatting looks right inside vim. Any help how I can fix this?
When setting tabstop it is also worth setting softtabstop and shiftwidth to the same value.
I set all the following at the same time (from my .vimrc file)
" tabstop: Width of tab character
" expandtab: When on uses space instead of tabs
" softtabstop: Fine tunes the amount of white space to be added
" shiftwidth Determines the amount of whitespace to add in normal mode
set tabstop =4
set softtabstop =4
set shiftwidth =4
set expandtab
Here is a great tutorial on how and why.
http://vimcasts.org/episodes/tabs-and-spaces/
Make a similar change to the other editor. The width of the tab a property of the editor, not a property of the file.
You need to "convert" tabs to blanks, the following code solves your problem.
:set expandtab

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.

Have vim ai obey the formatting of spaces from the previous line

When I indent/format my code I use tabs for indentation, spaces for alignment. I feel that this is intuitive and very effective at keeping code nicely formatted independent of the tab width of other programmers (as long as they are using monospace).
I like to keep my code nicely formatted for other potential developers and this is one thing about vim that irks me and I want to know if there is a solution. Using the ai format in vim if you have a line that is 2 tabs followed by 9 spaces (assuming 4 space wide tabs) when you enter a new line vim will have the new line be 4 tabs and 1 space. Which is not the desired behavior( I would like it to be 2 tabs and 9 spaces, like the previous line).
My question is, is this even possible? If no, why not? If yes, how do I do it?
Try this in your ~/.vimrc:
set noexpandtab
set copyindent
set preserveindent
set softtabstop=0
set shiftwidth=4
set tabstop=4
It will add spaces though, if your current indentation is not a multiple of tabstop.

How to avoid indentation error after changing tab stops in Vim?

I used to have 8-space tabs in Vim. Then I changed to 4 spaces, but now whenever I add a line to some code I had written before changing to 4 spaces, it gives me an indentation mismatch error even though everything is lining up nicely. Is there any way to avoid this problem?
Have you done a :%retab ...?
Have you changed just the tabstop option?
I use 4 spaces (fill with spaces when I hit tab, to insert actual tab hit ctrl-v tab). Here are the tab related settings from .vimrc:
" tabs
set tabstop=4
set shiftwidth=4
set expandtab
When you fill tab with spaces you will always insert spaces instead of tab and your code will always look the same.
When you use tabs each tool displays tab differently and you end up spending your time setting up how many spaces should be displayed for tab (8,4,3.5) instead of doing productive work.
Or choose one of these (from vim 7.1 help tabstop):
Note: Setting 'tabstop' to any other value than 8 can make your file
appear wrong in many places (e.g., when printing it).
There are four main ways to use tabs in Vim:
1. 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 <Tab> and <BS> will
behave like a tab appears every 4 (or 3) characters.
2. 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.
3. 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.
4. 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.
For python code, you are probably best off with the following:
:set tabstop=8
:set shiftwidth=4
:set expandtab
That way you are still using the 'industry standard' 8 space tabs, but you won't be putting any of them into your file. That should keep your old code clean as well, although you'll have to go back through and manually move everything left over time. You'll definitely want to :retab everything too.
If you want to replace everything with 4 space indents do
:set tabstop=4
:retab
:set tabstop=8
This will re-indent everything using spaces at 4 spaces per tab, and set you back to sane defaults.
Obvously, this is subject to opinion, but in my book using tabs set to anything other than what you get when you cat the file is asking for trouble.
The best way to visualise a mismatch is to :set list which will show whitespace issues.
set listchars=tab:>-,trail:-,nbsp:+ "This is terminal friendly but you can make fancy
set list
I'd say this is an essential setting for python editing when spaced indents are the norm. Especially when a file is edited by a co worker.
I also double checked the style guideunder "Code lay-out". theres a python -tt option you might want to use as specified in http://www.python.org/dev/peps/pep-0008/. That will throw warnings and errors if you mix tabs with spaces.
You could incorporate this into your unit testing. It seems the pep is recommending 4 spaces for newer code. You might want to consider changing if you intend on contributing to open source projects.
also to be extra tidy I have for deleting whitespace at eol
nnoremap <leader>wd :%s/\s\+$//<cr>

Resources