How Do I Fix Inconsistent Vim Auto-Indent Across Buffers?

2025-09-04 15:03:11 207

4 Answers

Wyatt
Wyatt
2025-09-06 02:49:38
I've had this happen when I hop between different projects and forget each repo's style. The quickest diagnostic trick I use is: open the two buffers, run :set filetype? and :verbose set shiftwidth? tabstop? softtabstop? expandtab? — that immediately shows if a buffer has a different filetype or which script set the option. If a plugin or ftplugin is the culprit, :scriptnames will help locate it.

For a durable fix I pick a default in my config and then allow exceptions: put global defaults like set tabstop=4 shiftwidth=4 softtabstop=4 expandtab in your vimrc (or init.lua), then add ftplugin/ files for languages that need different rules. If you want to enforce one style everywhere, a simple autocmd works: autocmd BufRead,BufNewFile * setlocal tabstop=4 shiftwidth=4 softtabstop=4 expandtab. Also check for .editorconfig in project roots—if you use the plugin it can override Vim settings, so adjust that file or disable the plugin. Finally, convert mixed files with :retab so old files don’t keep surprising you.
Hannah
Hannah
2025-09-06 07:27:22
I tend to approach this from the plugin/LSP era perspective: sometimes inconsistent indentation comes from external formatters (Prettier, stylua, gofmt) or from language-specific indentexpr functions. First thing I do is disable automatic formatting and indentation providers temporarily and see if the problem persists — for example, turn off your LSP formatting or a background formatter and edit buffers manually.

Then I inspect :setlocal indentexpr? indentkeys? and :verbose set shiftwidth? to see if a language plugin set an indentexpr. If you find that an indent script is the source and you prefer your own style, drop a file in after/plugin or ftplugin/ to override those values (e.g., setlocal shiftwidth=2 tabstop=2 expandtab). For Neovim with an init.lua I sometimes add an autocmd on BufEnter to normalize settings per filetype, but I prefer proper ftplugin files so they’re explicit and discoverable.

I also rely on .editorconfig for multi-language repos; it keeps editors in sync and avoids surprises when teammates commit files with different whitespace. If there’s mixing of tabs and spaces in existing files, use :retab or a formatter to normalize them, and run a linter/formatter in CI so the repo becomes self-healing. That workflow has kept my buffers behaving on both small and large projects.
Liam
Liam
2025-09-09 19:24:17
Okay — let me walk you through this like we’re debugging a stubborn editor together. In my experience inconsistent Vim indentation across buffers usually comes down to a few culprits: buffer-local options, filetype-specific plugins, modelines in files, or external tools like an .editorconfig plugin.

First, check what each buffer actually has set. Use :setlocal and :verbose set shiftwidth? tabstop? softtabstop? expandtab? and :set filetype? and :verbose set autoindent? — the verbose form tells you where a setting was last changed. If you see different values between buffers, that’s your clue: something is changing options per file. Often a ftplugin or indent script is overriding global settings, or a modeline inside a file is setting tabs/spaces.

To fix it, pick a consistent baseline in your vimrc/init.vim: filetype plugin indent on (or in Neovim, enable filetype and indentation early), then set sensible defaults like set tabstop=4 shiftwidth=4 softtabstop=4 expandtab or use set noexpandtab for projects that prefer tabs. If a project has specific rules, add an .editorconfig file and install the editorconfig plugin or add autocmds to apply per-filetype settings. When you need to find the source of an override, :scriptnames shows loaded scripts and :verbose set
Peyton
Peyton
2025-09-10 17:49:37
If you want a no-nonsense checklist to fix inconsistent Vim indentation across buffers, here’s what I do fast:

1) Compare settings between two problematic buffers: :set filetype? and :verbose set shiftwidth? tabstop? softtabstop? expandtab? — that tells you which buffer differs and where the value was set.
2) Check for modelines inside files (modeline can set options per file) and for .editorconfig files in the project root which can override Vim when you have the plugin. Disable temporarily to test.
3) Ensure you have enabled filetype plugins early (filetype plugin indent on) and put sane defaults in vimrc: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab.
4) If a plugin or ftplugin keeps overriding you, create ftplugin/.vim with setlocal ... or use autocmd BufRead,BufNewFile to enforce settings.

Do these steps and you’ll usually find the override quickly; once fixed you can happily stop worrying about rogue tabs and spaces.
View All Answers
Scan code to download App

Related Books

