Working in the Linux terminal every day, for me it is a no-brainer to use vi, more specifically vim. It’s installed by default on every Linux system (vi is anyway) and it is very, very powerful. The only thing that could hold you back is, as it is jokingly called, the vi ‘learning wall’ (in contrast to ‘learning curve’).
vim
is really a lot different than vi and by default not installed on many Enterprise Linux (EL) systems. Using EL 8, install it with:
$ sudo dnf install epel-release
$ sudo dnf install vim
First the basics in a nutshell. In vim you can use 3 modes:
Basics like navigating through the file when you do not have arrow keys (h, j, k and l), inserting (i) and adding (a) text and quitting (:q or :q!) or saving (:w or :x) are outside the scope of this post.
$ vim +114 /path/to/file
$ view +114 /path/to/file
:x!
:x! sudo tee %
:set nu
:set nonu
G
jump to the end of the filegg
to jump to the first line of the file:89
jump to line 89{}
jump paragraphs up and down$
jump to the end of the line^
jump to the start of the linedw
5dw
dd
8dd
D
dG
yw
10yw
yy
6yy
I briefly mentioned inserting text with i (insert), I (insert at start of the line), a (add) and A (add at the end of the line). What I’m probably using more often though to enter text is o (insert a new line after my cursor) and O (insert a new line before my cursor). Just so that you know.
Undoing when you made a mess is a push of a button in command mode: just the letter u. When you made several typos on the same line undo them all with U. Redoing is also possible with the dot (.). When you want to be sure not to save anything, quite the file without saving with :q!
:set ic
:set noic
:s/server01/server02/g
server01
in the current line to server02
. You can also do the entire file at once:
:%s/server01/server02/g
vim
. There are many shortcuts to jump a file and I think marks are quite unique. First mark a line you want to return to later:
ma
`a
'a
:marks
:delm! | delm A-Z0-9
:w ~/otherfile.txt
myfile.txt
, to a file otherfile.txt
in the home directory. You’re still working in myfile.txt
though. So, any edits after this save, are just as normal edited in the original myfile.txt
.
As easy you can write out files, you can also read them in. Say you want to insert a file in your current working file, at your current position:
:r ~/Documents/important.txt
vim
with the !
key that marks a command:
:! find ~ -name important.txt 2> /dev/null
/dev/null
to avoid permission errors cluttering the output).
Combining the two is also easy. Let’s say you for some reason want to read in the output of a command to your current file:
:r ! ls -al
ls -al
to your current position in your document. It seems there are a couple of ways you can open files simultaneously, edit them, save them, close them, switch between them, etc. The two main ways in vim:
$ vim myfile.txt otherfile.txt justanother.txt
:ball
:vert ball
:ls
:bn
:bp
:2b
:b openfileinbuffer.txt
:vert ball
CTRL WW
:new
:vnew
:vnew newfilefromvim.txt
:e ~/existingfile.txt
:vs ~/existingfile.txt
:Ex
:Vex
:close
:bw
:mksession! ~/my.ses
:xa
$ vim -S ~/my.ses
:qa!
To be honest I don’t use buffers a lot anymore but doing a comprehensive post on vim
without at least explaining a couple of the possibilities would feel incomplete. I think it emphasizes the many possibilities you have with vim
.
There is a cool saying I’ve read a while ago, that I unfortunately can’t find the source for, but always sticked with me. It is something along the lines of, ‘When you’re logging in on a server with SSH, you’re doing it wrong’. Meaning that you should never login via SSH and only infrastructure and configuration management tools should touch your servers. Although this is a noble goal and absolutely something we should strive for, in practice I see that a lot of people end up in the terminal anyway. For instance, for troubleshooting. This post was for these people 😉
Next time I want to briefly discuss a terminal tool I use daily: screen
. After that we will me moving on to some cool technologies like containers, Terraform, Ansible, etc.
There is so much to tell about vim that I had to make choices and had to end the post somewhere. There are a couple of points I want to briefly mention though:
And there are more tools and games out there. Have fun!
There has always been a dream to build something that’s bigger than yourself. A company by techies for techies. Not money driven but fun driven, the Greater connection is the foundation of our business.
Boeingavenue 217 (4th floor)
1119 PD Schiphol-Rijk
hello@greater.nl
backoffice@greater.nl
sales@greater.nl
Backoffice: +31 (0)85 303 64 28
Sales: +31 (0)85 303 64 23
Follow us on social media
©2016 – 2022 Greater BV | All rights reserved.
KVK: 65131460
BTW: NL855992499B01
Greater BV