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.

Durga Swaroop
  • 1,059
  • 6
  • 17
  • 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
  • @saginaw Hey thanks for the line number tip. I have searching for it so long. And, as for the tab sequence, Imagine its like arguments in a function where you tab over to the next argument but instead in this case you tab over to a different line. I hope that made some sense. – Durga Swaroop Jan 25 '16 at 16:56
  • 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: http://vi.stackexchange.com/a/5990/4939 Otherwise, could you provide more info on your end goal? – saginaw Jan 25 '16 at 17:05
  • Let's say you know in advance you want to move on line 7, then 12, then 19. In this case, you could type this: vim +':12ma a | 19ma b | 7' file_name, it will open your file on line 7. Then to go on line 12 hit 'a, and to go on line 19 hit 'b. – saginaw Jan 25 '16 at 17:12
  • @saginaw I guess that sort of helps what i want to do. But, what i am trying here is that I have a mark down file with headers, A, B and C. Now, we open that file for the user with the cursor on line below the header A. If he decides to skip that, he should be able to go to the line below B, with just a tab. Similarly from B to C and so on.. But, the line numbers are not constant. As, if the user types something the first time, everything else goes down. – Durga Swaroop Jan 25 '16 at 17:14
  • If your headers begin with the character # you could define the following mapping: nnoremap <silent> <Tab> /^#/+1<CR> Hitting <Tab> should move your cursor below the next header. You can also make the mapping local to the buffer by adding the argument <buffer> if you want <Tab> to have different behaviors depending on the filetype. – saginaw Jan 25 '16 at 17:26
  • @saginaw That was my first instinct to do. But, this vim will be opened and handled by a shell script. So, setting all the mappings before hand is not exactly possible. But, i guess i can put it in a file and source it before opening vim. – Durga Swaroop Jan 25 '16 at 17:30
  • Your shell script can source any file (which can contain any Ex command including :nnoremap) when it opens a file: vim +'so mappings.vim' file_name – saginaw Jan 25 '16 at 17:48
  • 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 01 '16 at 04:44
  • 1
    @dash-tom-bang this is good. I didn't know that there is a cursor method. Thank you. – Durga Swaroop Mar 01 '16 at 05:37

1 Answers1

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)
John O'M.
  • 8,552
  • 2
  • 42
  • 58