Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I edit a lot of PHP code with Vim, and I've run into a snag with some macros in my .vimrc.

I currently use these two macros to comment/uncomment in C-style

"c-style (//) comment (press the [.] key)
au FileType php vnoremap . :s/^\(\s*\)\(.\+\)$/\1\/\/\2/<CR>:noh<CR>gv
"c-style (//) uncomment (press the [,] key)
au FileType php vnoremap , :s/^\(\s*\)\/\//\1/<CR>:noh<CR>gv

And these two to comment/uncomment in HTML-style

"html-style (<!-- -->) comment (press the [.] key)
au FileType html vnoremap . :s/^\(\s*\)\(.\+\)$/\1<!-- \2 -->/g<CR>:noh<CR>gv
"html-style (<!-- -->) uncomment (press the [,] key)
au FileType html vnoremap , :s/^\(\s*\)<!-- \(.*\) -->/\1\2/g<CR>:noh<CR>gv

These macros work fine when applied to their intended file-type, but since there is often HTML embedded inside a PHP file, the PHP commenting style is useless.

Is there a way for Vim to detect if it's working on a chunk of HTML code inside a PHP file, and then apply the correct commenting behaviour?

Edit: I know that I can manually set the filetype, and that there are plugins available for this, I was looking to learn more about writing better macros.

share|improve this question

2 Answers

In case you can accept to choose manually which type of comment you want to use, I would recommend this tip.

You would have 4 combinations to remember:

C-style:
,* comment  
,c uncomment

HTML style: 
,< comment 
,d uncomment
share|improve this answer

To do what you want there is really no way to get around writing a function and mapping it your preferred keys.

To get started :echo synIDattr(synID(line("."), col("."), 1), "name") will print the name of the highlight group for the current word under the cursor.

You can use that information to determine which comment style to use.

A rough example to get you started:

function! s:MyComment () range
    for i in range(a:firstline, a:lastline)
        exe "normal " . i . "gg"
        normal ^
        let stuff = synIDattr(synID(line("."), col("."), 1), "name")
        if strpart(stuff, 0, 4) == "html"
            normal I<!-- 
            normal A -->
        else
            normal I//
        endif
    endfor
endfunction
vmap <silent> . :call <SID>MyComment()<CR>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["f=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &copy; 2013 stack exchange inc; \r\n                user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc-wiki</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"footer-flair\">\r\n                <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" class=\"cc-wiki-link\"></a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2013.5.22.697\r\n            </div>\r\n                <div id=\"additional-notices\">\r\n                    Linux is a registered tra", "\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &copy; 2013 stack exchange inc; \r\n                user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc-wiki</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"footer-flair\">\r\n                <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" class=\"cc-wiki-link\"></a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2013.5.22.697\r\n            </div>\r\n                <div id=\"additional-notices\">\r\n                    Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group. <br>This site is not affiliated with Linus Torvalds or The Open Group in any way.\r\n                </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Unix &amp; Linux Sta"], "copyright__copyright__copyright": ["^\n        let stuff = synIDattr(synID(line(\".\"), col(\".\"), 1), \"name\")\n        if strpart(stuff, 0, 4) == \"html\"\n            normal I<!-- \n            normal A -->\n        else\n            normal I//\n        endif\n    endfor\nendfunction\nvmap <silent> . :call <SID>MyComment()<CR>\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}