auto-formating tool for django html template files - linux

Is there a *nix tool (or vim plug-in) that would allow me to automatically format django html template files?
Hitting the space bar hundreds of time does not sound like a good use of my time but then neither does leaving some weird formatting that is hard to read.

Install vim-jinja2-syntax plugin by putting Glench/Vim-Jinja2-Syntax when installing it with plugin manager (vim-plug, Vundle etc.)
Put autocmd FileType html setlocal expandtab shiftwidth=2 tabstop=2 in the vim config file, which is usually at ~/.vimrc.
run gg=G in django html file(aka jinja template)

vim has a filter for fixing indentation (I think that's what you mean with "auto-formatting"): =. If you wan to indent the whole django html template file, just type gg=G and that will do the job. gg tells vim to go the the first line, = to indent and finally G to got to the end of the file.
You can read more about this on vim docs.

Related

set nofoldenable not working in my vim - why?

Every time I open one of my markdown (.md extension) files in my MacVim, they automatically fold lots of lines, which is a pathetically frustrating feature in almost all cases. And even after I added set nofoldenable in my ~/.vimrc, it doesn't disable the feature at all. Writing in ~/.gvimrc doesn't work, either.
So is it feasible to disable it? And what am I missing?
I also wonder why the depressing folding feature only functions in my markdown files. As far as I know, all the other extension I use don't conform to the functionality.
I use OS-X default Vim 7.3 and Mavericks 10.9.1.
Have you tried with
au FileType markdown setlocal nofoldenable

Trying to set .html files with htmljinja filetype and get correct indentation

I have just this configuration below. When I uncomment the last line, I dont get the correct indentation: the lines are put in the first column when I indent.
syntax on
autocmd FileType html setlocal shiftwidth=2 tabstop=2
"au BufRead,BufNewFile *.html set filetype=htmljinja
I need the htmljinja filetype because my html has twig code (twig's syntax is like htmljinja)
I have vim 7.3.547
In the meantime i circumvent vim's indentation capabilities and rather rely on specific indenters/reformatter wherever i can (by the way if anybody is aware of a tex/latex reformatter please let me know).
As a result the following lines found their way into my .vimrc:
au FileType c,cpp let &l:equalprg="indent -br -l62 -nce -blf -bbb -i4 -nbfde -nbfda -bad -bap -cli4 -nut"
au FileType perl let &l:equalprg="perltidy"
noremap <leader>i mzgg=G`z
A drawback of this approach is that you can't enjoy automatic indentation while writing. Nevertheless I get used to it very fast and I think of it as a very solid and suitable solution to the indentation-problem. Moreover the indented code looks great and i doubt that those fancy IDE's keep up with it.
Now, what has this got to do with your problem?
Following my approach i searched for an html-reformatter and found tidy. tidy seems to be a monster, that not only can pretty-print your html files but also can validate and correct them.
In order to cope with tidy you have to write a config file for your indentation purposes. The following one works for me:
~/.tidyrc_indent:
indent: auto
indent-spaces: 3
show-warnings: no
show-errors: 0
quiet: yes
indent-cdata: yes
output-html: yes
wrap: 80
But make sure to read the man pages of tidy because the options seems to be endless.
Now you can add
au FileType htmljinja let &equalprg="tidy -config ~/.tidyrc"
to your .vimrc and start indenting your html file with gg=G or a derived mapping that needs less keystrokes.
Please take care as tidy like any other reformatter may mess up your code. Therefore you should back up your code regularly (maybe with git or something similar). Although i never experienced this by myself wiht perltidy and indent there are some reports on the internet of angry users that complain about such events.

How to enable go.vim by default (automatically)?

The instructions on the Vim site says to just put the file in the /syntax folder. This works all right and well. But, for me to use the syntax I must set the following
:set syntax=go
Every single time. So, I know I am doing something wrong. I just don't know what.
Here are some things from looking around,
My HTML5 syntax set is from Rodrigo's HTML5 omnicomplete function and syntax vimball file. Though this uses some installation script to get it going.
As far as I can tell this would be my first manual adding of syntax file.
Also, my VIMRUNTIME is not set, well because there is no syntax.vim file, so from reading the documentation I see it checks for files via synload.vim
I even read the "Making Your Own Syntax Files" section, which says that same as above with the syntax=go option. Am I supposed to be detecting the .go filetype as described in new filetype section?
How can I enable syntax highlighting for GO by default?
This is for Mac Snow Leopard.
I don't think it is this complicated but I decided to leave all the different documentation I skimmed. GO and Vim say to just add the file. But it is definitely not detecting it automatically
If you are using filetype detection in your ~/.vimrc file with the following line:
filetype plugin indent on
then you can place the file in the following folder:
~/.vim/after/ftplugin/go.vim
or for windows
~/vimfiles/...
For the filetype detection to work, would would want the autocmd in a file in the ftdetect folder:
~/.vim/ftdetect/go.vim
and the contents would be:
autocmd BufNewFile,BufReadPost *.go set filetype=go
Use autocmd:
au BufRead,BufNewFile *.go setlocal filetype=go

VIM: set filetype=txt for every new file [No Name]

I tried all possible things to let vim set filetype to 'txt' to all new files I create (in a new tab) but it doesn't work.
This is p.e. what I've read on the web a few times:
au BufRead,BufNewFile *.txt setlocal ft=txt
(to put in _vimrc)
However it doesn't work.
Can anyone help me?
The following line, added to your .vimrc, will set the filetype to text if it is not already set.
autocmd BufEnter * if &filetype == "" | setlocal ft=text | endif
All files are considered plain text unless you have file-type detection turned on or explicitly set the file-type. However, Vim lets you set the file-type to any old text, so are you absolutely sure it is not working?
:set filetype=banana
:set filetype?
filetype=banana
Setting the filetype is not going to have any noticable effect unless there is a corresponding file in the ftplugin Vim directory and Vim does not ship with a txt.vim file-type file. You could, of couse, add a txt.vim here but I am not sure what this will gain you over default settings — what special behaviour would you want for text files that you would not want for the default behaviour?
(If you want syntax highlighting (whatever that may mean for text file!) then you will also have to create a txt.vim file in the syntax Vim directory.)
What effect are you trying to achieve?
It's actually way simpler than all this. Just put this as one of the first lines in your .vimrc.
set ft=txt
Whenever opening a new file, the filetype will be set to txt. But if you open a file with an known existing type it will still be overridden no problem.

How can i indent my html file with vim correctly?

I set up the indentation in my vimrc in the following way:
set tabstop=2
set shiftwidth=2
set autoindent
set indentexpr
set expandtab
I created the indent folder in my ~/.vim/ directory and pasted an html indentation-plugin into ~/.vim/indent/ . I'm using the following one:
http://www.vim.org/scripts/script.php?script_id=2075
Whenever i try to "gg=G" in normal-mode it deletes all the indentation instead of indenting the file in a proper way. JavaScript files for example work nicely with another indentation plugin i have found, but nomatter which html plugin i have used so far, it didn't really work in a useful way.
Is there anything i can do here?
///////////////
Update:
i just tried to change the filetype to xml and it began to work. Now i'm asking myself how i can get this to work with html files. Hope someone can help me out.
Heading
It works out of the box for me. Try if it works with the minimal setup:
$ vim -u NONE
:set nocp
:filetype indent on
:set ft=html
and type in some HTML code.
The second tag should be indented by a tab now.

Resources