I use tabs for indentation in all my files. However, some bigger projects still use spaces, so I also need to follow those guidelines when I contribute.
This issue could easily be solved if the editor would detect the indent character used in the edited file, and use that one when e.g. autoindenting.
Sadly, vim forces spaces for indent, even when I am editing a file which otherwise only uses tabs.
How can I tell vim to always just use what the file is using (and use tabs if the file isn't using anything)?
I tried adding the following line (as I understand it, this should be forcing tabs - not exactly what I want, but better than inserting spaces everywhere) to ~/.vimrc and reopening the file I was editing, but this failed to take effect.
I am running vim-8.0.0386.
It looks like tpope/sleuth plugin is exactly what you want.
Otherwise, DetectIndent works too.
Clone the DetectIndent repo in your .vim/bundle/ directory (I use pathogen to install plugins), then open any file and type: :DetectIndent
Now shiftwidth, expandtab and tabstop should be adjusted to your file.
Here's another plugin recommendation: My IndentConsistencyCop plugin. It does not only detect the used indent setting, but also warns you if inconsistent settings are used within the same file.
Related
I have enabled vim syntax on (in ~/.vimrc syntax on) and it works but only on files with a code in when I view them. When I create a new file with vim and write there some code - no syntax highlight. After saving this file and reopening with vim - syntax highlight works perfect.
I am using manjaro KDE.
When you open a new file without an extension (vim mynewfile) none of vim’s filetype detection mechanisms can recognize it (they all use either extensions or first-couple-of-lines heuristics, which don’t work here).
When you enter code and reopen the file, the line-checks for filetypes work, causing the syntax to be set correctly, causing highlights to apply.
You can always set syntax=mine (though set filetype=mine is better) to set it manually.
This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.
Vim must know how to highlight your syntax in order to actually highlight it. One way to do this, is for Vim to check the file name and sometimes inspect the contents of the file, and set the file type. The file type is then used to highlight the syntax.
To enable detection of the file type (and load plugin and indent files), add the following to your vimrc:
filetype on plugin indent
If Vim is unable to detect the file type, and you have not yet saved your file with a known extension, you can set the file type manually, like this:
:set filetype=html
Vim will then highlight the syntax of the file as HTML syntax.
More information is available in the help pages.
I uninstalled vim74 and compiled vim81 and installed it. However I found it strange, comparing to vim74. When there's no .vimrc file under HOME dir, I open a c++ file and syntax highlight is working and I can use Backspace to delete letters. However when I add a .vimrc under HOME dir and just put set number into it, when the c++ source file is opened, no highlight, and Backspace not working. Why is that ? I used to add some configurations in .vimrc under vim74 before, and this situation never happens.
After complaints that Vim in its default configuration is hard to use (especially for beginners), it was decided to enable a default configuration if the user hasn't created his own ~/.vimrc (yet). This was introduced with Vim 8.0, and explains what you're seeing (namely: syntax highlighting and sensible backspace behavior). Read more about the details at :help defaults.vim.
The help also has instructions how to keep the defaults when adding your own ~/.vimrc configuration:
If you create your own .vimrc, it is recommended to add these lines somewhere
near the top:
unlet! skip_defaults_vim
source $VIMRUNTIME/defaults.vim
Then Vim works like before you had a .vimrc.
Tip: Don't go all crazy with adding various snippets (especially not those you don't fully understand) and plugins to your ~/.vimrc, even though the Internet is full of them. Rather, build it up gradually, depending on needs, and back up your understanding with careful studying of the excellent :help. Also, avoid pre-packaged Vim distributions; they're even worse.
Is there a vim command or plugin that will span the source file I'm opening, and adjust my tab setting to whatever is used in the file.
I currently work on a diverse codebase at work, with some source files using hard tabs, other 4 space soft tabs, and other's 2 space soft tabs. My hard tab, if I forgot to changes, can lead to ugly whitespace in different editors if I forget to change it when I'm editting a soft tab file.
I'd like to not have to remember to check whatever's convention is used in a file every time I open a buffer, and adjust my preferences accordingly.
I use yaifa with great success. It's pretty much an "install-and-forget" plugin that does what it says it does without ever getting in the way. Working in the messy environment I work in was horrible until I found that gem.
If most people at work use Vim (and/or Emacs), you can encode the indent settings within the file itself, see :help modeline (at least Emacs has something similar). Of course, agreeing on a common setting and gradually migrating to it would be even better :-)
In case you can't do that or things are just too chaotic, you need a plugin to dynamically adapt the indent settings. My IndentConsistencyCop plugin checks whether the buffer's indentation is consistent and conforms to tab settings, and then offers to adapt the settings to the detected ones. (The plugin page has links to alternative plugins.)
I've used the following function (found it somewhere on the web) with good success. Just put it into your .vimrc:
function Kees_settabs()
if len(filter(getbufline(winbufnr(0), 1, "$"), 'v:val =~ "^\\t"')) > len(filter(getbufline(winbufnr(0), 1, "$"), 'v:val =~ "^ "'))
set noet
else
set et
endif
endfunction
autocmd BufReadPost * call Kees_settabs()
Take a look at sleuth by the prolific Tim Pope:
This plugin automatically adjusts 'shiftwidth' and 'expandtab' heuristically based on the current file, or, in the case the current file is new, blank, or otherwise insufficient, by looking at other files of the same type in the current and parent directories. In lieu of adjusting 'softtabstop', 'smarttab' is enabled.
I wrote this because I wanted something fully automatic. My goal is that by installing this plugin, you can remove all indenting related configuration from your vimrc.
If your file is consistently indented with hard tabs, 'shiftwidth' will be set to your 'tabstop'. Otherwise, a 'tabstop' of 8 is enforced.
Earlier, when I used open .py files in vim on ubuntu, they would be well formatted, with separate colours for separate segments of the program. Now, when I am using VIM on ubuntu, all the text in the .py file appears black. How can I correct this?
Formating in Vim means text formatting; e.g. indenting lists and breaking long lines. You're concerned about syntax highlighting, which is purely about the visual appearance of code.
First, it needs to be turned on.
:syntax on
does that.
Second, you probably want Vim to automatically detect the used language (e.g. Python) and choose the correct syntax plugin for you.
:filetype on
does that, though you usually enable more via :filetype plugin indent on.
To make these settings persistent, put them into your ~/.vimrc configuration.
Check man vim. In a nutshell, find a copy of a vimrc file, one might be under /usr/share/vim/ subtree. It may be named vimrc_example.vim. Copy to your home directory and rename it as .vimrc.
I'm working on a large codebase, where each file has different indentation conventions: tabs, 4 spaces, 3 spaces, etc.
I currently default on tabs and do set shiftwidth=N expandtab smarttab when I come across a spaces-indented file, but that's annoying.
Is there any functionality in Vim, or a plugin, which can recognize that, for instance, the current buffer uses an indentation with three spaces per shift?
Various plugins exist that attempt to handle that situation. Here are a few I found by search for detect indent at vim.org
sleuth
DetectIndent
yafia
IndentConsistencyCop
IndentFinder
Good question,I'm facing the same problem too, and recently I wrote such a vim plugin for myself:https://github.com/luochen1990/indent-detector.vim
it has the following features:
detect mixed indent and echo warnning on bufEnter and bufWrite
automatically.
switch setting about indenting to fit the current indenting style automatically.
detecting time is limited, so you don't
need to worry about opening huge files.
I think it is well designed, and need to be known, post an issue on github if you have any suggestion :)