Move to specific line and column in one command

245 Views Asked by At

Let's say we have a text file that is very long and has many lines. I want to move to 30th line and 15th column in this file. Are there any VIM commands that could be used to move to mentioned line number and column in one command? Thanks.

Please, do not suggest to use smt like :30 command and after 15| this is NOT an option. May VIM has an option to input smt like: :30,15, just in another syntax?

The only option I found is to use :call cursor(30,15) but it looks little bit too long, as I need to type it each time I want to jump to the another position.

2

There are 2 best solutions below

1
romainl On

Well, you want to move across two dimensions but Vim commands are limited to one dimension, therefore you can't move to line 30 and column 15 with a single built-in command.

Here are the shortest key sequences to move to column 15 of line 30:

" in normal mode
30G15|

" in command-line mode and normal mode
:30<CR>15|<CR>

" in command-line mode
:30norm15|<CR>

" from the shell
$ vim foo.txt +norm30G15\|<CR>

Note that the first suggestion above is already the shortest theoretically possible sequence because you would need:

  1. at least one key for the command itself,
  2. at least one key to separate the two coordinates,
  3. the vertical coordinate (can't be shortened),
  4. and the horizontal coordinate (can't be shortened).

The only improvement I can think of to 30G15| would be to use keys that don't require a modifier but, frankly, I am not sure that it is worth the hassle.

3
phd On

You can define your own command-line command. Define it in ~/.vimrc and it will be available everywhere. Define:

:command -nargs=* Go call cursor(<f-args>)

Run:

:Go 30 15