1

I tried the following in my .vimrc using the hints I found here:

map <Tab> i4<Space><Esc>
map <F2> /79*<CR>79*/<CR>PROGRAM<Space>NAME<Space><Space><Space><Space>:

The first mapping inserts a 4 while the behavior that I expected is to insert 4 whitespaces characters.

And the second one throws the following error:

E486: Pattern not found: 79*

while I expected it to produce the following line:

/*******************************************************************************
PROGRAM NAME    :

Is there something up there that I'm doing wrong or there's something I'm missing here?

6
  • 1
    You should point what you expect to get by both of this mappings (preferably, not as VimScript comments, because comments are not allowed after map) The first one must be 4i not i4; the second one, I can't even guess what you're trying to do.
    – Matt
    Dec 4, 2019 at 6:11
  • Sorry about that. Should have added it to make things clear. Just did that now. Dec 4, 2019 at 6:21
  • 2
    For the second mapping what it currently does: 1. search for 79(9), then 2. search for 79th occurence of current word under cursor, then 3. search again for previous search, then pasting, replacing and whatever should happen when you try enter PROGRAM in normal mode.
    – Maxim Kim
    Dec 4, 2019 at 6:21
  • Just to let you know, in vim there is >> and << used to indent and dedent the line. You don't have to explicitly insert spaces like that. :nnoremap <tab> >> should work for you. What would be inserted (spaces or tab) depends on expandtab option.
    – Maxim Kim
    Dec 4, 2019 at 6:40
  • 2
    I would suggest to use > and < normal commands. They work in visual mode and with text objects. Put your cursor on a paragraph and press >ip, it would indend whole paragraph. Then press . which would repeat previous operation.
    – Maxim Kim
    Dec 4, 2019 at 6:46

1 Answer 1

3

For the first one, exchange i and 4:

nnoremap <Tab> 4i<Space><Esc> " inserts 4 space characters

i starts insert mode and whatever you "type" is inserted into text. If you pass count before i, whatever you type afterwards would be repeated count times.

For the second one -- not sure what it is supposed to do.

EDIT For the second one:

nnoremap <F2> i/<esc>79a*<esc>oPROGRAM NAME    :<esc>
  1. i/<esc> -- insert /
  2. 79a*<esc> -- add 79 stars
  3. oPROGRAM NAME :<esc> -- open new line and insert PROGRAM NAME :

PS

better use nnoremap instead of map as it is more specific for normal mode and it uses no remapped key sequences (imagine you have remapped i and o and other used keys to smth else...)

PPS

I personally would go with functions, like

func! CstyleHeader()
    let stars = repeat('*', 79)
    let lines = ['/' . stars, 'PROGRAM NAME    :', stars . '/']
    call append('.', lines)
endfunc
map <F2> :call CstyleHeader()<CR>
4
  • The second line is basically like a program header that I would like to print at the top of the source file - a C comment starting with /* followed by 78 *s and a PROGRAM NAME in the next line. Dec 4, 2019 at 6:23
  • I have noticed that you completely discarded <CR> in your answer. Any specific reason for replacing <CR> with o for new line charactrer? Aren't they the same things? Dec 4, 2019 at 6:39
  • 2
    To use <CR> for newline I should be in insert mode. If I "connect" it with previous 79a*<CR> it would insert 79 stars and newlines. So I should either make it as 79a*<esc>a<CR> or just 79a*<esc>o
    – Maxim Kim
    Dec 4, 2019 at 6:43
  • Got it. Makes sense. Dec 4, 2019 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.