I have a to do list that I manage in vim. I want to keep track of things I remove from the list.

After I type dd to delete a line, I want to write this line to a different file.

I only want this behavior when I am in my todo list, not other files. Any suggestions on how to do this?



You probably should map dd to

  1. append current line to some file
  2. do normal dd aftewards
nnoremap dd :.write! >> ~/different_file<CR>dd

write command can append a range to a specified file if >> is supplied. Here . is the range of "current line".

To make it only available for a specified buffer you should create a mapping with <buffer> argument (:h :map-local):

nnoremap <buffer> dd :.write! >> ~/different_file<CR>dd

To properly apply this mapping to a specific buffer you will have to use autocommand:

augroup TODO | au!
    au BufRead,BufNew todo.txt nnoremap <buffer> dd :.write! >> ~/different_file<CR>dd
augroup END

Here you create an autogroup, clear it (needed in case you re-source your vimrc) and add autocommand for 2 events: BufRead and BufNew for a buffer named todo.txt.

Now if you open or create a buffer named todo.txt buffer local mapping would be added only available in this buffer.

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.