74

I know it is possible to have vim open a file at particular line number using this syntax:

vim +500 filename

However, I would like it to open with the specified line at the top of the editor window, not in the middle.

stinkypyper
  • 2,008
  • 4
  • 22
  • 29

3 Answers3

44

You could issue zt after opening the file, so something like: vim +500 filename -c 'normal zt'.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • 1
    Thank for the answer. This one worked best, as the above answer required the user to hit enter once after vim opened. This opened smoothly to the specified line. – stinkypyper May 01 '12 at 17:54
  • I thought vim optimized the keyboard layout. This is more complicated then it's worth. – Philip Rego Oct 13 '21 at 16:39
23

As an alternative (because I ended up here searching for it), if you want to have the syntax vim <filename>:<linenumber>, eg vim test.rb:45, you can use vim-fetch with Vundle:

Plugin 'kopischke/vim-fetch'
Dorian
  • 22,759
  • 8
  • 120
  • 116
  • 1
    With [Pathogen](https://github.com/tpope/vim-pathogen): `git clone git://github.com/kopischke/vim-fetch.git ~/.vim/bundle/` – Habax Nov 23 '17 at 12:34
  • This is the way that Kate open files with line number, Nice. – Mamrezo Sep 29 '20 at 10:37
15

This should do it:

vim +'500|norm! zt' filename

Or you could just start the habit of using zt (top), zz (center), and zb (bottom); In my case, it is so automatic, I wouldn't really require the extra invocation argument

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    Thank for taking the time to answer. It is not matter of habit, I am opening vim from another app, and I wanted it to jump nicely to a particular section of specified document. – stinkypyper May 01 '12 at 17:53