Fix Me
Fix Me
A Billionaire, Frederick falls deeply in love with a broken woman, Kharis, who later becomes his maid. A billionaire and maid are not a perfect match right! And even though they fall in love, it is rare before such a relationship works out. Frederick is already betrothed to a model; Ivy and the wedding is in two weeks. What will happen after Ivy accuses Kharis of sleeping with Frederick’s driver, Lois? Will Frederick be able to fix Kharis after all? Will Ivy consider marrying Frederick with Kharis in the picture? Will Frederick’s parents let them be together? Will Kharis forgive Frederick and marry him?
Not enough ratings
5 Chapters
I DO
I DO
It's a coalition of parallel worlds trying to survive a new and uncertain phase called marriage. It's the hurting, The loving, It's the sex, The secrets, It's the moment they said I DO. *** Marrying a billionaire and going from rags to riches wasn't at all what Dawn had foretold for herself but when the former becomes the latter, she finds herself sharing vows with a retired fuckboy who has quite the reputation in slutry. However, as time progresses, the newlyweds both realize that; it isn't what happens on the outset that matters, it's the rest of the other days when you have to live in a whole new world called marriage—where sometimes the steamy sex and miscellaneous extravaganzas aren't enough to keep the secrets at bay.
Not enough ratings
18 Chapters
How I Became Immortal
How I Became Immortal
Yuna's life was an unfortunate one. Her lover(Minho) and her cousin(Haemi) betrayed her and that resulted in her execution. The last words she uttered was that she was going to seek revenge if she ever got another chance! God as the witness, felt bad for poor Yuna and so he gives her the ability to remember everything in all of her lifetimes. She was planning on seeking revenge but unfortunately her plans didn't come to fruition. She was reincarnated into the modern era. During her 2nd lifetime, she becomes a successful engineer and moves on from her past lifetime. Unluckily for her, during her 3rd lifetime she gets reincarnated back to the past. Her plans change once again. She doesn't love Minho nor does she care about being empress. She decides on a new life without all of the chaos and scheming in the palace. Join Yuna on her journey to seeking a peaceful and successful life in the ancient period. Hi. Thanks for taking the time to read my novels:)
10
97 Chapters
Fix My Heart
Fix My Heart
Kaia Carson just got the job of her dreams, but with it comes a distraction she really does not need in her life. Will meeting Beau Navarro be the best thing to happen to her or will it destroy all the progress she thought she had made to get here? All Mr. Navarro knows is that he wants that woman for himself, to hell with what anyone thinks!
Not enough ratings
86 Chapters
How I Became Legend?
How I Became Legend?
She was once a woman—a lesbian to be exact—in her past life, fantasizing about having a date with beautiful girls and dreaming to act like a real man does someday. But she was afraid to show her true colors because she was living in a judgemental society. Not until, she was trapped in a burning hospital building, trying to save an old woman before herself but only to find out that old woman was only an apparition of a deceased person. She died there, sacrificing her life for nothing. Many things happened in her mind before she runs out of breath. The next thing happened, she emerged from a bamboo tree and woke up into another realm. And to her surprise, she was reincarnated as a teenage guy possessing magical skills. She is Princess Maria Isabelle De Lata who later known as Reign Thunderstorm in the magical world of Artesia. And this is her… wait a minute… and this is the story of how she or… he became a legend.
10
4 Chapters
Across the Desk
Across the Desk
When Deanna finds out that she has to do one more thing to graduate she is taken by surprise. She has to go to the one professor she had a crush on years before and see if he will take her on as a TA. Max looks up to see the one student he wanted in the five years he had been teaching standing there asking for a job. After his internal debate he accepts but he finds he has certain conditions. Everything around the two starts to fall apart as they grow together. The three book series is now complete.
9.8
55 Chapters

Related Questions

What Vim Auto-Indent Commands Adjust Indent Width?

4 Answers2025-09-04 09:02:52
If you're fiddling with Vim's indentation and want precise control, the trio I reach for is :set shiftwidth, :set tabstop, and :set softtabstop. shiftwidth (sw) controls how many spaces a single indentation level uses for operations like >>, <<, and automatic indentation. I usually do :setlocal shiftwidth=4 for projects that use four-space indents. tabstop (ts) sets how many spaces a literal TAB character displays as; use :set tabstop=4 to make existing tabs line up visually with your intended width. softtabstop (sts) affects insert-mode behavior: :set softtabstop=4 makes pressing Backspace or Tab behave like you're working with 4-space logical tabs even if actual file uses tabs. A couple of other practical commands I keep in my .vimrc: :set expandtab to insert spaces instead of real tabs (or :set noexpandtab to keep tabs), :set autoindent to keep the previous line's indentation, and :set cindent or :set smartindent for C-like auto-indenting. If you want the changes to apply only to the current buffer, use :setlocal sw=2 ts=2 sts=2. To reformat an entire file after changing settings, I often run gg=G to reindent the whole buffer, or :retab to convert tabs to spaces (or the reverse with :retab!). These little tweaks saved me hours when I was switching between Python, Makefiles, and Go projects.

