≡ Menu

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

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.


   :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.

Add your comment

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

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • nikhil March 3, 2009, 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

  • SathiyaMoorthy March 3, 2009, 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.

  • nikhil March 4, 2009, 7:43 am

    thanks a lot.
    worked like a charm.

  • jihan March 5, 2009, 1:55 am

    very useful article