VIM: how to go to exact line on Ubuntu

kevin picture kevin · Jun 17, 2011 · Viewed 57.8k times · Source

I'm using vi on Ubuntu 12.10. Some files are quite long so when I want to go to the middle of the file, I have to page down or scroll down.

Is there a VIM shortcut to go to an exact line number?

Answer

matchew picture matchew · Jun 17, 2011
: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.