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?
If we are in the vim editor, then simply do this, “Press the ENTER key, write the Line number, and press Shift+ g”: Again the output is the same.
If you're already in vi, you can use the goto command. To do this, press Esc , type the line number, and then press Shift-g . If you press Esc and then Shift-g without specifying a line number, it will take you to the last line in the file.
You can type f<character> to put the cursor on the next character and F<character> for the previous one. You can also use ; to repeat the operation and use , to repeat it in opposite direction.
In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up.
: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.
Other vim tips: in command mode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With