Why is vim not indenting two spaces for html? - html

So I am trying to set up my vimrc for python and web development. This is what my vimrc looks like.
"--------------vundle------------------------
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
"add plugins here
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-surround'
Plugin 'itchyny/lightline.vim'
call vundle#end()
filetype plugin indent on
"------------------end of vundle-------------------
"--------------python development-----------------
"PEP8 python indentation and formatting
au BufNewFile,BufREad *.py
\ set tabstop=4
\ set softtabstop=4
\ set shiftwidth=4
\ set textwidth=79
\ set expandtab
\ set autoindent
\set fileformat=unix
let python_highlight_all=1
syntax on
"---------------web development------------------
"basic tab spacing for html, css and js
au BufNewFile,BufRead *.js, *.html, *.css
\ set tabstop=2
\set softtabstop=2
\set shiftwidth=2
However when I open or create an html file it indents 8 spaces rather than 2. What am I missing?
Thanks!

You can check where the indent options were set via
:verbose setlocal ts? sts? sw? et?
I believe in your case, the problem is with the list of patterns in your :autocmd:
au BufNewFile,BufRead *.js, *.html, *.css
There must not be a whitespace between the patterns:
au BufNewFile,BufRead *.js,*.html,*.css
See :help autocmd-patterns; it talks about comma-separated list, and the example there also doesn't have whitespace.
alternative approaches
By encoding the file patterns for the various languages, you're duplicating the built-in filetype detection.
I would recommend to put these :setlocal commands into ~/.vim/after/ftplugin/html.vim (and so on. This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/html.vim.)
Alternatively, you could define an :autocmd FileType {filetype\} ... directly in your ~/.vimrc. This would at least avoid the duplication of file patterns, but tends to become unwieldy once you have many customizations.

Related

Error editing neovim config. E79: Cannot expand wildcards; plugins not working

When I open my init.vim, I'm met with this error message repeated many times
E79: Cannot expand wildcards
My neovim config file is stored: ~/.config/nvim/init.vim
And vim does not respond to my plugin commands,which have been installed via a plugin manager.
:help e79 returns:
Cannot expand wildcards
A filename contains a strange combination of characters, which causes Vim to
attempt expanding wildcards but this fails. This does NOT mean that no
matching file names could be found, but that the pattern was illegal.
The current contents of my init.vim:
set number
set scrolloff=16
set relativenumber
set tabstop=4 softtabstop=4
set shiftwidth=4
set expandtab
set smartindent
colorscheme koehler
au TextYankPost * silent! lua vim.highlight.on_yank {higroup="IncSearch", timeout=500}
call plug#begin('~./config/nvim/plugged')
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-telescope/telescope.nvim'
call plug#end()
let mapleader = " "
nnoremap <leader><CR> :so ~/.config/nvim/init.vim<CR>
vnoremap <leader>yy "+y
nnoremap <leader>pf :Files<CR>
Error messages when using plugin keywords:
E492: Not an editor command: Files

How to add html attributes and values for all lines quickly with vim and plugins?

