lockdoc
lockdoc

Reputation: 1699

Vim: Select all without scrolling away

is it possible in vim to select all lines in the current file, but leave the position where my cursor is unchanged?

Let's say I am currently at line 500 (of 3000) and want to quickly select everything (not yank), as my selection is simply set up to show whitespace characters. Can this be done without leaving my current line?

Upvotes: 2

Views: 1853

Answers (4)

K.H.A.
K.H.A.

Reputation: 376

It is not possible to have the cursor inside a visual selection. This caused by that, vim defines visual selection through two marks. As soon as you move the cursor one of the marks gets updated. Basically this means one of the marks is always lays where the cursor is(at least when using "v" to select). You cannot have the border in the middle of the region that the border defines :)

Upvotes: 0

Mathias Begert
Mathias Begert

Reputation: 2480

To achieve exactly what you like, you can press the following:

ggVG<Esc><Ctrl-O><Ctrl-O>
  • gg moves to the beginning of the file
  • V starts visual line mode
  • G moves to the and of the file (now you have selected the whole file)
  • <Esc> leaves visual mode
  • <Ctrl-O> moves your cursor back to the prevois location (first to the beginning of the file, then the second time to your last position before pressing gg)

And if you like to select only the visible lines in you window (to not scroll away). You can use HVL instead of ggVG (H moves to the top of your window and L to the bottom).

You also could show whitespaces without using visual selection with something like this in your .vimrc:

set list listchars=tab:»·,trail:·,nbsp:· 

This helps me to detect trailing whitespaces, and mixed (spaces/tabs) indentation.

Upvotes: 2

Xavier T.
Xavier T.

Reputation: 42268

Depending on what you are trying to achieve you can use something like :

%cmd

To apply the command to the whole file.

For example, %y will yank the whole file, %=will format the whole file, without moving your cursor. It does not really work if you do something like %d...

It is not a real selection though but rather a way to apply a command on the whole file.

To go further you can use something like

%norm Atest

To add 'test' at the end of each line. (Actually this is a bad example, because this command will move to the last line...)

Upvotes: 0

imbichie
imbichie

Reputation: 1616

usually pressing

ggVG

in normal mode will select all the lines, but it will leave your cursor at the last line of the file. If you wants to highlights the whitespace characters then you can highlight this by using the below command in command mode (this white color chosen is for dark theme)

: hi ExtraWhitespace ctermbg=White guibg=White

Upvotes: 0

Related Questions