How to execute selected text as Vim commands

隐身守侯 提交于 2019-12-29 16:27:45

问题


When I am writing documents, I find myself settling on an organization convention appropriate to that document, and I would like Vim to syntax highlight that convention. But making a ftplugin is too "global", I want the syntax coloring to come with the document, so if I send it somewhere without that plugin they can still get the coloring. I found that you can't do it through the modeline because that only accepts options.

Right now I am trying to find out if there is a way to select some text in visual mode (or whatever) and have it executed as a series of Vim commands.

For example, at the bottom of one document I have:

vim highlighting:
    syn match Comment "^>.*$"

How can I select that text and say "boom, execute it" rather than having to retype it?


回答1:


You can select the lines, yank the selection and execute it with

:@"



回答2:


I like to make this mapping which will require you to make a visual selection first and doesn't overwrite your unnamed register:

vmap <space> "xy:@x<CR>

If you like to write Vim commands with line continuations:

vmap <space> :w! /tmp/x.vim<CR>:so /tmp/x.vim<CR>



回答3:


I've defined a few mappings based on lh-vim-lib that do not involve any register. This is indeed an overkill solution, but a neat one for mappings -- as it will never modify the default register.



来源:https://stackoverflow.com/questions/4268532/how-to-execute-selected-text-as-vim-commands

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!