How to turn off HtmlBeautify for md files vim - vim

I want to turn off htmlbeautify for md files.
I am using the plug in vim-jsbeautify and use a plug in on save the following way: in my vimfiles/ftplugin folder
├── ftplugin
│   ├── html
│   │   └── main.vim
I have added a html folder and added a main.vim file with the content
autocmd BufWritePre <buffer> call HtmlBeautify()
noremap <buffer> <c-f> :call HtmlBeautify()<cr>
so that html files are formatted on save. The command verbose set filetype returns
filetype=markdown
Last set from /usr/share/vim/vim80/filetype.vim
for md files
Some how the md files are also considered of html files. how can I turn this of?

Inside $VIMRUNTIME/ftplugin/markdown.vim you will find the following line:
runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
This will source html ftplugins for markdown files as Markdown uses html in its syntax.
One way to prevent this is do add an :if to avoid these inclusion for markdown.
if &filetype == 'html'
autocmd BufWritePre <buffer> call HtmlBeautify()
noremap <buffer> <c-f> :call HtmlBeautify()<cr>
endif

Related

Run selected text through filter and reinsert

I like using code cleanup scripts; perldidy; uglifierjs, etc. I previously ran them all like this in my vimrc...
map <leader>pt :!perltidy<CR>
map <leader>jt :!uglifyjs -b<CR>
map <leader>pjt :!python -mjson.tool<CR>
map <leader>ct :!column -t<CR>
How this functionally works; it runs currently selected text throguh the CLI program, and replaces the selection with the output. Works wonderfully; but as you can see I now have to stop and think what beautifier I want to run and remember the nemonic I have for it. This led me to think there has to be a better way. So I tried did this...
map <leader>jt :call RunTidy()<cr>
function! RunTidy()
if (&ft == "javascript")
echo 'is js..'
:'<,'>!ulifyjs -b
endif
if (&ft == "json")
echo 'is json'
:'<,'>!python -mjson.tool
endif
endfunction
The problem being this just doesn't work; executing once for each line and not replacing contents.. Anyone aware of a better way to do this? I feel like this should be a solved problem...
augroup filters
autocmd!
autocmd FileType perl map <buffer> <leader>t :!perltidy<CR>
autocmd FileType javascript map <buffer> <leader>t :!uglifyjs -b<CR>
autocmd FileType json map <buffer> <leader>t :!python -mjson.tool<CR>
augroup END
or put those mappings, all with the same lhs, in different ~/.vim/after/ftplugin/{filetype}.vim.
Use ftplugins. You need to have filetype plugin indent on in your vimrc.
Create files in ~/.vim/ftplugin/<filetype>.vim. These files will be sourced when the file type is set.
For example using javascript put the following in ~/.vim/ftplugin/javascript.vim
noremap <buffer> <leader>jt :!uglifyjs -b<CR>
to map <leader>jt to :!uglifyjs -b<CR> in all javascript buffers. This will not show up in other filetypes.
You would do the same for all other filetypes.
You can do the same for file type specific settings by using setlocal.
Take a look at :h ftplugin

How do I set a .vimrc configuration for many file types?

Let's say I want one group of settings for HTML, CSS and JavaScript files but another set for Ruby files (completely different). What's the simplest way to do this?
You can put ftplugin directory with filetype-specific settings inside
.vim directory
.vim
└── ftplugin
└── ruby.vim
└── markdown.vim
And keep your settings there. The are applied when file with
corresponding filetype is opened.
Also, you might need to have filetype detection(if not detected
properly). You can put this to your .vimrc
autocmd BufNewFile,BufRead *.markdown,*.md,*.mdown,*.mkd,*.mkdn set ft=markdown
Or, put it into ftdetect directory
.vim
└── ftdetect
└── markdown.vim
You can give global settings like this.
For other files:
set shiftwidth=4
for Ruby files:
autocmd BufRead,BufNewFile *.rb set shiftwidth=2
You can get what you want via autocmd, it's format as follows:
au[tocmd] [group] {event} {pattern} [nested] {cmd}
[group] and [nested] are optional.As I give the example above, BufRead,BufNewFile is the event, *.rb is the pattern, and set shiftwidth=2 is the cmd.
more information about autocmd, please refer: :help automcd

writing to .vimrc breaks pathogen?

