Wayne Werner
Wayne Werner

Reputation: 51807

Is it possible to improve this vim mapping that repeats a command on the command line?

So here is my mapping:

nnoremap <cr> :nnoremap <lt>cr> :w!<lt>cr>:!tmux send-keys -t :1.1 "py.test --cov=." C-m <lt>cr><lt>cr><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left>

It's pretty awesome. What it does is when I first start my vim session (or after reloading a vimrc), and hit enter, I can immediately type the name of the tmux session that I'm in and hit enter again. Assuming that I'm editing my files in the zeroth pane of window 1 and I have a tmux split (horizontal, in my typical case). Subsequent times hitting enter in normal mode will save my active file and launch my py.test tests in another window. That means that I can technically continue coding before my tests have finished passing. I forget who I first got the idea from, but h/t that guy.

Anyhow, you've no doubt noticed that there are a lot of <left>'s in that mapping, because I'd like to start the command out being able to set the session name. But there also might be cases where I'm editing my code in a different window or something, or I need to change my pytest command or something, so I still want the ability to make those modifications.

Is there a way that I can improve this mapping? Maybe by approaching it in a different way altogether?

Upvotes: 3

Views: 55

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172600

Your initial mapping builds another mapping command, and uses <Left> to insert the cursor on the right position to complete and then execute it.

An alternative to that would be defining a custom command (e.g. :TestInSession) that takes the variable parts as argument(s). Then, your initial mapping could just build the other mapping, leaving the cursor at the end of :TestInSession, and you would have less clutter in the command-line, and easier editing at the end.

If you need the ability to re-configure your other mapping, you could define an alternative initial mapping that isn't overwritten, e.g.:

:nnoremap <Leader><cr> :nnoremap <lt>cr> ...

Upvotes: 1

Related Questions