How Do I Disable Vim Auto-Indent Temporarily?

4 Answers2025-09-04 20:03:23
Okay, here's a practical and friendly way I handle Vim's auto-indent when I need it out of the way for a few moments. If I just want to paste something without Vim reformatting it, I usually toggle paste mode: :set paste to turn it on, paste the text, then :set nopaste to go back. I often map a key for that so it’s painless, for example :set pastetoggle= or put in my config nnoremap :set paste! to flip it. Paste mode stops auto-indent, indentexpr, and other niceties, so your pasted code won't get mangled. If I need to disable automatic indentation for editing (not just pasting), I prefer buffer-local switches so I don’t mess with other files: :setlocal noautoindent nosmartindent nocindent and, if needed, :setlocal indentexpr= to clear any expression-based indent. To restore, use :setlocal autoindent smartindent cindent or reopen the buffer. Little tip: :set paste? shows whether paste is on. Personally, I use paste for quick fixes and :setlocal for longer edits — keeps things predictable and quiet during a frantic refactor.

Why Is Vim Auto-Indent Not Working After Vimrc Changes?

4 Answers2025-09-04 02:43:46
Man, that frustration is so real — I’ve been there. First thing I do is check whether vim even thinks it should indent: open the file and run :set filetype? and :verbose set autoindent. If filetype is empty or wrong, indent scripts won’t run. If :verbose shows autoindent being turned off by some script, that points to the culprit. Next, consider obvious toggles that silently kill indentation: if you’ve got 'set paste' enabled (or you toggled paste mode earlier with a mapping), indentation won’t behave. Also check whether you disabled 'autoindent', 'smartindent', or 'cindent' by mistake. Use :set paste? and :set autoindent? to inspect current state. If those look fine, source your vimrc manually (:source ~/.vimrc) and watch :messages for errors — a syntax error early in the file can stop the rest of the config from loading, so later indent settings never get applied. Also run vim -u NONE (or nvim -u NORC) to see if a vanilla session indents correctly; if it does, a plugin or a line in your vimrc is to blame. Useful commands: :scriptnames (shows loaded scripts), :verbose set shiftwidth? tabstop? expandtab? and checking ~/.vim/indent or plugin ftplugin files for overrides. If you want, paste the problematic snippet and I’ll poke at it with you.

What Are The Best Vim Auto-Indent Settings For JavaScript?

4 Answers2025-09-04 23:27:43
Okay, this is the hot take I give my friends when they ask how to stop JavaScript files from turning into a jagged mess: treat indentation as a filetype thing, not a global, and use 2 spaces plus an actual JS-aware indent engine. I usually put this in my vimrc (or better, in ftplugin/javascript.vim): filetype plugin indent on autocmd FileType javascript,typescript setlocal shiftwidth=2 softtabstop=2 tabstop=2 expandtab autocmd FileType javascript,typescript setlocal autoindent smartindent Those lines give you consistent 2-space soft tabs (the de facto style for many JS projects) and rely on Vim's smartindent for basic braces. But honestly, for real-world code with ES6/JSX/template literals, install a javascript-indent plugin (like the popular one that provides an indentexpr) and let it set indentexpr for you; it handles arrow functions, template literals and some weird edge cases better than plain smartindent. I also map = to re-indent visually: vmap = = or use gg=G to reformat a whole file. Finally, I pair this with an on-save formatter — 'prettier' is my go-to — so even when teammates differ, my local formatting is predictable. If you want the exact plugin names or a sample ftplugin that runs Prettier on save, I can paste that too.

How Can I Enable Vim Auto-Indent For Python Files?

4 Answers2025-09-04 03:25:38
Honestly, getting Python auto-indent working in vim is one of those tiny victories that makes editing a joy. My go-to is to enable vim's filetype detection and then set sensible Python indentation rules in my config. Add these lines to your ~/.vimrc or init.vim for Neovim: filetype plugin indent on set autoindent set expandtab set shiftwidth=4 set softtabstop=4 set tabstop=4 The first line turns on filetype-specific plugins and indent scripts (this loads vim's python indent file). The rest make tabs into spaces and use four spaces per indent, which is the common Python convention. If you want the setting to apply only to Python buffers, drop the global lines into ~/.vim/ftplugin/python.vim and use setlocal instead of set. If indentation still feels off, check the buffer's filetype with :set filetype? and inspect loaded scripts with :scriptnames. I sometimes install a plugin like 'vim-python-pep8-indent' or use external formatters like 'black' called via a formatter plugin to normalize whitespace. Try opening a .py and typing an indented block — it should behave. If not, tell me what output :set filetype? and :verbose set shiftwidth? give and we can debug further.

