Monday, November 15, 2010

Vi open file at required line number


Sometime we are required to open a file in vim and then need to go to a particular line number in order to check something or to edit something. This is how we can open a file in vim and directly put the cursor on the required line of our interest.

Way1:
Using vim -c option:

From VIM(1) man page:

-c {command}
{command} will be executed after the first file has been read. {command} is interpreted as an
Ex command. If the {command} contains spaces it must be enclosed in double quotes (this
depends on the shell that is used). Example: Vim "+set si" main.c

Following command will open file 'run_parallel_qs.py' in vim with the cursor on line number 23.

$ vim -c 23 run_parallel_qs.py


Way2:
One can use the + to specify the cursor starting point.

$ vim run_parallel_qs.py +23

which is same as

$ vim +23 run_parallel_qs.py

Without line number, i.e. the following command will open the file in vim and will put the cursor on last line

$ vim run_parallel_queries.py +

And as we know just opening a file in vi/vim, the cursor will be on line number one.

Related UNIX vim editor tips:
- Open file under cursor in Vi Editor
- A short tutorial on using tab window in Vi editor
- Pull word under cursor in Vim
- Start a new shell from Vi editor
- How to edit remote file using ssh in Vi editor

No comments:

© Jadu Saikia www.UNIXCL.com