3 Powerful Musketeers Of Vim Editor — Macro, Mark and Map

by Ramesh Natarajan on March 2, 2009

Vim - Macro, Mark & MapThis article is written by SathiyaMoorthy

This article is part of the ongoing Vi / Vim Tips and Tricks series. There are 3 powerful M’s in VIM — Macro, Mark, and Map. Each is intended for a separate job.

  1. Macro for recording a job and repeating it.
  2. Mark for bookmarking a particular position in a file and then coming back to it.
  3. Map for mapping a job to a key, and then executing that job by that key.

In this article, let us look at various features of Vim’s Macro, Mark and Map along with practical examples.

First M: Vim Macro

A macro is used to record a sequence of actions inside Vim. Once recorded, it can be executed multiple times using the repeat factor in vim.

Refer to our previous Vim Macro Tutorial article for more details on how to use Macro to record and play inside Vim.

Second M: Vim Mark

Mark is a bookmarking feature inside Vi and Vim editor, where you can bookmark a particular line and return to it quickly later. There are two types of marks — local and global.

Refer to our previous Vim Mark Tutorial article for more details on how to use Mark to bookmark inside Vim.

Third M: Vim Map

Using Vim Map feature, you can map a key to a particular job that you perform repeatedly.

Create a Map in Vim

In the following example, anytime you type :write , it will compile the current open *.c program file and execute the ./a.out, if the compilation is successful.

:map :write :!cc % & ./a.out
  • :map – Vim command to create the map
  • :write – Name of the map (map-name)
  • :!cc % & ./a.out – The command that should be executed when the map-name is called.

 

Vim Map Feature

Fig: Create a map called  :write to compile a C program and execute it

Execute the map

To execute the map, call the name of the map. In the example shown in Fig 1, :write is the name of the map. When you type :write, it will get automatically replaced with :!cc % & ./a.out inside the Vim and compile the C program and execute the a.out.

Display Available maps

Type :map that will display all the available maps as shown below.

:map

   :write        :!cc % & ./a.out
   <xHome>       <Home>
   <xEnd>        <End>
   <S-xF4>       <S-F4>
   <S-xF3>       <S-F3>
   <S-xF2>       <S-F2>
   <S-xF1>       <S-F1>
   <xF4>         <F4>
   <xF3>         <F3>
   <xF2>         <F2>
   <xF1>         <F1>

Recommended Reading

Learning the Vi and Vim Editors, by Arnold Robbins. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to carry the Vi editor pocket reference with me all the times. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.

 

This article is part of the ongoing Vi / Vim Tips and Tricks series. Please subscribe to TheGeekStuff and don’t miss any future Vi and Vim editor tips and tricks.
 
This article was written by SathiyaMoorthy, developer of Enterprise Postgres Query Analyser, an efficient tool for parsing postgresql log to generate html report, which can be used for fine tuning the postgres settings, and sql queries. The Geek Stuff welcomes your tips and guest articles.

Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. Vi and Vim Macro Tutorial: How To Record and Play
  2. How To Add Bookmarks Inside Vim Editor
  3. Vi and Vim Editor: 12 Powerful Find and Replace Examples
  4. 8 Essential Vim Editor Navigation Fundamentals
  5. Vim Editor: How to Correct Spelling Mistakes Automatically
  

Vim 101 Hacks Book

{ 4 trackbacks }

3 Powerful Musketeers Of Vim Editor — Macro, Mark and Map « CrapIRead.com
March 25, 2009 at 4:47 pm
Vim - Ressourcen im Netz | duetsch.info
May 11, 2009 at 2:33 am
Turbocharge Firefox Browser With Vim Editor Functionality Using Vimperator Add-on
June 5, 2009 at 10:27 am
Become a Better Writer from Unix Command Line
July 15, 2009 at 12:03 am

{ 4 comments… read them below or add one }

1 nikhil March 3, 2009 at 11:39 am

thanks a lot
the map article was most helpful
now i dont have to compile and run the program again and again

i had one question though
when i create a file with vim
another file with the same name and a ~(tilde) is created in the end.
how can i stop this

2 SathiyaMoorthy March 3, 2009 at 11:54 pm

set nobackup

Having this option set in ~/.vimrc , additional files created will be removed. Check it.

After setting this your problem might get solved, if it does not try out the following also.
set noswapfile

If you use this option, then recovery will become impossible. But if you want this you can use it for solving your issue.

3 nikhil March 4, 2009 at 7:43 am

great
thanks a lot.
worked like a charm.

4 jihan March 5, 2009 at 1:55 am

very useful article
thanks

Leave a Comment

Previous post:

Next post: