Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute selected text as Vim commands

Tags:

vim

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?

like image 630
luqui Avatar asked Nov 24 '10 15:11

luqui


People also ask

How do I run a command in Vim?

You can run commands in Vim by entering the command mode with : . Then you can execute external shell commands by pre-pending an exclamation mark ( ! ). For example, type :! ls , and Vim will run the shell's ls command from within Vim.

How do I search for a selection in Vim?

When you press * ( Shift + 8 ) in Normal mode, Vim will search for the word (i.e. keyword ) under the cursor. Assuming the text you want to search is a single word, this is a great way to search.

How do I run a Vim command in terminal?

You can run the shell commands from inside of Vim by just using :! before the command, : means you have to be in command mode. Just after being in command mode, the ! or bang operator will execute the command typed after it from the terminal(Linux/ macOS) or your default shell(Windows -> CMD/Powershell).

How do I run a .sh file in Vim?

To do this, in the command mode of Vim, just input colon (:) followed by a bang (!) and finally the command ('wc' in this case) followed by the file name (use % for current file). After you are done seeing the output, press the Enter key and you'll be taken back to your Vim session.


1 Answers

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

:@" 
like image 136
Baramin Avatar answered Sep 24 '22 11:09

Baramin