Which Plugins Improve Vim Auto-Indent Accuracy?

4 Answers2025-09-04 05:51:09
Okay, I’ll gush a bit: if you want auto-indent to actually behave instead of randomly guessing, start by combining a detector, a language-aware indenter, and a formatter. I like using vim-sleuth to sniff tabs vs spaces and shiftwidth automatically; it fixes half my headaches before I even open the file. After sleuth, for Neovim I plug in nvim-treesitter with its indent module turned on — it understands syntax much better than old regex-based indent scripts. Pair that with either null-ls or coc.nvim (or ale if you prefer linters/formatters) to run real formatters like prettier, clang-format, shfmt, or rustfmt on save. That lets the language tools correct structural indentation rather than vim guessing. Small extras that helped me: editorconfig-vim to respect project settings, indent-o-matic as a fallback detector in weird repos, and indent-blankline.nvim for visual guides so you can spot mistakes. Also don't forget filetype plugin indent on and sensible defaults (autoindent, smartindent/cindent where appropriate). With those layered, indentation accuracy improves dramatically and my diffs stop being a jungle of whitespace edits.

How Do Filetype Plugins Interact With Vim Auto-Indent?

4 Answers2025-09-04 22:00:34
If you've ever opened a file in Vim and wondered why indentation behaves one way in one project and differently in another, the way filetype plugins and indent scripts interact is the usual culprit. In my messy but beloved setup I keep separate snippets in ~/.vim/ftplugin/ and ~/.vim/indent/ and they each have a job: ftplugin files generally set buffer-local editing options (things like shiftwidth, tabstop, expandtab, mappings) while indent scripts (under indent/) provide indentation logic by setting 'indentexpr', 'cindent', 'indentkeys', or related buffer-local options. Because these are buffer-local, whichever script writes a particular option last wins for that buffer. Practically that means you can get conflicts. An ftplugin might set 'shiftwidth' to 4 for 'python' and an indent script might expect 2; or an indent script will set 'indentexpr' to a custom function that overrides simpler behaviors such as 'autoindent'. The usual fixes I use are: enable both with :filetype plugin indent on, then put overrides in after/ftplugin/ or after/indent/ so they load later; or explicitly set local options with setlocal in a ftplugin; or prevent an indent script with let b:did_indent = 1 if you deliberately want to skip it. For debugging, :scriptnames shows what got sourced, and :verbose setlocal shiftwidth? / :verbose setlocal indentexpr? tell you who last changed a setting. I like keeping ftplugin for styling and small mappings, and leaving indentation math to indent scripts — but I always keep an 'after' copy for those moments when I need the last word.

Can Vim Auto-Indent Be Configured Per Project Directory?

4 Answers2025-09-04 16:52:11
I'll be blunt: yes, you absolutely can set up Vim to auto-indent differently per project directory, and I've done it a bunch of times across projects with different coding styles. When I need a project-specific policy I usually pick one of three safe routes: use a repository-level '.editorconfig' with the EditorConfig Vim plugin (works across editors and is a huge life-saver), add per-project autocommands in my global vimrc that match the project path, or—if I must—use a controlled local vimrc mechanism (with security checks). For example, in your main vimrc you can add an autocmd that applies settings only when the buffer lives under a particular path: augroup proj_indent autocmd! autocmd BufRead,BufNewFile /path/to/myproj/* setlocal shiftwidth=4 tabstop=4 expandtab augroup END That keeps the rules scoped to files under that directory. I avoid blindly enabling 'exrc' because executing arbitrary project .vimrc files can be risky; instead I either require a checked-in '.editorconfig' or use a trusted plugin like 'localvimrc' that prompts you before sourcing. Also remember to use setlocal so other projects aren’t affected. For Neovim, the same autocmds work, but I often detect the project root via an LSP/root_pattern helper and then apply settings dynamically. Overall, choose EditorConfig if you want a cross-editor approach, or autocommands if you prefer staying purely in Vim land.
Explore and read good novels for free
Free access to a vast number of good novels on GoodNovel app. Download the books you like and read anywhere & anytime.
Read books for free on the app
SCAN CODE TO READ ON APP
DMCA.com Protection Status