While I cannot deny that I too struggled with vi in the beginning, I think an argument can be made that vi is actually very simple to learn. (When I began using Unix the choice was either vi or emacs, so at the time I chose ed, ed being a very simple line editor, which I had learned already from reading early Unix textbooks.) I use vi daily, but I probably use only a very small subset of its features.
Here's first an argument for using vi: vi exists on all systems, uses very few resources, and may be usable over unstable and slow connections, where other editors may fail or be slow and unusable. Or in situations where the system is broken, so arrow keys are not interpreted correctly (bad terminfo settings for example.) So vi can be used to fix the configuration files leading to the problem, without being affected by the problem.
And here is how to learn a useful subset of vi:
First: stop thinking of vi as an interactive, terminal-GUI "editor", and see it as a programming language for manipulating text files. In the beginning "vi" stood for VIsual mode, and vi has a nonvisual counterpart ex, which is a good place to start.
Type ex at the shell prompt (I will use ¬ to represent return):
$ ex¬
Entering Ex mode. Type "visual" to go to Normal mode.
:
Now press enter.
:¬
E749: empty buffer
:
So, lets put something in it. Type "a¬", and type a line of text, then a period on a line by itself:
:a¬
Hello World¬
.¬
:
That was the append command. Easy, short and mnemonic. But it would be nice to see what is in the buffer:
:%p¬
Hello World
:
That was the print command, p, obviously. The % is a line range shorthand. Each command may be preceded by a line number or range which the command should apply to. % means "all lines". $ means the last line, and a period means the current line. A number means that particular line. A range is written n,m and a number can be added or subtracted, so .+1,$-1 mean from the next line to the second-last line. A command consisting of just a line number goes to that line. Here is the remaining small subset of commands I use:
:1i¬
this text is inserted before line 1.¬
.¬
:
:w /tmp/filename¬
"/tmp/filename" [New] 2L, 49C written
:
w writes all (or the chosen range of) lines to a file.
:1d¬
deletes line 1.
:.s/Hello/Hello,/¬
Hello, World
:.s/$/!/¬
Hello, World!
s substitutes a regular expression. It's good to know regular expressions!
:q¬
E37: No write since last change (add ! to override)
q quits. wq writes and quits. q! quits without writing, and in a similar vein, w! forces a write to a file if possible.
Now a cool one:
:p
Hello, World!
:.!tr a-z A-Z
:p
HELLO, WORLD!
! as a command filters the lines into a shell command.
And finally:
:vi¬
enters VISUAL mode.
Don't be fooled. It looks like an editor, but it's still just an interactive programming shell. You just get to look at the buffer all the time, and a few more commands. In visual mode a : shows the : prompt from ex mode. The arrow keys (if they work) move around, and if they don't, then hjkl do! h left, j down, k up and l right. 0 moves to the first character and ^(think regex here!) to the first non-space character on the line. $ (regex again!) moves to the end of line. Typing :999¬ goes to line 999, naturally, just like in ex mode.
"i" enters insert mode, which now inserts on characters instead of lines, and ends with pressing escape instead of ".¬". "I" inserts at the beginning, "a" after the character under the cursor, and "A" after the end of the current line. Always press escape when done typing text. "x" deletes the character under the cursor, and "D" deletes from the cursor to end of line. "Y" yanks (copies) the current line, and "P" pastes it back. ":pu" does the same, but can be preceded by a line number. ":ya" is the ex equivalent of "Y", and again, useful to apply to a line range. I think there is a mark command as well, but I will admit I don't remember it. I don't miss it.
The above is what I use, and I know I am probably not a very "efficient" vi user. On the other hand, I do use ex's versatility, for example to script editing configuration files. Need to change your hostname foo.bar.com to www.foobar.com in a number of conf files?
for file in conf/*
do (echo "%s/foo.bar.com/www.foobar.com/" ; echo "wq") |ex $file ; done
What I am trying to say is, that I think the problem with vi is that people think of it as a difficult editor. All it takes is to change your mindset a little, and view it as a very simple yet also very powerful interactive programming language instead. So powerful that even with a subset of the available commands, you can use it to great effect - not just as an editor but as a general, scriptable tool well integrated with the rest of Unix. I doubt nano would do all of this - emacs...maybe.
mcedit
(part ofmc
). If I need anything else, it usually means I don't need a text editor but either a text processor or an IDE. There's a lot of console-based editors out in the wild (pico
,nano
,vi
,vim
,ed
,emacs
,joe
... the list goes on), but there was only one editor I didn't need to memorize at all - and that'smcedit
(based oncooledit
btw). – May 29 '17 at 21:43ciw
diw
yiw
and a few movement commands to really understand what vim is trying to do. Baby steps might work for some, but in my case (and many other anecdotal cases) going into the deep end for a week worked out better than the one-step-at-a-time approach because as a noob I would be tempted to resort to 'jjjjjjjjjjjhhhhhh' every time I switched back from a "dumb" editor. It's akin to learning Morse Code at 18 wpm (good) vs 5 (bad habits). – SO_fix_the_vote_sorting_bug Jan 07 '21 at 19:15