0

So I'm not sure how to go about running some some code in my mappings, like:

nnoremap <Return> :execute "normal! if 1 echo('one') endif"<cr>

Also tried it without the 'normal' - tried different combinations with separating the commands via '\' and '|' but nothing worked - keep getting variable not defined errors.

Any idea how?


EDIT:

So here's what I'm actually doing:

" Quickly toggle between insert/normal modes
nnoremap <A-e> i
inoremap <silent><A-e> <esc>:call GoRightIfNotBOL()<cr>

" Returns 1 if the cursor is at the beginning of a line "
function! IsBOL()
    return col('.') == 1
endfu

function! GoRightIfNotBOL()
    if !IsBOL()
        execute "normal l"
    endif
endfu

So instead of calling GoRightIfNotBOL I thought I could inline its code cause really, I can't think of another location where I would be using this function, and it's pretty small.

2
  • 1
    where did you get the logo? arch + vim? does it have a blue version? Commented Aug 22, 2014 at 13:13
  • Haha, it's pretty sick I know. Found it at google images. It was originally green and white transparent on the back. Here's a blue one for you imgur.com/DjMLYuB and here's the original xyne.archlinux.ca/img/arch_linux_programming_language_logos/… Commented Aug 22, 2014 at 13:20

1 Answer 1

1

you are looking for <expr> mapping

read :h <expr> there you'll find examples.

If your codes were a bit long, put them in a function, and call that function in your mapping. It is more readable if you later want to do some change on it.

An example with inoremap <expr>:

inoremap <expr> <YourKeys> "<esc>".(col('.')>1?'l':'')
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks. The code was actually in a function. But the code is not long so I thought making a function for it wouldn't be so necessary, don't want to clutter my vimrc with tiny functions.
making functions is not bad idea, no matter in vimrc or somewhere else. functions are more readable, easy to debug (compare to one-liner), reusable. However, I don't know your exact requirement, maybe your goal could be easily done with a short one-liner.
I'm still not sure how to do it :/ - the examples in the doc are mapping functions, not one-liners like mine. Not sure how to project my if 1 echo(..) endif to them. Could you post some code?
@vexe what do you want to do if you pressed <Enter> in normal mode?
Well It's not actually enter, it's <A-e> but I was just using Enter to quickly see if the map is working or not. Basically, I use <A-e> to quickly toggle between insert/normal modes. There's a small piece of code that I run when I exit insert mode to correctly position the caret if it was at the beginning of a line. I will add it to my question.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.