How to View Line Numbers in vi

The vi vs vim debate can be just as heated among some people as the vi vs emacs debate, but regardless of where you stand there are a few situations where you might find yourself with vi and no other option. It’s a standard part of POSIX and therefore it’s included with pretty much every Unix and Linux implementation ever made in the modern era. If you’re ever troubleshooting an embedded system like a wireless router or something similar, then you’ll need to use pure vi.

A pure albeit stripped down form of vi is also included with busybox, so if you’re ever working from a limited recovery command prompt on a Linux box you might only have this as your editor. You don’t have to worry if you find yourself in any of the preceding cases because you can still view line numbers in vi and restore some of the other niceties that you’re used to if you’re a regular vim user.

Method 1: Checking Line Numbers in vi

If you’ve ever seen anyone engage in the vi vs vim debate, then you’ve probably seen someone bring the vim set ruler command as a huge benefit for vim over the original vi. Fortunately, you can view line numbers in pure vi as well. You’ll probably be at some sort of login prompt if you’re working on an embedded device. If not, then you can push Ctrl+Alt+F1 or F2 to get to where you need to go. Users of graphical versions of Linux might want to push Ctrl+Alt+T or Super+T to bring up a terminal. You can also search for the word terminal on the Ubuntu Unity Dash, but keep in mind that if you just type vi on a fully-featured version of Linux you might very well get vim instead. You can always run the command busybox vi as many Linux gurus have already pointed out.

You can generally type vi fileName, where fileName is replaced with an actual file name, to load up whatever text file you need to work with.  You might not see anything at the bottom, but you shouldn’t be in insert mode right away. In anything that’s based on the original vi, such as that used on AIX, HPUX, Solaris and some OpenIndiana machines, you can push Ctrl+G to pop up a bar that should tell you precisely where you are in the file.

Use the vi key bindings or the cursor keys if available to navigate to a new position and then push Ctrl+G again. This primitive form of ruler won’t update dynamically the same way that the one in vim would, but it should work to allow you to check line numbers fairly well in vi as it were.

If you’re on Linux rather than pure Unix and you used busybox vi to open the file, then you automatically have a very primitive ruler at the bottom without needing to push any key combination. This won’t tell you column position, but it will let you know where you are in the file and give you a percentage that tells you how much longer you have to go. This percentage should be available on other versions as well, and this makes it extremely easy to see how much longer you have to go when reading documentation. You might occasionally run into this issue if you’re installing something on a router or a headless server and don’t yet have any man pages to reference.

Method 2: Deleting a Line

If you’re used to using key commands in vim, then you already know most of the ones that you’ll need for regular orthodox vi. Push :d to delete a current line. Depending on your version, you might also be able to just type dd and you’ll see the line suddenly vanish in front of your eyes.

That being said, you also have access to an admittedly primitive form of a clipboard that many Unix programmers find much more easy to work with compared to the graphical clipboards that many other types of programs feature.

If you want to remove a line of text and yank it out so that you can put it somewhere else, which is in effect cutting it, then you can use :y or yy depending on your version. Users who are in insert mode that find these letters are making their way into the document can simply push Esc to get back to command mode first.

Once you’ve gotten the text yanked out, navigate to the new position that you want it placed at. As soon as you’re there, type p to put the text after the current position. You might want to use an upper case letter P if you were going to position the text before the current place that your cursor is at.

Please keep in mind that if you’ve gotten used to the Ctrl+C, Ctrl+X and Ctrl+V keyboard shortcuts that are common in the Windows and Macintosh environments you won’t be able to use them here. The vi editor is modal, meaning that there is a separate command and insert mode. On the other hand, if you’re having a little difficulty navigating, then position your fingers on the h, j, k and l keys.

Use h as a right arrow, l as a left arrow, j to move down a line and then k to move up when you want to select a line to delete. This might seem a little counterintuitive, but these keyboard shortcuts were made at a time when keyboard cursor keys were anything but universal. An overwhelming majority of vi users actually prefer them because they don’t require you to remove your fingers from the home row.

While deleting and moving text in this way might not necessarily be as sophisticated as it is in vim, you might very well come to like it with a little bit of practice.


Check Also

How to Root the LG Aristo 2

The LG Aristo 2 LM-X210 is recently released budget-friendly LG phone, released in USA for …