I have pathogen set up in my vim installation and various plugins installed.
"Call pathogen to set up various plugins
"filetype off
call pathogen#infect()
call pathogen#incubate()
call pathogen#helptags()
When I write my .vimrc in vim, the following command is supposed to reload the file (and it does seem to work).
" Source the vimrc file after saving it
if has("autocmd")
autocmd bufwritepost .vimrc source $MYVIMRC
endif
After writing .vimrc, however, pathogen does not reload.
Here is the output of :set rtp? after starting vim:
runtimepath=~/.vim,~/.vim/bundle/Jellybeans,~/.vim/bundle/TwitVim,~/.vim/bundl
e/badwolf,~/.vim/bundle/calendar,~/.vim/bundle/tagbar,~/.vim/bundle/vim-airline,
~/.vim/bundle/vim-colors-solarized,~/.vim/bundle/vim-colorschemes,~/.vim/bundle/
vizardry,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/s
hare/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
and after :w in .vimrc it returns to the default.
runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/
vim74,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
I tried adding the following modification to no avail...
if has("autocmd")
autocmd bufwritepost .vimrc source $MYVIMRC
autocmd bufwritepost .vimrc call pathogen#incubate()
endif
I've been looking around and can't seem to find a solution other than just restarting vim every time I modify my .vimrc, which is fairly disruptive. Any help would be appreciated.
edit: output of tree -d -L 2...
.
├── autoload
└── bundle
├── badwolf
├── calendar
├── color~
├── Jellybeans
├── tagbar
├── TwitVim
├── vim-airline
├── vim-colorschemes
├── vim-colors-solarized
└── vizardry
The problem may be the following lines on your .vimrc:
" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim
On $VIMRUNTIME/debian.vim there is a line that reset the runtime path:
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
The problem doesn't happens when the .vimrc is loaded on startup because these lines are executed before call pathogen#infect(). When you reload your .vimrc you are overwriting your &rtp, but pathogen doesn't set it again (possible because s:done_bundles is already set).

Vim bind some hotkeys different to filetype

I want to bind something like this:
For CSS, HTML files: <c-space> <c-x><c-n>
For Ruby files: <c-space> <c-x><c-u>
How to do this?
The documentation is more complete, precise and correct, but briefly:
Make a file in your ftplugin folder (create this folder if necessary) starting with the filetype you'd like to use.
├── vim
│   ├── after
│   ├── autoload
│   ├── bundle
│   ├── ftplugin
│   │   ├── python.vim
│   │   ├── rnoweb.vim
│   │   └── tex.vim
│   └── syntax
and in one of of these files, the python.vim one for example, a line like
noremap! <buffer> <F5> <Esc>:w<CR>:!python % <CR>
will be a bind only defined for this filetype, just in this buffer. Make sure filetype plugin on or similar appears in your vimrc.
Edit: excellent suggestions by Peter Rincker incorporated: noremap version of map command and <buffer> flag. He also reminds us to use setlocal instead of set in our filetype plugin files.
Use autocmd:
autocmd filetype css inoremap <buffer> <c-space> <c-x><c-n>
autocmd filetype html inoremap <buffer> <c-space> <c-x><c-n>
autocmd filetype ruby inoremap <buffer> <c-space> <c-x><c-u>
Put the following in your .vimrc. Substitute the do stuff lines with your bindings.
:if match(#%, ".css") >=0
: do css stuff
:endif
:if match(#%, ".rb") >=0
: do ruby stuff
:endif

Can you have file type-specific key bindings in Vim?

In my .vimrc file, I have a key binding for commenting out that inserts double slashes (//) at the start of a line:
" the mappings below are for commenting blocks of text
:map <C-G> :s/^/\/\//<Esc><Esc>
:map <C-T> :s/\/\/// <Esc><Esc>
However, when I’m editing Python scripts, I want to change that to a # sign for comments
I have a Python.vim file in my .vim/ftdetect folder that also has settings for tab widths, etc.
What is the code to override the keybindings if possible, so that I have Python use:
" the mappings below are for commenting blocks of text
:map <C-G> :s/^/#/<Esc><Esc>
:map <C-T> :s/#/ <Esc><Esc>
You can use :map <buffer> ... to make a local mapping just for the active buffer. This requires that your Vim was compiled with +localmap.
So you can do something like
autocmd FileType python map <buffer> <C-G> ...
The ftdetect folder is for scripts of filetype detection. Filetype plugins must be inside the ftplugin folder. The filetype must be included in the file name in one of the following three forms:
.../ftplugin/<filetype>.vim
.../ftplugin/<filetype>_foo.vim
.../ftplugin/<filetype>/foo.vim
For instance, you can map comments for the cpp filetype putting the following inside the .../ftplugin/cpp_mine.vim:
:map <buffer> <C-G> :s/^/\/\//<Esc><Esc>
:map <buffer> <C-T> :s/\/\/// <Esc><Esc>
I prefer to have my configuration in a single file so I use the autocmd approach.
augroup pscbindings
autocmd! pscbindings
autocmd Filetype purescript nmap <buffer> <silent> K :Ptype<CR>
autocmd Filetype purescript nmap <buffer> <silent> <leader>pr :Prebuild!<CR>
augroup end
Vim doesn't clear set autocmds when you source your vimrc, so starting vim, changing something in your vimrc and running :so ~/.vimrc would define autocmds twice. That's why the bindings are grouped and cleared with autocmd! group_name. You can read more here.
Since mappings are applied to every buffer by default, and you want to change them for buffers matching the filetype only, the <buffer> modifier is in there, limiting the mappings to the local buffer.
Btw... if your primary problem is about commenting... you should check out 'nerdcommenter' plugin, its the fastest way to comment/uncomment your code in java/c/c++/python/dos_batch_file/etc etc.
This is only a partial answer for people coming here having difficulties getting any ftplugin scripts working, but remember that your .vimrc (or a file that it sources) should contain
filetype plugin on
or
:filetype plugin on
for filetype-plugins to be executed when a file of a given type is loaded.
I recommend the .../ftplugin/<filetype>.vim approach that freitass suggests, but in your specific case Vim Commentary will solve all of this for you.

Resources