Search code examples
vim

How to "select all" in vim without losing cursor position?


My mswin.vim contains the following command for Ctrl-A

inoremap <C-A> <C-O>gg<C-O>gH<C-O>G

This command correctly selects all lines in the file but also makes the cursor jump to the end of the file (undesired side effect). Is there a way to "select all" without moving the cursor?

Thanks in advance for any help or suggestion.


Solution

  • I am afraid that this cannot be achieved. Since in visual selection mode, the cursor will be either at the beginning or end of the visual range. Any motion in visual mode will lead to the visual selected range changes ( depends on the visual mode: char/line/block-wise).

    The point is, what do you want to do after select-all and kept the cursor position? It may be a X->Y problem, just tell us what is your final goal. There could be better solution for it.

    Btw, C-A is so useful to increment numbers, maybe you want to reconsider the map trigger?