My os:debian8.
uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux
Here is my base file.
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
I want to create a html file as bellow.
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
Every line was added href and id attributes,whose values are line content pasted .html and line content itself correspondingly.
How to add html attributes and values for all lines quickly with vim and plugins?
sed,awk,sublime text 3 are all welcomed to solve the problem.
$ sed 's:.*:&:' file
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
if you want to do this in vi itself, no plug-in neccessary
Open the file, type : and insert this line as the command
%s:.*:&
it will make all the substitutions in the file.
sed is the best solution (simple and pretty fast here) if your are sure of the content, if not it need a bit of complexity that is better treated by awk:
awk '
{
# change special char for HTML constraint
Org = URL = HTML = $0
# sample of modification
gsub( / /, "%20", URL)
gsub( /</, "%3C", HTML)
printf( "%s\n", URL, Org, HTML)
}
' YourFile
To complete this easily in Sublime Text, without any plugins added:
Open the base file in Sublime Text
Type Ctrl+Shift+P and in the fuzzy search input type syn html to set the file syntax to HTML.
In the View menu, make sure Word Wrap is toggled off.
Ctrl+A to select all.
Ctrl+Shift+L to break selection into multi-line edit.
Ctrl+C to copy selection into clipboard as multiple lines.
Alt+Shift+W to wrap each line with a tag-- then tap a to convert the default <p> tag into an <a> tag (hit esc to quit out of any context menus that might pop up)
Type a space then href=" -- you should see this being added to every line as they all have cursors. Also you should note that Sublime has automatically closed your quotes for you, so you have href="" with the cursor between the quotes.
ctrl+v -- this is where the magic happens-- your clipboard contains every lines worth of contents, so it will paste each appropriate value into the quotes where the cursor is lying. Then you simply type .html to add the extension.
Use the right arrow to move the cursors outside of the quotes for the href attribute and follow the two previous steps to similarly add an id attribute with the intended ids pasted in.
Voila! You're done.
Multi-line editing is very powerful as you learn how to combine it with other keyboard shortcuts. It has been a huge improvement in my workflow. If you have any questions please feel free to comment and I'll adjust as needed.
With bash one-liner:
while read v; do printf '%s\n' "$v" "$v" "$v"; done < file
(OR)
while read v; do echo "$v"; done < file
Try this -
awk '{print a$1b$1c$1d}' a='' d='' file
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
Here I have created 4 variable a,b,c & d which you can edit as per your choice.
OR
while read -r i;do echo ""$i";done < f
home
help
variables
compatibility
To execute it directly in vim:
!sed 's:.*:&:' %
In awk, no regex, no nothing, just print strings around $1s, escaping "s:
$ awk '{print "" $1 ""}' file
home
help
If you happen to have empty lines in there just add /./ before the {:
/./{print ...
list=$(cat basefile.txt)
for val in $list
do
echo ""$val"" >> newfile.html
done
Using bash, you can always make a script or type this into the command line.
This vim replacement pattern handles your base file:
s#^\s*\(.\{-}\)\s*$#\1#
^\s* matches any leading spaces, then
.\{-} captures everything after that, non-greedily — allowing
\s$ to match any trailing spaces.
This avoids giving you stuff like home .
You can also process several base files with vim at once:
vim -c 'bufdo %s#^\s*\(.\{-}\)\s*$#\1# | saveas! %:p:r.html' some.txt more.txt`
bufdo %s#^\s*\(.\{-}\)\s*$#\1# runs the replacement on each buffer loaded into vim,
saveas! %:p:r.html saves each buffer with an html extension, overwriting if necessary,
vim will open and show you the saved more.html, which you can correct as needed, and
you can use :n and :prev to visit some.html.
Something like sed’s probably best for big jobs, but this lets you tweak the conversions in vim right after it’s made them, use :u to undo, etc. Enjoy!

How to add different colors to mercurial template command?

What I want:
A command that prints number of added (+) and removed (-) from change log, where the added portion(+) written in green and deleted portion (-) written in red.
What I have currently:
hg log -T {diffstat} prints what I want (+20/-31:) but in black color.
hg log -T "{label('custom.colorcode', diffstat)} \n" prints the entire diffstat (+20/-31:) in green (my custom.colorcode is set to green in .hgrc)
References:
https://www.mercurial-scm.org/repo/hg/help/templates
Can I add custom colors to mercurial command templates?
I don't believe there is a way for Mercurial to automatically parse the diffstat output and to assign colors to parts of it, but you can use a workaround by doing the parsing yourself. E.g. with the following template:
hg log -T '{sub("(.*): (.*)/(.*)", "\\1: \033[0;32m\\2\033[0m/\033[0;31m\\3\033[0m", diffstat)}\n'
Note that this hardcodes ANSI color escapes (32 for green, 31 for red). If you want to do it with labels, this is also possible, but much slower (because diffstat has to be calculated multiple times). This approach can still be useful for other keywords, so I'm explaining it anyway. Here is an example template:
{sub(":.*","",diffstat)}: \
{label("diff.inserted", sub(".*([+][0-9]+).*", "\\1", diffstat))}/\
{label("diff.deleted", sub(".*(-[0-9]+).*", "\\1", diffstat))}
The easiest way to use such a long template is to put it in a file (for example ~/.hgtemplates/diffstat) and then use hg log -T ~/.hgtemplates/diffstat. If a template contains a slash or backslash and corresponds to an existing file, Mercurial will look at the contents of the file instead. Long templates can also be put in the templates section of your .hgrc, e.g.:
[templates]
diffstat = "{sub(":.*","",diffstat)}: \
{label("diff.inserted", sub(".*([+][0-9]+).*", "\\1", diffstat))}/\
{label("diff.deleted", sub(".*(-[0-9]+).*", "\\1", diffstat))}\n"
And can then be used with the corresponding name (e.g. hg log -T diffstat).

No auto-complete on HTML and CSS files in VIM (YouCompleteMe)

For some reason, I get no autocomplete on html and css files. All works well with other languages, for example JS, Ruby or Python.
I've spent close to 2h today trying to fix it but to no avail. I use Vundle and YouCompleteMe to do all of this. You can have a look at my .vimrc file here https://gist.github.com/comatory/73aacac1b0249b8c1c55.
I'm on OS X 10.10.5 and my Vim version is 7.4 (installed via Homebrew). I also use Macvim but it doesn't matter, it doesn't work in either of them.
Check the value of the paste setting
:verbose set paste?
will show you the current value for this buffer as well as which plugin set it last.
In my experience, completion doesn't happen if paste is set. Disable with
:set nopaste
Maybe it is in your filepath blacklist as in default.
Quote from: https://github.com/ycm-core/YouCompleteMe/
The g:ycm_filepath_blacklist option
This option controls for which Vim filetypes (see :h filetype) should filepath completion be disabled. The option value should be a Vim dictionary with keys being filetype strings (like python, cpp, etc.) and values being unimportant (the dictionary is used like a hash set, meaning that only the keys matter).
let g:ycm_filepath_blacklist = {'*': 1}
The * key is special and matches all filetypes. Use this key if you want to completely disable filepath completion:
You can get the filetype of the current file in Vim with :set ft?.
Default: [see next line]
let g:ycm_filepath_blacklist = {
\ 'html': 1,
\ 'jsx': 1,
\ 'xml': 1,
\}
I would suggest you use NeoComplete. I tried YouCompleteMe on Vim8, but doesn't work for some reason. NeoComplete does the job for me.
After installing NeoComplete just add, let g:neocomplete#enable_at_startup = 1 to your .vimrrc

HTML::Entities encoding and single ampersand

I'm attempting to use the following line of perl, as described here: Does anyone know of a vim plugin or script to convert special characters to their corresponding HTML entities? - to encode HTML entities in Vim.
%!perl -p -i -e 'BEGIN { use HTML::Entities; use Encode; } $_=Encode::decode_utf8($_) unless Encode::is_utf8($_); $_=Encode::encode("ascii", $_, sub{HTML::Entities::encode_entities(chr shift)});'
It works fine (£ to &pound, curly quotes etc.) except for an ampersand on it's own - & - which is left as it is.
I've tried removing the uf8 decoding, and looked at the CPAN documentation for HTML::Entities.
Answer:
#ZyX has answered the original question, but as others have pointed out in the comments, this is redundant as it's not actually necessary to use HTML entities if you are serving pages with a UTF-8 character set (which I am, both with the meta tag -
<meta charset="utf-8">
and also in the Apache configuration:
AddDefaultCharset utf-8
Indeed it's arguably a bad thing adding them in such cases; the filesize is bigger and the text is obfuscated should anyway want to make use of the source code.
It's essential you ensure whatever editor(s) you use to create files are writing them in UTF-8 as well.
My answer was only encoding characters that are above ascii range. If you want to encode something as html, you should use
$text=HTML::Entities::encode_entities($text);
:
%!perl -MHTML::Entities -MEncode -p -i -e '$_=Encode::decode_utf8($_) unless Encode::is_utf8($_); $_=HTML::Entities::encode_entities($_);'
I was not using this in that answer because TS only requested to encode unicode characters without encoding <, >, & as well.
By the way, you may use $text=HTML::Entities::encode_entities($text, '<>&"'); to encode only really unsafe characters (though I guess this is easily expressed with vimscript:
:let entities={'<': 'lt', '>': 'gt', '&': 'amp', '"': 'quot'}
:execute '%s/['.escape(join(keys(entities), ''), '\-]^').']/\="&".entities[submatch(0)].";"/g'
perl -MHTML::Entities -i -e 'print encode_entities shift'
should work, doesn't it?