TextMate-style code beautifucation in vim? - vim

I have a bunch of ugly code in vim: it's not indented consistently at all. TextMate has this wonderful "code cleanup" function... and I'm sure vim is just as powerful, I just don't know how to automatically clean up my entire file (putting consistent tabs, with consistent length, after curly braces...and then unindenting after every code block is the main thing I want).

Have you tried gg=G in normal mode?

In addition to Gebb's answer, make sure the following is in your .vimrc:
filetype indent plugin on
Also, to strip out any trailing whitespace automatically, add this:
autocmd BufWritePre * :%s/\s\+$//e

In Vim, indentation is defined by filetype-specific scripts. The default JavaScript indent file is not very up to date and doesn't play very well with "modern" ways of writing JS (lots of nested objects, anonymous functions…). Try other indent files and see if it solves your issue.

Related

vim execute command depending on file type

How can I get vim to execute a command depending on the file type?
Recently I have been editing a lot of Python files and comments autoindent to the 0th column.
One way to fix this is to run the command inoremap # X#<left><backspace><right>
How can I get this command to run every time I open a python (*.py) file for editing?
TL;DR
Use vim's filetype plugin mechanism.
File-type specific code
There are (at least) two solutions to this, with one preferable to the other.
autocmd FileType
This has already been posted as an answer, but the general form is
augroup your_group
autocmd!
autocmd FileType python your good code
augroup END
This works, and has the advantage of grouping code in your vimrc.
But there is a second, more powerful, probably more performant alternative. It
is my preferred
solution.
after/ftplugin
I've written
extensively about using filetype
plugins in vim.
The gist of it is that you need filetype detection and plugins (filetype plugin
on at a minimum, though you may want filetype plugin indent on). Then you
drop filetype specific code in ~/.vim/after/ftplugin/<filetype>.vim (or a few
variants, see :h add-filetype-plugin, :h ftplugin-name, :h ftplugin).
So ultimately,
" ~/.vim/after/ftplugin/python.vim
inoremap # X#<left><backspace><right>
A few notes
It is a good practice to set b:undo_ftplugin properly: it helps (trust me).
You can borrow my undo script on
Github.
Consider using <buffer>, -buffer, setlocal, and <LocalLeader> where
possible. This is for local stuff, after all.
On power
A script gives you more flexibility than a single line of autocommands. You have
to jump through more hoops to do anything complex in autocommands, where in a
script they can just sit. Besides, you feel more like you're writing a program
(which you are) than a one off config.
The modularity is also perfect for organization: all your python customization
code is in one place.
And it's a lot less typing :)
On performance
I have not measured anything. Don't shoot me.
Having lots of autocommands can slow down vim, because they have to be checked
when their events fire. Rather than clog up the autocommands with filetype
stuff, use a file that is already going to be sourced for you (an ftplugin).
By piggybacking on an existing customization mechanic, you get all the benefit
and none of the risk. Again, it is almost certainly more efficient to let vim
source a file on demand for you then to try to reimplement it's ftplugin wheel.
Does this solve your issue?
au FileType python inoremap # X#<left><backspace><right>
This autocommand runs your mapping for filetype python

How to put PreserveNoEOL's command to .vimrc

