0

What I am asking about can be done using multiple ways but none of them fully achieves what I want. For an instance :

  • 0vg_ selects the current line without the new line character at the end but changes the cursor position
  • vV or V enters Linewise Visual Mode and selects the current line without moving the cursor position but includes the new line character within the selection

I am not sure if what's mentioned here in the documentation about inclusive/exclusive might help or not, I couldn't really come up with a solution from it (maybe I am missing something) but here is the link anyways : https://vimdoc.sourceforge.net/htmldoc/motion.html#exclusive

So is there a method to achieve what I am looking for? I am planning to create a keybinding in my vimrc for this, so it is ok if the solution contains vimscript or requires a certain plugin (but I prefer it to be simple without the need for plugins)

2
  • Please edit and include more details on what you want to achieve. Both cursor movement and newlines are in the nature of a text editor. No way to avoid them. I think it will be possible to find a solution to achieve your goal with them.
    – Friedrich
    Commented May 7 at 4:08
  • What about nnoremap Y y$?
    – Barzi2001
    Commented May 9 at 6:07

2 Answers 2

1

As mentioned in another answer, visually selecting text generally moves the cursor to one end of the selected area, because moving the cursor is the mechanism by which the boundaries of the visual area are defined: you start your selection at point A and you end it at point B.

The only exception is visual-line mode, which, because of its fixed, implicit nature, doesn't require cursor motion.

The problem, now, is that there is no built-in way to force visual-line mode to exclude the newline character. The main consequence of this, which may or may not be a deal breaker, is that you will have to resort to regular visual mode… which will always move the cursor.

So we have a bit of a catch-22, here:

  • Not moving the cursor is only possible in visual-line mode, which can't be made to exclude the newline.
  • Excluding the newline is only possible in regular visual mode, which always moves the cursor.

Whether this problem can be solved or not depends largely on context, which is largely missing from your question. If we knew: a) what you intend to do with that visual selection, and b) why not moving the cursor is an important part of it, then we may be able to form a more definitive answer.

For example, "doing X without moving the cursor" often actually means "doing X and then restoring the cursor location", which is a different problem entirely.

0

In visual and visual block mode, your cursor is always at one corner of the selection; in visual line mode, the end of line is always included. So what you want is not supported.

But perhaps you want to implement a motion for an operator or something, in which case you might be able to do something like what you want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.