How to Open Vim with a Particular Line Number at the Top

How can I open vim with a particular line number at the top?

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

How to open a file and anchor to dedicated line number with vim

Have a look at the file:line - Allows you to open file:line and it does the right thing plugin; it sets up autocmds to handle those, so you can pass your path/to/file:lnum directly to Vim on the command line and :edit such, too!

VIM: how to go to exact line on Ubuntu


:150

will take you to line 150 in vi

:1500

will take you to line 1500 in vi

As per the comments you may want to try

150G

to get to line 150.
which is less key strokes then
:150Enter
if you aren't sure what line you are on try

 :set nu!

notice the :

if you want to always see the line consider editing your vim profile. Most often

vi ~/.vimrc

and add

:set nu! 

and write and quit

:wq
#or you could use :x

this can be done outside of vi. For example, if I want to delete line 5000 in a text file I could use a scripting language. For example, using sed it would be the following

sed -i '5000d;' inputFile.txt

to delete line 10 to 20 it would be

sed -i '10,20d;' inputFile.txt

notice the -i will edit the file in place. Without the -i it will goto stdout. Try it. you can redirect stdout to a file

sed '5001,$d;' inputFile.txt >> appenedFile.txt

this might have a lot going on here for you. this deletes line 5001 to $. With $ being the end of the file. >> will append to a file. where as > creates a new file.

if you are curious how many lines are in a file you may want to type wc -l inputFile.txt

some of this may seem awfully trivial, but if you are trying to edit a file with 50,000 lines it may take vi a sweet minute to open and traverse. where if you know you just want to delete the last line you could use sed and do it in a fraction of the time.

sed can also search and replace inside a file as well. But perhaps awk, perl, or python might also be a viable solution.

but overall, you may wan to find a good tutorial on vi. thousands exist. I'd consult google. Perhaps find yourself a VIM Cheatsheat.

Open a buffer from within vim and jump to a specific line in one command

You can simply do :e +123 myFile.txt. See :help :e and :help +cmd for details.

vim line numbers - how to have them on by default?

Add set number to your .vimrc file in your home directory.

If the .vimrc file is not in your home directory create one with
vim .vimrc and add the commands you want at open.

Here's a site that explains the vimrc and how to use it.

Open specific number of files in Vim with specific pattern from command line

You can put those configuration in a customized script file, then optionally use that script file to open your workspace configuration.

Script file:

:bl
:bf
:tabe
:n
:split
:vsplit
:wincmd l
:n
:wincmd j
:n
:n
:vsplit
:wincmd l
:n
:wincmd h
:wincmd k
:tabn

And you can call it as such:

vim -S script.vim file1.txt file2.txt file3.txt file4.txt file5.txt

This will open vim on file1.txt, with an open tab next to it, according to your configuration, and put the cursor on the upper left file (file2.txt) when you switch to it.

Added explanation of each line:

Note that when we split, the cursor will stay on the original window, and that each window will show different files, which can be navigated independently using :n and :N. When a new window is created, it will display the same file as the window we're in when we created the window.

As noted in my comment, the first two lines is to tell VIM that we have read every file, so VIM won't complain "4 more files to edit" at the end of the session.

:bl       Go to last file
:bf Go to first file
:tabe Create new tab
:n Open next file (file2.txt)
:split Split horizontally (will create new window below with the content of file2.txt)
:vsplit Split vertically (will create new window on the top right with the content of file2.txt)
:wincmd l Go to the window to the right (which currently displays file2.txt)
:n Open next file (file3.txt)
:wincmd j Go to window at the bottom
:n Open next file (file3.txt, because bottom window was displaying file2.txt)
:n Open next file (file4.txt)
:vsplit Split vertically (will create new window on the right with content file4.txt)
:wincmd l Go to window to the right (which is bottom right)
:n Open next file (file5.txt, because bottom-right window was displaying file4.txt)
:wincmd h Go to window to the left
:wincmd k Go to window above (this sets the cursor on file2.txt on top left)
:tabn Go to next tab (the first one, displaying file1.txt)

Go to first line in a file in vim?

In command mode (press Esc if you are not sure) you can use:

  • gg,
  • :1,
  • 1G,
  • or 1gg.

Search and replace in vim in specific lines

Vim has special regular expression atoms that match in certain lines, columns, etc.; you can use them (possibly in addition to the range) to limit the matches:

:5,12s/\(\%5l\|\%12l\)foo/bar/g

See :help /\%l



Related Topics



Leave a reply



Submit