I've noticed that plugins and other things will often modify formatoptions. is the following sufficient to keep them set/static?
" Do not auto-wrap comments and don't insert comments when pressing o/O. See: :h *fo-table* and :h *'formatoptions'*
set formatoptions-=cro
autocmd Filetype * set formatoptions-=cro
Or, is this missing anything? (Is the first line unnecessary as well?)
Yes, what you have is (almost) good. You need to wrap it in proper augroup
augroup FORMATOPTIONS
autocmd!
autocmd filetype * set fo-=c fo-=r fo-=o " Disable continuation of comments to the next line
augroup END
Also notice as I divided -cro into singular option removing, otherwise you won't have guarantee you get the desired effect.
Related
Vim now has built-in Markdown folding, yay!
I want to fold on Markdown headers, and within headers, fold on indents also.
How can I have both Markdown header folding + indent fold method at the same time?
There can only be one of Vim's standard fold methods used within an individual window, but what you ask should still be possible. Here are a few options that I can think of:
You can use the expr method option and create your own custom rules with that. This is probably the only "real" way to get what you want, but it's the most complicated option.
You could also use two windows in a tab, each pointing at the same buffer, and set different fold methods locally for each split.
Finally, you could always hack something together with autocommands. From the Vim wiki page on folding, about having both indent and manual folding:
augroup vimrc
au BufReadPre * setlocal foldmethod=indent
au BufWinEnter * if &fdm == 'indent' | setlocal foldmethod=manual | endif
augroup END
This hack takes advantage of modelines, and Vim's behavior for setting variables before vs. after the modeline is read. Note that you have to have Vim's nocompatible option set for this sort of trick to work.
I have the following text:
");
When I hit return in insert mode, Vim creates a new line (as expected), but also indents the line on which return was hit:
");
Vim only does this with some lines, presumably when it thinks the indentation is incorrect.
How do I configure Vim to not indent the current line when hitting return? I want Vim to not touch the line at all. My current settings are as follows:
set tabstop=4
set shiftwidth=4
set expandtab
set softtabstop=4
Filetype plugins may set 'cinkeys' or 'indentkeys' so
that automatic indenting is triggered when certain keys are
pressed. To turn this off, while leaving 'indentexpr' intact
so that you can still indent with ==, you can add the
following autocommand to your vimrc:
" Allow filetype detection, plugins and indent files ...
filetype plugin indent on
" ... but keep certain preferred defaults.
augroup overrideftplugins
au!
au FileType * set cinkeys= indentkeys=
augroup END
Note that the autocommand needs to come after the line that turns on filetype detection etc. This is because they are triggered in the order that they were
registered, and we want the autocommands for the overrides to fire after the autocommands that are set up for the ftplugins and indent scripts.
I was able to reproduce your issue with an XML document where one tag was not aligned with the others. I tried disabling autoindent, cindent and smartindent, but what finally fixed it for me was clearing the indentexpr.
:setlocal indentexpr=
Alternatively, you can remove return from the list of indentkeys, or clear them altogether.
:setlocal indentkeys=
I have created a function which can satisfy your need.
function! Enter()
let pos=getpos('.')
let substr = strpart(getline(pos[1], pos[2])
if strlen(substr)==0
exe("normal! o")
exe("normal x")
startinsert
return 1
else
exe("normal! li\<Enter>")
startinsert
return 0
endif
endfunction
When the current cursor is at the end, and you press enter, it will create a new line and move to first column. (As usual, any lines below will be moved down)
When the cursor is at the middle of the line, and you press Enter, it will split the line.
To use the function, you can map like
:imap <Enter> :call Enter()<CR>
This works well for C comments, subroutines/functions.
I am trying to call this autoformatting plugin on save
Here is my autocommand:
autocmd BufWrite *.css,*.html,*.js,*.py :Autoformat<CR>
When I save nothing happens, if I manually call :Autoformat then the autoformatter runs.
What am I doing wrong?
You've already found the solution - here's the explanation:
The <CR> is for mappings, which work like a recorded sequence of typed keys, so you need to start command-line mode with : and conclude with <CR>. An autocmd takes an Ex command, so the <CR> is taken as an (invalid) argument. You also don't need the :, but that doesn't do harm.
As :help BufWrite shows, this is a synonym for BufWritePre.
BufWrite or BufWritePre Before writing the whole buffer to a file.
So, this is the recommended form:
autocmd BufWritePre *.css,*.html,*.js,*.py Autoformat
From what I've experienced, you sometimes need to surround it in an augroup
augroup autoFormat
autocmd BufWrite *.css,*.html,*.js,*.py :Autoformat<CR>
augroup END
I don't know why but it works for me! Technically just the autocmd should work on its own but sometimes it doesn't. Also, on the GitHub page it says to use :Autoformat<CR><CR>, maybe try that.
For some reason i had to get rid of the carriage return and change to BufWritePre (although the CR is the main issue, the BufWritePre just makes sure it gets changed before the buffer is written instead of after so it gets saved):
autocmd BufWritePre *.css,*.html,*.js,*.py :Autoformat
Why, I don't know?
Whenever I want to open a new line in Vim with o, it automatically indents (instead of starting at the beginning of the line). Why is that? How can I fix this?
(I don't want to switch off auto-indent, which is great for other file types.)
UPDATE:
It seems to have something to do with the actual text: auto-indenting (=) the following two lines indents the second line (why? -- I would like both lines to start in column 1!)
*in golf: failing to make par is a loss, missing a birdie putt is a foregone gain, not a loss
*negotiations, especially renegotiations: concessions you make cause you much more pain
UPDATE 2 (my .vimrc):
:set cpoptions+=$
:set virtualedit=all
:filetype plugin indent on
:set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
:set fdm=expr
:set gfn=Ubuntu\ Mono\ 11
setlocal autoindent
setlocal cindent
setlocal cinwords=if,else,elseif,do,while,foreach,for,case,default,function,class,interface,abstract,private,public,protected,final
setlocal cinkeys=0{,0},0),!^F,o,O,e
setlocal nosmartindent " don't use smart indent option
You can set options to take effect only for specific files. For example, I have the following in my vimrc:
if has("autocmd")
augroup LISP
au!
au BufReadPost *.cl :set autoindent
augroup END
augroup C
au!
autocmd BufNewFile,BufRead *.cpp set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4
augroup END
endif
Using this, I can turn on autoindent, or file formatting, or whatever for the files where it makes sense, but not have it on generally, when it might annoy me in other cases. In this case, I turn on autoindent for .cl files, but not necessarily for others.
You could also, in theory, use the same thing to turn off autoindent for .txt files.
A while ago, I had to put
filetype plugin on
in my .vimrc for a plugin I use.
But this caused a change in autoindent: Whenever I write a comment "//", and then press enter, vim autoindentation automatically enters another "//" in the next line.
// This is a comment. <ENTER>
// <-- vim automatically puts '// ' there
What can I do to avoid this?
I use the autoindent setting in my vim file.
I already tried
filetype plugin indent off
but it does not work.
I am answering your title rather than the body of your question, since your title brings people to this page who are looking to stop Vim from indenting comments.
The variable that controls whether Vim auto-indents a new character is indentkeys. I've noticed incorrect indentation only in Python and Yaml, so I've turned off auto-indentation only for the "#" character at the beginning of the line: :set indentkeys-=0#
Since loading the filetype indentation plugin will override any .vimrc settings you've made, you can set up an autocmd to change the indentkeys after a file is created or loaded. Here are mine:
autocmd BufNewFile,BufReadPost * if &filetype == "python" | set indentkeys-=0# | endif
autocmd BufNewFile,BufReadPost * if &filetype == "yaml" | set expandtab shiftwidth=2 indentkeys-=0# | endif
See :h indentkeys
Note that because of (possibly) a bug, if you use Neovim you must also specify filetype plugin indent on, or the filetype won't be set.
Take a look at :h formatoptions and :h fo-table. The options you need to turn off are r and o. Turning them off prevents vim from automatically inserting the comment leader (in this case "//") when you press enter in insert mode or when you press o or O in normal mode.
See :help 'formatoptions' - I know how annoying this is!
Try this:
:set fo-=or