Saving time with vim editor

vim_with_cleansing_action_by_carsonfire

How to create macros in vim editor

I’ve been talking so much in class about the nice and amazing features vim has. Specially the ability to edit or save macros and even record and reproduce it later for further use.

So today I’m going to teach you how to create a simple macro to insert a bunch of text inside your file, just typing some word. Imagine, for instance we want to create an HTML structure or any other programming language. Or just insert a GPL license headers into all our code files. Better you have a nice method to do it or it’s going to be a hard task.

Vim as you probably already know reads (if exist) a file located into your $HOMEDIR (/home/username/) called .vimrc

This file can contain any parameter, config setting or even vim syntax inside, so vim are going to read and execute everything we put inside this file.

We can simply define macros inside vim like this documentation explains.

Or we can use some different methods like make an abbreviation, or just map some key to assign the execution of our macro to that key or keys combination ( i.e.: Crtl + R ) . For example we would like to press F1 to insert some text:

To accomplish this we only need to edit our ~/.vimrc file and type:

:map <F2> blah<CR>

Which means when I type F2 vim writes «blah» on the buffer. There are several ways to do this because depending on what mode you are in vim you can define if the mapping works or not.

NOTE: <CR> is just an enter.

For example if we want to be able to use the F2 mapping to write some text it’s ok how we defined above the mapping. But if we want to do this when we’re typing text in INSERT mode, we should define the mapping like this:

:imap <F2> blah<CR>

Note the «i» of «imap» instead of «map». And of course it has nothing to do with IMAP mail protocol. If you take a look inside vim documentation you will se that we have also mapping methods like, nnoremap, imap, nmap, map,… and so on. Every one with some different behaviors.

But instead of just type F2 or a key combination what if vim could detect what are we typing and replace automatically that with some previous chosen text?

We can use abbreviations. Type this into your .vimrc file:

iab HTML <HTML><CR><BS><CR>     <TITLE>EXAMPLE_TITLE</TITLE><CR>       <body><CR><CR><CR>     </body><CR></HTML>

NOTE: it’s a ONE LINE sentence!

Syntax explained: <vim command>  <macro_name> <macro_content>

So:

  • iab: is an INSERT mode abbreviation. ( there are also several types for other modes )
  • HTML: is the name of our «macro» command.

and finally

  • <HTML><CR><BS><CR>     <TITLE>EXAMPLE_TITLE</TITLE><CR>       <body><CR><CR><CR>     </body><CR></HTML>

    Is the text we want to put inside our file. This text contain the proper ENTERS (<CR>) and TABS to indent the code.

Save it and then open the vim editor and in INSERT mode type HTML and press ENTER.
It should appear an HTML structure like the one defined into the .vimrc file.

Example:

Captura de pantalla 2016-12-14 a las 13.02.28.png

Cool eh?

But could be a bit hard to insert large and more complex lines with parameters right? So for this reason, for more amazing stuff or large and complex strings you can use something like create a new file generate_html.vim in ~/.vim/plugins/ with the following code:

"
" <This plugin generates html tags to new html files>
"

autocmd BufNewFile  *.html  call    Generate_html()

function! Generate_html()
    call append(0, "<!DOCTYPE HTML>")
    call append(1, "<html><head>")
    call append(2, "    <title></title>")
    call append(3, '    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />')
    call append(4, '    <style type="text/css">')
    call append(5, '    </style>')
    call append(6, '</head>')
    call append(7, '<body>')
    call append(8, '</body>')
    call append(9, '</html>')
endfunction

the three first lines beginning with » are COMMENTS (vim comments) and are not going to be executed. The we define a function named Generate_html and we call this function everytime a new file named *.html is CREATED.

So every time you create any NEW file called whatever.html vim is going to insert those lines inside.

As you can see this is very useful for programmers or even devops.

Now, two very useful cheat sheets for vim:

vim-cheat-sheet-en

And vim default keymap for qwerty keyboards:

vi-vim-cheat-sheet

I hope you like it.

Enjoy!

 

Un comentario en “Saving time with vim editor

Deja un comentario