Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've just created a Vim function to replace Windows linebreaks with Unix ones. Now I want to be able to type :linebreak and have it run. Here's my first attempt:

function UseUnixLineBreaks()
  :%s/^M/\r/g
endfunction

cmap linebreak :call UseUnixLineBreaks()<CR>

(By the way, that ^M is a control character - type it with Ctrl+v Ctrl+m)

When I try to run this by typing :linebreak, it does run, but it behaves oddly: each letter of the command appears in sequence, erasing the previous one. For example, I see :l, then :i, then :n, etc. When I type the last letter, it runs immediately.

I want the whole command to appear, so that I can see I've typed it correctly, then press enter to run it.

How can I do this?

share|improve this question
add comment

2 Answers

That's how mappings work: the replacement takes place after the entire mapping is entered.

To create command aliases, try the cmdalias.vim script.

On a related note, :setl ff=unix.

share|improve this answer
add comment
up vote 3 down vote accepted

Figured it out

Apparently what I want is not a mapping at all, but a command. So, the last line should be replaced with:

command Linebreak call UseUnixLineBreaks()

Note the capital "L" - user-defined commands must begin with a capital letter. See :help command.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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