给PHP 和HTML 专用的VIM 缩排(Indent)

 

转载自 http://plog.longwin.com.tw/my_note-app-setting/2009/01/10/vim-indent-for-php-html-2009

 

 

Vim 在写*.html 和*.php 时, 在HTML 排版上, 都有些不够聪明的状况.

  • *.html => 自動排版會將所有HTML Tag 都排在同一列, 等同於沒有縮排. *.html => 自动排版会将所有HTML Tag 都排在同一列, 等同于没有缩排.
  • *.php => 自動排版會將HTML Tag 排在在同一列外, 按Enter 後, 游標會在第一個位置, 不會做聰明的縮排動作. *.php => 自动排版会将HTML Tag 排在在同一列外, 按Enter 后, 游标会在第一个位置, 不会做聪明的缩排动作.

註: 下述太多不想看, 只想直接解決的, 只要下載html.vim(於indent/html.vim ) 和php.vim(於Better indent support for php with html ), 將這兩個檔案放到~/.vim/indent, 再於.vimrc 加入filetype indent on就完成囉~注:下述太多不想看,只想直接解决的,只要下载html.vim(于indent/html.vim )和php.vim(于Better indent support for php with html ),将这两个档案放到~/.vim/indent,再于.vimrc加入filetype indent on就完成啰~

要解決上述的問題, 由下面兩個步驟來解決:要解决上述的问题, 由下面两个步骤来解决:

  1. 設定VIM 讓HTML 正確縮排(Indent) - 先讓HTML 正確縮排设定VIM 让HTML 正确缩排(Indent) - 先让HTML 正确缩排
  2. 設定VIM 讓*.php 做HTML 縮排(Indent) - 讓*.php 遇到HTML 時, 能用HTML 縮排來排版设定VIM 让*.php 做HTML 缩排(Indent) - 让*.php 遇到HTML 时, 能用HTML 缩排来排版

設定VIM 讓HTML 正確縮排(Indent)设定VIM 让HTML 正确缩排(Indent)

首先讓HTML 能正確的縮排, 步驟如下:首先让HTML 能正确的缩排, 步骤如下:

  1. indent/html.vim - alternative html indent script于indent/html.vim - alternative html indent script
  2. 下載html.vim下载html.vim
  3. mkdir ~/.vim/indent mkdir ~/.vim/indent
  4. mv html.vim ~/.vim/indent mv html.vim ~/.vim/indent
  5. 於~/.vimrc 加入filetype indent on (Enable filetype detection and use of indent plugins)于~/.vimrc加入filetype indent on (Enable filetype detection and use of indent plugins)

設定VIM 讓*.php 做HTML 縮排(Indent)设定VIM 让*.php 做HTML 缩排(Indent)

HTML 既然都可以縮排, 本來想法是把filetype=php 指成html, 就可以先頂著用, 不過這樣子在寫php 時又得要換回來, 有點麻煩. HTML 既然都可以缩排, 本来想法是把filetype=php 指成html, 就可以先顶着用, 不过这样子在写php 时又得要换回来, 有点麻烦.

此篇Better indent support for php with html有另外一種解法, 解法沒有研究, 不過稍微看看, 大概是下面的流程:此篇Better indent support for php with html有另外一种解法,解法没有研究,不过稍微看看,大概是下面的流程:

  1. 縮排時先執行indent/html.vim, 如果有做過縮排, 就結束.缩排时先执行indent/html.vim, 如果有做过缩排, 就结束.
  2. 若沒有縮排, 再執行indent/php.vim (php.vim 的內容在下面)若没有缩排, 再执行indent/php.vim (php.vim 的内容在下面)

php.vim # 將下面的文字存成php.vim, 並搬到~/.vim/indent 下即可. (此php.vim 轉載自: " Better indent support for php with html ") php.vim #将下面的文字存成php.vim,并搬到~/.vim/indent下即可. (此php.vim转载自: " Better indent support for php with html ")

" Better indent support for PHP by making it possible to indent HTML sections
" as well.
if exists("b:did_indent")
finish
endif

" This script pulls in the default indent/php.vim with the :runtime command
" which could re-run this script recursively unless we catch that:
if exists('s:doing_indent_inits')
finish
endif
let s:doing_indent_inits = 1
runtime! indent/html.vim
unlet b:did_indent
runtime! indent/php.vim
unlet s:doing_indent_inits

function! GetPhpHtmlIndent(lnum)
if exists('*HtmlIndent')
let html_ind = HtmlIndent()
else
let html_ind = HtmlIndentGet(a:lnum)
endif
let php_ind = GetPhpIndent()
" priority one for php indent script
if php_ind > -1
return php_ind
endif
if html_ind > -1
if getline(a:num) =~ "^<?" && (0< searchpair('<?', '', '?>', 'nWb')
\ || 0 < searchpair('<?', '', '?>', 'nW'))
return -1
endif
return html_ind
endif
return -1
endfunction

setlocal indentexpr=GetPhpHtmlIndent(v:lnum)
setlocal indentkeys+=<>>

转载于:https://www.cnblogs.com/striveford/articles/1950339.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值