Skip to main content
deleted 1 character in body
Source Link
doraemon
  • 1.7k
  • 12
  • 27

I usesuse neovim 0.3.1 only. I define the following function and map to do a test for getpos in visual mode:

function! show_position()
  echo getpos("'<") getpos("'>") getpos(".") getpos("v") getcurpos()
endfunction
vmap ;j :<c-u>call show_position()<cr>gv

The test file contains (please ignore : and | for now)

0123456789
11234:56789
2123456789
3123456789
4123456789
51234|56789
6123456789
7123456789

I then enter visual mode using v, V or <c-v>, move around and then press ;j to get the position of various markers. I have two cases tested (we can press o in visual mode to switch the case):

  1. When : is the place I started the visual selection and | is the current cursor place,
  2. When | is the place I started the visual selection and : is the current cursor place,

In both cases, the result shows that getcurpos() == getpos(".") == getpos("v") == upper left corner of the selection, which I feel contradicts with common sense as well as the doc :h line

      .   the cursor position
      $   the last line in the current buffer
      'x  position of mark x (if the mark is not set, 0 is
          returned)
      v   In Visual mode: the start of the Visual area (the
          cursor is the end).  When not in Visual mode
          returns the cursor position.  Differs from |'<| in
          that it's updated right away.

How to get the current cursor position in visual mode?

I uses neovim 0.3.1 only. I define the following function and map to do a test for getpos in visual mode:

function! show_position()
  echo getpos("'<") getpos("'>") getpos(".") getpos("v") getcurpos()
endfunction
vmap ;j :<c-u>call show_position()<cr>gv

The test file contains (please ignore : and | for now)

0123456789
11234:56789
2123456789
3123456789
4123456789
51234|56789
6123456789
7123456789

I then enter visual mode using v, V or <c-v>, move around and then press ;j to get the position of various markers. I have two cases tested (we can press o in visual mode to switch the case):

  1. When : is the place I started the visual selection and | is the current cursor place,
  2. When | is the place I started the visual selection and : is the current cursor place,

In both cases, the result shows that getcurpos() == getpos(".") == getpos("v") == upper left corner of the selection, which I feel contradicts with common sense as well as the doc :h line

      .   the cursor position
      $   the last line in the current buffer
      'x  position of mark x (if the mark is not set, 0 is
          returned)
      v   In Visual mode: the start of the Visual area (the
          cursor is the end).  When not in Visual mode
          returns the cursor position.  Differs from |'<| in
          that it's updated right away.

How to get the current cursor position in visual mode?

I use neovim 0.3.1 only. I define the following function and map to do a test for getpos in visual mode:

function! show_position()
  echo getpos("'<") getpos("'>") getpos(".") getpos("v") getcurpos()
endfunction
vmap ;j :<c-u>call show_position()<cr>gv

The test file contains (please ignore : and | for now)

0123456789
11234:56789
2123456789
3123456789
4123456789
51234|56789
6123456789
7123456789

I then enter visual mode using v, V or <c-v>, move around and then press ;j to get the position of various markers. I have two cases tested (we can press o in visual mode to switch the case):

  1. When : is the place I started the visual selection and | is the current cursor place,
  2. When | is the place I started the visual selection and : is the current cursor place,

In both cases, the result shows that getcurpos() == getpos(".") == getpos("v") == upper left corner of the selection, which I feel contradicts with common sense as well as the doc :h line

      .   the cursor position
      $   the last line in the current buffer
      'x  position of mark x (if the mark is not set, 0 is
          returned)
      v   In Visual mode: the start of the Visual area (the
          cursor is the end).  When not in Visual mode
          returns the cursor position.  Differs from |'<| in
          that it's updated right away.

How to get the current cursor position in visual mode?

Source Link
doraemon
  • 1.7k
  • 12
  • 27

getpos(".") in visual mode equals getpos("v")

I uses neovim 0.3.1 only. I define the following function and map to do a test for getpos in visual mode:

function! show_position()
  echo getpos("'<") getpos("'>") getpos(".") getpos("v") getcurpos()
endfunction
vmap ;j :<c-u>call show_position()<cr>gv

The test file contains (please ignore : and | for now)

0123456789
11234:56789
2123456789
3123456789
4123456789
51234|56789
6123456789
7123456789

I then enter visual mode using v, V or <c-v>, move around and then press ;j to get the position of various markers. I have two cases tested (we can press o in visual mode to switch the case):

  1. When : is the place I started the visual selection and | is the current cursor place,
  2. When | is the place I started the visual selection and : is the current cursor place,

In both cases, the result shows that getcurpos() == getpos(".") == getpos("v") == upper left corner of the selection, which I feel contradicts with common sense as well as the doc :h line

      .   the cursor position
      $   the last line in the current buffer
      'x  position of mark x (if the mark is not set, 0 is
          returned)
      v   In Visual mode: the start of the Visual area (the
          cursor is the end).  When not in Visual mode
          returns the cursor position.  Differs from |'<| in
          that it's updated right away.

How to get the current cursor position in visual mode?