I've installed PreserveNoEOL and I can use :SetNoEOL to remove EOL from current file.
But I do not want type :SetNoEOL every time I edit a file, I want to put it to .vimrc
How can I do that?
So, you always want Vim to leave off the final EOL character, creating what many people consider broken text files?! The plugin (I'm the author) wasn't meant for that, and I wouldn't recommend this unless you have good reasons, but it can surely be done via an autocmd in your ~/.vimrc:
autocmd BufNewFile,BufRead * SetNoEOL

Turn on Vim plugins for some files, not others?

I recently added filetype plugin indent on to my .vimrc in order to enable special indenting and syntax highlighting for Clojure code (*.clj files). However, it's also causing indenting in my LaTeX files (*.tex). This is annoying when I'm editing, and even more annoying, because the tab characters that get inserted confuse a custom program I use to process my LaTeX files. I know I can make indenting use spaces, but I really just want "intelligent" LaTeX indenting to go away. Actually, I want all intelligent indenting to go away, except where I specifically ask for it.
How can I get correct auto-formatting for Clojure code in Vim, but turn off all special handling of LaTeX files (except for syntax highlighting)?
Sorry if this has already been answered; I haven't succeeded in finding the answer yet.
(Irrelevant editorial comment: Sometimes Vim "upgrades" make me want to go back to Unix 'vi'. OK, not really.)
Each proper filetype plugin script has an inclusion guard at its beginning. If you don't want any of the filetype options for Latex files (i.e. filetype of tex), create a file ~/.vim/ftplugin/tex.vim with these contents:
:let b:did_ftplugin = 1
This causes the default ftplugin from $VIMRUNTIME to abort its execution. The same applies to indent: ~/.vim/indent/tex.vim and b:did_indent is the guard variable.
Alternative
On the other hand, if you just want to undo certain options (e.g. :setlocal expandtab to avoid inserting tabs), you'd put those overriding commands into the so-called after directory: ~/.vim/after/ftplugin/tex.vim.

How can I disable code folding in vim with vim-latex?

I have tried the usual approaches, and have read :help tex.vim
(see : http://vimdoc.sourceforge.net/htmldoc/syntax.html )
I've taken a brief look at syntax/tex.vim, but can't see how to disable it without rebuilding vim without folding. I'm sick of hitting 'zE'.
Lines I've tried in my .vimrc:
set foldlevel=manual
set foldlevelstart=99
let g:tex_fold_enabled=0
Just noticed that there are variables to control folding in vim-latex-suite, at least as of v1.6 of the plugin. The functionality is documented here:
http://vim-latex.sourceforge.net/documentation/latex-suite.html#latex-folding
In short you should be able to change three global variables to get rid of all folding:
:let Tex_FoldedSections=""
:let Tex_FoldedEnvironments=""
:let Tex_FoldedMisc=""
That should get rid of all folding. If you want to disable some folding but not all then you can control things by setting the appropriate values for each variable, as described in the documentation link above. Hope that helps.
What about
autocmd Filetype tex setlocal nofoldenable
The folding functionality all seems to located in folding.vim file of latex-suite distribution. This file is referenced in line 825 of my main.vim file in the latex-suite folder of the ftplugin folder. That line reads:
exe 'source '.fnameescape(s:path.'/folding.vim')
Comment out that line and, as far as I can tell, it strips out all the folding in latex-suite plugin. I don't think it affects anything else, but I haven't checked.

How can you control folds by an external file in Vim?

I have been searching a solution which puts the fold marks and codes to an external hidden file.
This way you could have permanent folds without extra fold signs.
How can you control folds by an external file in Vim?
That's a very general question. What is "an external file" in vim, really ? Vim is, after all, a collection of "external" files. So if I define my folding preferences in my vimfiles, is that "an external file" solution ?
You could link, for example, define regex mechanisms of folding and source them from an external file.
But I think, what you ment is, "can I define an external file, so I can have by project custom folding, so that everyone who uses vim after I give them my files, will have the same folding" ? Yes, I guess you could do that by extrapolating from the method above.
But remember, vim has several methods of folding:
manual - where you manually define folds (this is nice, but it leaves your code with lots of curly brackets, but it is "portable")
indenting - where indending defines folds
expression (which I mentioned)
syntax - defined by syntax highlighting
marker - you already know that
... in the end, it will all come down to those few settings in your vimrc.
My advice: DO NOT CHANGE vim's indenting, syntax, etc. files. Normally, you will not even need to change those (unless you're using a language vim has no support for, which I doubt). So, define your settings and preferences in your vimrc and vimfiles directory, and just give your coleagues preferences which they need (usually just those from vimrc) to have the same folding behaviour.
Vim can be made to atuomatically remember folds in a file by putting these two lines in ~/.vimrc
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
Use manual method of making folds. This is not the same as marker as implied above. Use :mkview to save folds and :loadview to reload them.

Resources