34

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
luqui
  • 59,485
  • 12
  • 145
  • 204
  • Great question -- I've always wished you could enable extended modelines for various things such as custom matchings or similar stuff. Used to write temporary ftplugins for that. – mike3996 Apr 14 '11 at 12:25

3 Answers3

53

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

:@"
Baramin
  • 1,359
  • 10
  • 12
  • 2
    Also see [http://stackoverflow.com/questions/3997078/how-do-you-paste-text-into-a-vim-colon-command/3997110#3997110 ](this answer) for more insight on how to use registers – Benoit Nov 24 '10 at 16:00
  • 3
    This works with any register, by the way. You can do a `"ayy` to yank your whole line to a, then use `@a` to run it. Note that you're going to get a bunch of spaces if you run an indented line, which might screw things up (it'll move you around in normal mode). Might be better to select the command with visual mode `v`. – naught101 Apr 11 '12 at 06:50
  • 2
    I have `nnoremap 2 :@"` and I use it a lot. (mnemonic is Shift-2=@=Execute.) – Brady Trainor Aug 20 '14 at 21:35
  • +1, but note that this doesn't work if the commands are continued on a separate line with the '\' continuation character (see `:help line-continuation`). – Codie CodeMonkey Apr 17 '19 at 18:00
4

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>
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jake
  • 2,106
  • 1
  • 24
  • 23
1

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.

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83