When I use `gqap' command to reflow a paragraph in vim, vim seems to try to be smart and adds indentation automatically, e.g.
When a line ends with a ',':
We protect your rights with two steps: (1) copyright the software, and (2),
offer you this license which gives you legal permission to copy, distribute
and/or modify the software.
When a line has unmatched brackets:
Program or a portion of it, either verbatim or with modifications and/or
translated into another language. (Hereinafter, translation is included without
limitation in the term "modification".) Each licensee is addressed as
"you".
Is there a way to turn this off?
Thanks!
What filetype do you do this in? And what is the output of ':set'?
If you copy your texts to an empty file, it formats it the way you want it. I have smartindent and autoindent enabled, so you could try that. (set si, set ai)
I notice I have cindent set.. Unset that and instead set smartindent and autoindent and it works now. Thank you!
I experienced this problem too, but unlike the other answers suggest, it was occurring in a .txt file without cindent set. However, I noticed that smartindent was set, and disabling this fixed the problem.
I had the same problem. I looked at my settings like this:
:set
I didn't have cindent set, but I did have smartindent. So, based on the above:
:set nosmartindent
Then the unwanted indentation on the last line (just as described above) was fixed.
Thanks for the help!
Related
I'm having problems with auto indentation in vim while programming in lisp.
My .vimrc had the following settings for tabs:
set noexpandtab
set autoindent
set softtabstop=0
set shiftwidth=8
set tabstop=8
When I type (if <enter> in insert-mode, a new line is created with an indentation of two spaces.
None of my settings say anything about two spaces, so why don't I get a tab?
What setting can I use to change the indentation while in insert-mode?
Thanks in advance!
Update:
Thanks for the answers, the settings are not overwritten.
It has to do with the "default lisp indenting".
In the :help lisp it says something about changing the p flag in
cpoptions. This is what it says in the help for the cpoptions flags:
p - Vi compatible Lisp indenting. When not present, a slightly better algorithm is used.
Setting it does change the indent to one space instead of two spaces.
Still not sure how to change this to something else though.
Looks like this is two-space indentation is a hard coded behavior of :set lisp mode which ignores shiftwidth.
We can trace this to the source code which contains a hard-coded amount += 2; increment statement.
It's that way for a good reason; it has come to be the predominant way of writing Lisp.
As I wrote this answer, I peeked at samples of source code the following projects (all Lisp or Lisp-like languages):
Steel Bank Common Lisp (SBCL);
Clozure Common Lisp (CCL);
GNU Emacs;
Clojure;
GNU Guile;
Racket;
and GNU CLISP.
I did not spot a single example of anything but two-space indentation! With two-space indentation, you are in good/large company. You might as well get used to that; even if you somehow get Vim to follow your way, if you upstream anything into anyone's Lisp project, you will likely have to reformat.
Now, I have seen Lisp code using four-space indentation, like in very old code bases and some books.
By the way, sometimes you see if indented like this:
(if (condition)
(then)
(else))
This may happen where indentation is four spaces, but I'm referring situations when this is alignment, and not four space indentation. This formatting is, of course, standard for function arguments, but controversial for operators such as if. Some people like it that way in their code bases. For instance, this institution's randomly found coding style guide recommends this way of writing if, while also recommending two-space indentation.
Vim will do the above if you remove if from lispwords.
The :set lispwords=... parameter contains a comma-separated list of identifiers which follow operator-like indentation, meaning two spaces rather than function-like alignment: second and third lines align with argument. By default, if is found in lispwords.
Lisp mode also applies two space indentation to a function (i.e. form not listed in lispwords, if there is no argument):
(gobbledygook 1 2
2 3)
(gobbledygook
1)
That's also "canonical". Sometimes this comes in handy if you're trying to conform to some maximum line length; a function call running past the limit can sometimes be made to fit by moving all its arguments down, and just giving them two space indentation.
Each filetype can have its own settings in vim. So your .vimrc values can be overwritten in filetype plugins. To learn the current values of the settings for lisp filetype open the lisp file and run the following commands in vim command line:
:set noexpandtab?
:set autoindent?
:set softtabstop?
:set shiftwidth?
:set tabstop?
If they are different from the ones in .vimrc, then your values are overwritten in some plugin.
In order to overwrite them again with your custom values, create the ~/.vim/after/ftplugin/lisp.vim file with the required values:
set noexpandtab
set autoindent
set softtabstop=0
set shiftwidth=8
set tabstop=8
I'm trying to enable vim indent folding.
From what I can see online, setting :set fdm=indentshould just work. For me, it doesn't do anything. I can fold manually, but I'm quite strict about indenting code, so indent-folding is ideal. I use spaces to indent (two spaces per level)
My ~/.vimrc looks like this:
set foldmethod=indent
and is definitely being loaded (according to :scriptnames)
Is vim folding in some way dependent on the file type? I'm writing C CUDA, so the extension is .cu.
Do I have to install some sort of plugin for indent folding? I know it's a broad question, but this seems like basic functionality and I have no idea why it's not working.
set nofoldenable turns off folding, it should be set foldenable to enable it
Also set foldlevel=2 sets how many levels of the identified folds should be visible. If you want to specify the depth of indent to use for folding, the setting to use is shiftwidth (e.g., set shiftwidth=2)
Another register I found that needs to be looked at if the fold behavior is "ignoring" what is clearly indented: make sure foldminlines is set to 1 to generate all the folds you might expect.
This, in addition to the shiftwidth setting to something like 2, or even 1 to generate a new fold level with every single space difference.
- E
I love tabs, and prefer it to spaces in indentation.
But I'd like to transform tabs into 4-space groups when saving files. (Because the file may be opened and edited in other environments) And of course, those generated spaces should be converted back to tabs if I open the file again. (Assume that there are no 4 contiguous spaces in the original text)
Well in your .vimrc:
set noexpandtab
set tabstop=4
set shiftwidth=4
fun MyRetab()
set expandtab
retab
set noexpandtab
endfun
au FileWritePre *.YOURFILEEXTENSION call MyRetab()
But I don't know what you mean by "those spaces should be still recognized as tabs if I open the file again."
If you write a file spaces instead of tabs, well it can't be undone easily AFAIK. EDIT: see the super retab wiki page for undoing it!
NOTE if you have tab(s) in your source's string contents this will replace that as well!
In addition to Zsolt Botykai's answer, you could try using retab!, which attempts to replace spaces with tabs where appropriate. This seemed to work quite well when I just tried it, but I got a few erroneous tabs. I suppose it depends how good your assumption is that there no other sequences of 4 spaces other than expanded tabs.
HOWEVER... this all seems like a risky business. In my experience, when there are coding/encoding standards such as these, it is always easiest to adhere to them from the start. "Fixing-up" the file in this way is asking for trouble.
I think Vim does a good job of emulating tab-like behaviour while using only spaces. Have you tried using smarttab and expandtab?
Emacs, while editing a C file, forces me to follow a particular indentation. When I press tab on an incorrectly indented line, it corrects the indentation. I want this behaviour from my vim editor. Till now I have done the following :
set cindent
set smartindent
set autoindent
set expandtab
set tabstop=2
set shiftwidth=2
set cinkeys=0{,0},:,0#,!,!^F
in my .vimrc file. However, I am not achieving the same emacs-like forced effect as I want.
Is it possible at all in vim ?
'smartindent' is obsolete. There's really no reason you need to have that in your vimrc.
'cindent' overrules 'smartindent', so setting both in your vimrc is pointless. Setting 'cindent' in your vimrc isn't very useful either, since it only really works well on C-like languages.
filetype indent on will enable the filetype-specific indentation plugins (c.f., the indent directory under $VIMRUNTIME). That paired with 'autoindent' at least gives you basic automatic indentation support regardless of what filetype you're editing.
If you want to add indent settings for a specific filetype, you can create your own indent script in ~/.vim/indent/<filetype>.vim, or ~/.vim/after/indent/<filetype>.vim if you're augmenting an existing system-wide indent script.
As the settings you posted show, pressing Ctrlf in insert mode will do what Emacs does when you press Tab. This is described at :help indentkeys-format. 'cinkeys' is used when 'cindent' is enabled and 'indentexpr' is empty. 'indentkeys' is for the reverse. It's just a slight change to modify the setting so Tab can be used in place of/in addition to Ctrlf.
On a final note, I'd recommend learning your way around Vim's help. It's very thorough and easy to use once you figure things out. :help usr_toc is a good place to start for user-level documentation. :help describes some of the basic about navigating the help, how to search for topics, etc.
The == command is what you want, if I understand you correctly. It reindents the current line according to syntax rules.
As for binding it to tab, that's certainly possible, but I have not done that and am not completely sure as to how you can catch the right moment when it should actually insert the tab and when it should reindent.
Personally, I find it less confusing to just press ==. = accepts a range even, so you can go into visual mode, make a selection and tap = and the region will be reindented.
In TeX vim usually screws up my indentation. Mainly when I'm in a displayed equation which I think should look like this:
\[
x=\frac{y}{z}
\]
where the whitespace infront of the x is one tab.
When I start type the equation I type the \[ and \] marks first and then go back between them, typing the tab and then the rest of the equation.
Vim doesn't do anything wrong until I have to use something that incorporates curly braces (\frac{} for example). When I type the closing } vim automatically shifts the indentation for the whole line to the left, which undoes my typed tab.
This is very anoying, how do I disable it?
my .vimrc contains:
"indentation
set smartindent
set autoindent
set tabstop=5
set shiftwidth=5
filetype indent on
I just spent a few hours working through indentation pains with javascript, and the conclusion I came to is don't remove filetype indent on from your vimrc!
This setting provides the best smart indentation for multiple file types. If you're getting bad results with this, there's likely a configuration issue at hand.
File Specific Indent Settings
So if you're like me, you probably had filetype indent on in your vimrc and had no idea what it was doing.
All this setting does is tell vim to look for files with filetype-specific indent rules. There are a few places it looks, but there are probably only two that you'd be interested in.
$VIMRUNTIME/indent/
~/.vimrc/after/indent/
The first place holds the default indent rules that come with vim. If you were to set filetype indent on on a fresh vim installation, this is where all the smart indenting would come from. For example, when you open a file called index.html in would get the rules from $VIMRUNTIME/indent/html.vim.
In my experience, these default rules are pretty darn good, but they can get messed up by other settings.
The second place (the after directory) allows you to add settings that will supercede those in the first place. This is nice because you don't have to edit the default files in order to customize them.
Flavors of Indentation
There are a few different indentation options as you've seen, and they don't all play nice together. From the Vim wiki:
autoindent
'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering. 'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.
I use filetype indent on and set autoindent in my vimrc, since they work well together. I don't have the others set.
smartindent & cindent
'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files. 'cindent' is more customizable, but also more strict when it comes to syntax.
'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.
When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.
Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works.
indentexpr
Runs filetype indent scripts found in (vimfolder)\indent\\(indentscripts). It is mentioned in the vim documentation for filetype, alongside the others just mentioned (also, it was the cause of the problem I was having):
Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.
Troubleshooting
There's a chance that some rogue plugin is changing your indent settings and that's why you're getting poor results. Luckily verbose will tell you which file was the last to change the option in question.
:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?
You may get a result such as
indentexpr=SomeMessedUpValue
Last set from ~/.vim/bundle/some_plugin/indent/plaintex.vim
If that happens, you can move that file, close and open vim, and see if it fixes your problem.
Turning Off Indent Settings for TeX
Maybe the defaults just aren't doing it for you, and you want to disable the indent settings for TeX, but leave all other file types alone. You can easily do so by setting these values to their defaults in a file in the after directory.
I don't know much about Tex or LaTex, but when I created a file with the .tex extension and ran :filetype it had the filetype as plaintex. Assuming that this is correct, you'd want to create a file, ~/.vim/after/indent/plaintex.vim. In that file:
set autoindent&
set cindent&
set smartindent&
set indentexpr&
This will set all these values to their defaults whenever you open a .tex file.
There seem to be a little mix of terms in your question. In vim the term autoindent points to a special kind of indentation that simply follows the indent level of the previous line (which is quite handy sometimes). To remove it set noautoindent by hand, or write it in your _vimrc.
There are two other automatic kinds of indentation, cindent and smartindent. Similarly, if you wish to disable them go with set nocindent and set nosmartindent
If you look in help (help autoindent, ...) they are all quite nicely explained. Which one you prefer (or don't) is mostly determined by your programming style and habits. So, try them out and see which you like most.
Unfortunatelly, I don't use LaTeX that much anymore, so I'm not familiar with its internal filetype indentation rules.
For anyone else having a similar problem, a solution that worked for me was:
Use :verbose set indentexpr? to find what file was causing the de-indentation
Find where indentexpr is changed (for me it was setlocal indentexpr=GetTeXIndent())
Change that line to setlocal indentexpr& to turn indentexpr off
This removed all de-indenting from brackets, parentheses, and braces.
Remove the lines set autoindent and set smartindent to remove all vim autoindentation.
The following command finally stopped VIM from pretending it knows how to indent files for me and only do what I explicitly tell it:
:setl noai nocin nosi inde=
Courtesy https://vim.fandom.com/wiki/How_to_stop_auto_indenting
If you are using the vim-latex plugin, set this option:
let g:tex_indent_brace=0
For other plugins, if you don't want to turn off indentexpr as in the above answers, you can find where indentkeys is set and comment out those lines. This should stop triggering re-indent when you type a closing brace.
:verbose set indentkeys?