Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date completion #11

Open
redstreet opened this issue Dec 30, 2015 · 1 comment
Open

Date completion #11

redstreet opened this issue Dec 30, 2015 · 1 comment

Comments

@redstreet
Copy link

With vim-ledger, if I press Tab on the first column on an empty row, autocompletion enters the current date. I miss this feature, and this would be a great addition to vim-beancount if it's easy to add in.
https://github.com/ledger/vim-ledger

(Please feel free to delete this issue if this is not of interest).
Thanks!

@yegle
Copy link
Contributor

yegle commented Mar 13, 2016

Even better, I've come up with a smart solution.

The idea is: assume your transactions are ordered in chronicle order and you want to keep in that way, using the following vim function will find the right place (the last transaction whose date is <= today), place the cursor there, enter current date and leave vim in insert mode.

If you don't have transactions in a future date then it can be simplified a lot.

@nathangrigg let me know if you want to include this, I can send a PR.

function GotoInsertPosition()
    let today=strftime("%Y-%m-%d")
    let f=bufname("%")
    let cmd="grep ^20 ".f."|sort|awk '{ if($1 > \"".today."\") { printf \"%s\",lastday; found=1; exit; } lastday=$1 } END {if(!found) printf \"%s\",lastday}'"
    let lastday=system(cmd)
    let cmd="awk '/^".lastday."/,/^$/{lineno=NR} END {printf \"%d\",lineno}' ".f
    let lineno=system(cmd)
    execute ":".lineno
    execute "normal! yyppk"
    execute "normal! i".today." *"
    execute ":startinsert!"
endfunction
nnoremap <C-x> :call GotoInsertPosition()<CR>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants