Lindsay Winkler
Lindsay Winkler

Reputation: 761

How to script cursor movements in vim?

I'd like to set up a mapping to make it easy for me to send lines to the system clipboard in vim.

I'd normally specify a range, and write that range to pbcopy (I'm on OSX, where pbcopy "pastes to the pasteboard"), like this:

:20,23w !pbcopy

To make this more convenient, I'd like to set up a mapping to fill in the bulk of this command, but then return to the start of the line to allow me to fill in the range I want.

Essentially this:

nnoremap <leader>g :w !pbcopy

but that leaves the cursor at the end of the line. Is there any way I can create a mapping that adds the text and leaves the cursor at the start of the line for range insertion?

Upvotes: 0

Views: 75

Answers (1)

Bambu
Bambu

Reputation: 548

In order to go to the begining of the so that you can enter a distance you could try the following mapping:

nnoremap <leader>g :w !pbcopy<c-b>

<c-b> will bring the cursor to the beginning of the line in order for you to enter a distance.

alternatively you could select your text visually and have the following mapping in order to copy your selection and restore your cursor to where it was in the beginning:

vnoremap <leader>g :w !pbcopy<cr>`[

`[ could be replaced with gv to re-select the lines

Upvotes: 1

Related Questions