vi commandline, goto line and column

14,342

Solution 1

You can use:

vi '+normal 15G25|'  myfile.xml

Solution 2

With the file-line plugin, you can simply append line and column to the file name, both when invoking Vim and inside with commands like :edit:

vim myfile.xml:15:25

Solution 3

just for fun there is -c 'normal 14j24l' which will put you on line 15 character 25

Solution 4

I don't know about the -c option, but you should be able to do:

vi '+cal cursor(15,25)' myfile.xml

(make sure you quote because of the parenthesis () )

Solution 5

Worth noting:

-c command 

Run the given ex command upon startup. Only one -c option is permitted for vi; Vim accepts up to 10. An older form of this option, +command, is still supported.

The +command has the same single ex command limitation when using vi.

The +/pattern is also limited to one command with vi.

( taken from vim and ex editor )

Share:
14,342

Related videos on Youtube

Dominique
Author by

Dominique

Updated on September 18, 2022

Comments

  • Dominique
    Dominique over 1 year

    I have been mixing the use of emacs and vi (vim) for a long time. Each of them has its advantage. I parse error output from a compilation like process and get a line and column number but I can only use emacs to directly go to a line and column:

    emacs +15:25 myfile.xml
    

    with vi I only have the line number (according to the man page)

    vi +15 myfile.xml
    

    There is an option to go position cursor on a pattern (vi +/pattern myfile.xml) which i never got to work. But that would not help me as the pattern is not always the first occurrence in the file.

    How can I start vi so it goes to column 25 on line 15 of my file? Can I do something with -c option?

    • Admin
      Admin over 9 years
      Since you tagged vim, have you considered the quickfix system, which runs the compiler from within vim, parses the error output, populates a list of errors (navigate with :cp, :cn, :cl commands), and positions your cursor at the first one?
    • Admin
      Admin over 9 years
      If you open a file in vi, and type :15 it will jump to line 15.. Hope this helps you
  • Ingo Karkat
    Ingo Karkat over 9 years
    cursor() uses byte index for the column, but it's usually a character count.
  • Dominique
    Dominique over 9 years
    Good to know this exist, but I think I will use a answer that doesn't require extra plugin
  • Dominique
    Dominique over 9 years
    @IngoKarkat Does that really matter? Text is UTF-8.
  • Ingo Karkat
    Ingo Karkat over 9 years
    It matters for non-ASCII characters, which are represented by more than one byte. Also, a Tab and control characters usually occupy more than one display column.
  • Izkata
    Izkata over 9 years
    @Dominique You can also vim myfile.xml +15 for row without a plugin, but not column as far as I know
  • Olivier Dulac
    Olivier Dulac over 9 years
    explanation: most commands (ie, most letter) in vi can be preceded by a number (or range) to change its default behaviour or to repeat it (or apply it on a specific range). Inside vi, in command mode, you can do 15G to go to line 15 and 25| to go to col 25. And Christa shows how to do the same from the command-line.