3

I am working on a project that could really benefit if I can open vim by specifying a line number/ match so that the file would open with the cursor positioned on that line. Is there a way to do that with a command line argument? (because i am opening a file as vim file_name)

Also, is there a way to specify the tab sequence as well? So, after typing some stuff on line 2, Hit tab and go to line 7 , something to that effect. Thanks in advance.

PS: I am sorry if this is already asked before. I searched a lot but i could not find anything.

  • 4
    If you type vim +7 file_name or vim -c7 file_name, it will open file_name and position the cursor on line 7. If you type vim +'/pattern' file_name or vim -c '/pattern' file_name it will open file_name and position the cursor on the first line where pattern is found. From there if you hit n, you will reach the other matches. However, I did not understand your second question concerning the tab sequence and Tab key, sorry. – saginaw Jan 25 '16 at 16:44
  • 1
    Do you mean you want to move across a predefined set of lines? Like for example, you know in advance you want to be on line 7, then move to line 12, then 19 ? If so, I may be wrong but it seems you would benefit from scripting your editions. For example, you could write all your editions in a file, let's say myedits.vim, where each line is an Ex command, and then from the shell type vim +'so myedits.vim' file_name. If it's what you want, you could be interested in this: vi.stackexchange.com/a/5990/4939 Otherwise, could you provide more info on your end goal? – saginaw Jan 25 '16 at 17:05
  • 1
    You might be able to do this with syntax folding and ]z? Or by putting the sections into the quickfix list or the location list with vimgrep and mapping tab to :cn or :ln – Steve Jan 25 '16 at 18:07
  • 1
    It might also be worth noting that you can call functions from the command line, in case you want to jump to a specific column and row you can just do vim "+call cursor(8, 16)" myfile.txt to jump to line 8, column 16. (I use this when launching Vim from other editors/debuggers.) – dash-tom-bang Mar 1 '16 at 4:44
  • 1
    @dash-tom-bang this is good. I didn't know that there is a cursor method. Thank you. – Durga Swaroop Mar 1 '16 at 5:37
1

Ordinarily, tab (a.k.a. ctrl+i) and ctrl+o traverse the jump list.

Vim supports adding entries to the jump list by setting the ' mark. As such, for your line numbers case, you could set up a jump list with the following:

vim +"12ma '|20ma '|56ma '" file_name

But, then Vim still starts on the file like normal, and at the end of the jumps list. As such, you can use ctrl+o to move back through the list (starting at line 56, then 20, then 12) and tab to go back forward through the list.

As such, it might make sense to construct your list in reverse, so that your first ctrl+o gets you to the first entry you want.

Downsides:

  • Jump operations (described in :help jump-motions) will move you to the end of the jumps list and add another entry, making it a bit interesting to then navigate to your saved entries.
  • The backwards/forwards nature of this can be confusing

I tried doing stuff to start the session at the beginning of the set of jumps, with stuff like

vim +"12ma '|20ma '|56ma '|norm 3^O" file_name

but they don't work unless I prevent my vimrc from running. I think I must have a plugin or something trying to be "nice" by moving my cursor around (probably that common snippet that places your cursor where you left it last on BufReadPost), so the following actually works, so long as you don't jump before you want to use tab to get to the next line:

vim -u None +"12ma '|20ma '|56ma '|norm 3^O" file_name

where the 3 is however many items you put on the jump list and ^O is a literal control+o (typed like ctrl+vcrtl+o).

Downsides:

  • Harder to type (but if it is constructed by a program, probably not bad)
  • Might not be able to use your default vimrc (though, again, if you are running via a program, this is probably an advantage)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.