How to specific the line to go to from the command line?

12 views
Skip to first unread message

Peng Yu

unread,
Mar 23, 2021, 8:46:47 PM3/23/21
to vim...@googlegroups.com
Hi,

I want to specify the line number to go to at the command line. Could
anybody let me know how to do it with vim? Thanks.

--
Regards,
Peng

Sven Guckes

unread,
Mar 23, 2021, 9:03:00 PM3/23/21
to vim...@googlegroups.com
* Peng Yu <peng...@gmail.com> [2021-03-24 01:51]:
> I want to specify the line number to go to at the command line.
> Could anybody let me know how to do it with vim? Thanks.

how to go to line #23:

jump to line 23 on startup:
vim +23 filename

jump to line 23 in command mode:
23G
23gg

jump to line 23 in command mode:
:23

turn line numbering on:
:set nu

see also:
:help G
:help gg
:help range
:help 'nu'
:help +cmd

Sven
signature.asc

Tony Mechelynck

unread,
Mar 23, 2021, 10:45:12 PM3/23/21
to vim_use
On Wed, Mar 24, 2021 at 2:02 AM Sven Guckes <guc...@guckes.net> wrote:
>
> * Peng Yu <peng...@gmail.com> [2021-03-24 01:51]:
> > I want to specify the line number to go to at the command line.
> > Could anybody let me know how to do it with vim? Thanks.
>
> how to go to line #23:
>
> jump to line 23 on startup:
> vim +23 filename
>
> jump to line 23 in command mode:
> 23G
> 23gg
>
> jump to line 23 in command mode:
> :23

Yes, and as icing on the cake, a variation on this one: how to go to a
specific line and column:

" Go to line and column
function GoTo(line, column)
exe min([line("$"), a:line]) "| normal" a:column . "|"
endfunction

see
:help [range]
" a naked range goes to the line(s) mentioned
:help bar
" to go to a certain column
:help min()
" this function takes only a List or a Dictionary as argument

>
> turn line numbering on:
> :set nu
>
> see also:
> :help G
> :help gg
> :help range
> :help 'nu'
> :help +cmd
>
> Sven
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20210324010250.7f2354bqmwsbixhl%40guckes.net.

Eli the Bearded

unread,
Mar 24, 2021, 7:36:27 PM3/24/21
to vim...@googlegroups.com
Sven Guckes <guc...@guckes.net> wrote:
> * Peng Yu <peng...@gmail.com> [2021-03-24 01:51]:
>> I want to specify the line number to go to at the command line.
>> Could anybody let me know how to do it with vim? Thanks.
>
> how to go to line #23:
>
> jump to line 23 on startup:
> vim +23 filename

That's a good answer. The more modern version is to use -c like this:

vim -c :23 filename

Up to ten -c commands can be given, each with an ex mode command. Note
it may need quotes from the shell:

vim -c ':normal 23G' filename

I have a script that I use where I consider the filename to be sensitive
and I don't want it to appear in `ps` output. To invoke vim to edit that
file from script I use a temporary tags file. One could (but probably
would find it too cumbersome) use a method like that to go to line 23.

Here's the bit of sh script I use:

case "$mode" in
# ...
edit) tmp=tags
printf "main\t%s\t1\n" "$name" > $tmp
vim -t main
rc=$?
rm $tmp
exit $rc
;;
# ...
esac

In the 'tags' file I create, the three columns are tagname, filename,
and line number. (In typical 'tags' files the third column is a search
pattern, not a line number. Typically they also have multiple lines with
separate entries.) I then invoke vim with the tag name.

I bring it up in case this isn't going to be cumbersome and might be
something that helps your use case:

vim -c ':set tags=/some/shared/tags/file' -t tagname

Elijah

Eli the Bearded

unread,
Mar 24, 2021, 7:41:06 PM3/24/21
to Tony Mechelynck, vim...@googlegroups.com

Tony Mechelynck <antoine.m...@gmail.com> wrote:
> Yes, and as icing on the cake, a variation on this one: how to go to a
> specific line and column:
>
> " Go to line and column
> function GoTo(line, column)
> exe min([line("$"), a:line]) "| normal" a:column . "|"
> endfunction

You don't need a vim function to do that. You can run normal from the
command line. Go to line 23 column 45:

vim -c ':normal 23G45|' filename

Elijah

Reply all
Reply to author
Forward
0 new messages