Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best strategy for code chunks and macros in vim?

Tags:

vim

vi

As I develop more with vim, I find myself wanting to copy in blocks of useful code, similar to "templates" in Eclipse.

I was thinking of making a separate file for each code chunk and just reading them in with

:r code-fornext

but that just seems kind of primitive. Googling around I find vim macros mentioned and something about "maps" but nothing that seems straightforward.

What I am looking for are e.g. something like Eclipse's "Templates" so I pop in a code chunk with the cursor sitting in the middle of it. Or JEdit's "Macros" which I can record doing complicated deletes and renaming on one line, then I can play it again on 10 other lines so it does the same to them.

Does vim have anything like these two functionalities?

like image 588
Edward Tanguay Avatar asked Sep 10 '08 15:09

Edward Tanguay


People also ask

Do Vim macros persist?

6) actually persists macros and named buffers automatically (by default, although I haven't looked for a way of turning this behavior off). Closing a Vim session will update the ~/. viminfo file with any named buffers / macros.

How do I delete a macro in Vim?

To clear a single macro - record over it - with nothing. For your situation in normal mode you'd just type "qwq".

How do I record in Gvim?

To start recording, press q in normal mode followed by a letter ( a to z ). That starts recording keystrokes to the specified register. Vim displays recording in the status line. Type any normal mode commands, or enter insert mode and type text.


1 Answers

To record macros in Vim, in the command mode, hit the q key and another key you want to assign the macro to. For quick throw away macros I usually just hit qq and assign the macro to the q key. Once you are in recording mode, run through your key strokes. When you are done make sure you are back in command mode and hit q again to stop recording. Then to replay the macro manually, you can type @q. To replay the previously run macro you can type @@ or to run it 10 times you could type 10@q or 20@q, etc..

In summary:

+----------------------------------+-------------------------------------+
| start recording a macro          | qX (X = key to assign macro to)     |
+----------------------------------+-------------------------------------+
| stop recording a macro           | q                                   |  
+----------------------------------+-------------------------------------+
| playback macro                   | @X (X = key macro was assigned to)  |
+----------------------------------+-------------------------------------+
| replay previously played macro   | @@                                  |
+----------------------------------+-------------------------------------+

In regards to code chunks, I have found and started using a Vim plug-in called snipMate, which mimics TextMate's snippets feature. You can get the plug-in here:

http://www.vim.org/scripts/script.php?script_id=2540

And a short article on using snipMate (along with a short screencast showing it in use):

http://www.catonmat.net/blog/vim-plugins-snipmate-vim/

Hope you find this helpful!

like image 172
brian newman Avatar answered Oct 14 '22 14:10

brian newman