2

The documentation for :put says that the command accepts a line. That is, it puts the content after the line specified. Is it possible to put the content at a particular mark?

For example, say I am in the middle of refactoring an anonymous JavaScript function into a named JavaScript function. I have the following in-process code (mid-refactoring), and I want to move the anonymous function to before the semi-colon on line 2.

function createClickListener(element) {
    return ;
}

var listener = createClickListener(element);
element.addEventListener('click', function() {
    alert("You clicked the element!");
}, false);

In normal mode, I can place my cursor at the beginning of f in the anonymous function, type "fc/}/e (followed by the <enter> key) to cut the anonymous function into the named register f and enter insert mode, and then type listener.

However, in order to put the code on line 2, I have to move the cursor to the semi-colon then type "fp. If I were to do this with the :put command (e.g., :2put f) the anonymous function would be placed after line 2.

Is it possible to use the :put command to put content mid-line or at a mark? Or is there an alternative (or better) way of doing this (I guess I could jump using marks more...)?

1

No, it's not. You'll have to use other means:

  • go to the right position and use p or P normal mode commands thanks to :normal -> :normal! P for instance. You may need first to put your variable into a register.
  • you could insert the new thing at cursor position also thanks to :exe "normal! i".the_string. But you may have to fight against automatic indentation.
  • obtain the line with getline(line_nr) do your computations and write it back with :call setline(line_nr, new_line). But that'll be extremely tricky to use if you want to move several lines, and if you have multi-bytes characters.
  • use :substitute along with column specifications -> :2s/\%42c./\=a_string_variable.submatch(0)/. See :h /\%c and :h :s\=. I suspect, you'll have to reindent what have been modified afterwards as well.

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.