2

I have this in my vimrc:

" Export text under the cursor
noremap <leader>e :w! >><Space>

This allows me to select text using visual mode, press leader e, type a file name, and append the text under the cursor to that file.

I would like to also have the text under the cursor be deleted from the current file as part of this command, but I don't know how to accomplish that.

2

It seems you are using this mapping line-wise by selecting lines with V before executing <leader>e. This is worth a command in my opinion.

command! -range=% -nargs=1 -complete=file MoveTo
        \ <line1>,<line2>write! >> <args> | <line1>,<line2>d_
  • -range=% to allow a range, if none, consider the whole buffer
  • -nargs=1 -complete=file accept one argument and allow file completion for it
  • MoveTo name of the command...
  • <line1>,<line2>write! >> <args> append lines from the range to the given argument
  • | <line1>,<line2>d_ then delete the lines in the black hole register so that they aren't copied in ""

Then

xnoremap <leader>e :MoveTo<Space>
nnoremap <leader>e :MoveTo<Space>

:h user-commands, :h quote_

  • Thanks! This as even better functionality that I anticipated and I appreciate your explanation. I have been using ed recently and I have been interested in learning ex mode to replace it. – jsb Aug 8 at 1:11
  • Note that I added the =% to -range to be consistent with your original mapping, but a maybe more intuitive definition could be simply -range to apply on the current line by default in normal mode. – perelo Aug 8 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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