N.B. my question title might be poor, so feel free to improve it.
Background
I want to write a command to move my cursor to the body of my email, add blank lines, and change to insert mode. Previously, I used the following for new emails.
nnoremap gb <C-Home>/^$<CR>:nohlsearch<CR>o
mutt
would create a new email in vim
, then I'd press gb
. The cursor would move to the top of the email (<C-Home>
), find the first blank line and immediately stop highlighting (/^$<CR>:nohlsearch<CR>
), create a new line and start editing a new line (o
).
However, I often reply to an email, and I wanted to automatically create another "buffer" empty line below my cursor position. Hence, I changed the command to this following.
nnoremap gb <C-Home>/^$<CR>:nohlsearch<CR>o<Esc>ko
This would do as above, then get into normal mode <Esc>
, move up (k
), and create another new line to edit o
.
Conditional
I wanted to create a conditional. After searching for the first blank line (<C-Home>/^$<CR>:nohlsearch<CR>
), if this were the last line of the email, I'd be editing a new email, so just o
. Otherwise, I'd be replying, so o<Esc>ko
. The following doesn't work, because I've mangled together different syntaxes, but it should give an idea of what I want.
nnoremap gb <C-Home>/^$<CR>:nohlsearch<CR>|if line('.') != line('$')|o<Esc>k|endif|o
I suspect nnoremap <expr> gb ...
is useful, but I'm not sure how to use it.
nnoremap gb :call YourFunction()<CR>
. In the function you can do all your mapping is currently doing and have a more nicer view.remap
,remap <expr>
,function
andcommand
. I'm not sure what I'm writing where. I'm not sure exactly what syntax to use for this function, as obviously my last piece of code is a hybrid of different things (and doesn't work as is). Could you possibly write an answer with the function code itself?:
(example:echo "foo"
). A function is a set of commands that you define in your.vimrc
or another.vim
file, you can call the function from the command line mode:call YourFunction()
. Finally a mapping is the fact to assign an action to a key or a key sequence (e.g.:map <C-a> :call YourFunction()<CR>
says to Vim "When I press Ctrl-a do as if I had press:
followed bycall YourFunction
followed byEnter
).gg
is a normal mode mapping so if you want to use it in a function you have to say to vim "Use gg as if I was in normal mode" you do that withnormal gg
. In the command line using:normal gg
will be the same as typinggg
in normal mode. Actually the best I can advise to you is to learnVimScriptTheHardWay it might be a lot of reading but you'll find an answer to 98% of your questions there and you can ask the 2% remaining on this site ;-)