Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scripting common tasks in Vim

While using Vim (at home and at work), I often find myself doing similar things repeatedly. For example, I may turn a bunch of CSV text into a series of SQL inserts. I've been using Vim for years, but only recently have I tried to seriously think about how I could improve my productivity while using it.

My question is.. Is there a good way (or right way) to store commonly used commands or command sequences? And how is the best way to execute them? It would be nice to be able to use the same script on a live session and also over the command line against some file.

I'm hoping that I can store them in a .vim file so that I can hand them to coworkers (who are not as proficient with vim) for them to use.

like image 372
Jeremy Cantrell Avatar asked Nov 01 '08 07:11

Jeremy Cantrell


People also ask

How do I run a script in Vim?

You can always execute Vimscript by running each command in command mode (the one you prefix with : ), or by executing the file with commands using a :source command. Historically, Vim scripts have a . vim extension. Here, :so is a short version of :source , and % refers to the currently open file.

Why are macros used in Vim?

In Vim, a macro is a feature that allows you to record a sequence of commands that you use to perform a given task. You then execute that macro many times to repeat the same job in an automated way.

Are Vim macros persistent?

Vim 8.0 on MacOS Mojave (10.14. 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.


2 Answers

You can store your common task macros in .vim files, like this for example, and then you can load them with the command :so file.vim

Here you can find a bunch of useful macros, also I recommend you to learn well the useful q macro recording command, it's very very powerful...

The macros created with the q command are stored in a register, qq stores the macro in the q register, so when you end the recording you can simply paste the macro, with "qp and save it, later you can load it by simply yanking the macro into a register i.e.: "qY, the macros are only text and remember you can use any register instead of q. There is a Vim Script for storing q macros:

marvim : Macro Persistent Storage and Shareable Repository for VIM

Also take a look to the Vim Scripting Language.

like image 119
Christian C. Salvadó Avatar answered Nov 14 '22 21:11

Christian C. Salvadó


I use q/@ to record/replay a macro quite frequently.

The next step up is trying to write in something like 3 or fewer ex commands.

If something so complex that neither a macro nor a short ex sequence won’t suffice, I tend to write it as a Perl script instead. The vim script language is a bit too limited to try to do grand things in, for my taste. (Although vim 7 has made great strides in that respect, by borrowing a bunch of things from Python.)

Note that @ does something very simple: it takes the contents of a register and replays them as if you had typed them in normal mode. Likewise q simply records the sequence you are typing into the register you name. These are the self-same registers you use for yanking/putting – which means you can directly paste a recorded sequence into a file (.vimrc anyone?) or yank a sequence of commands from a file and replay it (so you could keep a bunch of them in ~/my-vim-macros.txt or something).

like image 35
Aristotle Pagaltzis Avatar answered Nov 14 '22 19:11

Aristotle Pagaltzis