The Vim Text Editor

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
1,203
Reaction score
2,230
Credits
3,442
I use Vim, all my homies use Vim.. do you use Vim? Vim is a powerful and popular text editor for Linux that is known for its efficiency and flexibility. It is widely used by developers and system administrators for writing code, configuration files, and scripts. While Vim can be intimidating for those new to the text editor, its features and functionality make it well worth the time to learn.

In this article, we will go over the basics of using Vim in Linux, including how to open files, navigate the text, and perform basic edits. We'll then go into some more advanced features after that.

Getting started with Vim

To open a file in Vim, simply type vim [file_name] in the terminal. This will open the file in Vim, and you will be in Command mode, which is used for navigating the text and executing commands. To start editing the text, press i to enter Insert mode. You can then type in your text and use the cursor keys to navigate.

To save the changes you've made to the file, press Esc to return to Command mode, and then type :w followed by the Enter key. To exit Vim, type :q and press Enter. If you want to exit and save your changes, type :wq and press Enter. Another way to quickly save and quit is to just type ZZ (no hitting Enter required).

Navigating text

In Command mode, you can use the cursor keys to navigate the text, but Vim provides several shortcuts that can make navigation even faster. For example, the h key moves the cursor to the left, the j key moves it down, the k key moves it up, and the l key moves it to the right. The w key moves the cursor to the next word, and the b key moves it to the previous word. The 0 (zero) key moves the cursor to the beginning of the line, and the $ key moves it to the end of the line.

Performing basic edits

Once you've navigated to the desired location in your text, you can perform basic edits. In Insert mode, you can insert text as you normally would in any text editor. To delete a character, navigate to it in Normal mode and press the x key. To delete a word, navigate to the first letter of the word and type the dw keys. To delete a line, navigate to the line and type dd.

Undoing changes

If you make a mistake while editing your text, you can undo your changes by pressing the u key in Command mode. To redo a change, press ctrl-r.

Searching and replacing text

Vim also provides powerful search and replace functionality. To search for a word in your text, press / followed by the word you're searching for and press Enter. Vim will highlight the first occurrence of the word and move the cursor to that location. You can press the n key to jump to the next occurrence, and the N key to jump to the previous occurrence.

Similarly, you can search upwards by typing ? in Command mode!

To replace a word in your text, type :%s/old_word/new_word/g and press Enter. This will replace all occurrences of old_word with new_word.

More advanced edits / commands

:w filename - This command allows you to save the contents of the file to a different file name.

:r filename - This command reads the contents of another file and inserts it into the current file at the cursor location.

:set number - This command displays line numbers in the left margin of the editor, making it easier to navigate large files. To turn off line numbers, use the command :set nonumber.

:g/pattern/d - This command deletes all lines in the file that contain the specified pattern. For example, :g/error/d would delete all lines that contain the word "error".

:split filename - This command splits the editor window into two panes, with the specified file open in the new pane.

:vsplit filename - This command splits the editor window vertically into two panes, with the specified file open in the new pane.

:tabnew filename - This command opens the specified file in a new tab in the editor.

:tabclose - This command closes the current tab.

:tabs - This command displays a list of all open tabs in the editor.

gt - Will bring you to the next tab

gT - Will put you on the previous tab

Conclusion

Vim is a powerful and flexible text editor that is widely used by developers and system administrators. While it can be intimidating for those new to the text editor, its features and functionality make it well worth the time it takes to master it.

Type man vim for more info! Also check out the vimtutor application!
 
Last edited:


For those interested, I thought Vim Adventures was a great way to learn Vim and practice the muscle memory.
I think i remember a video game app that helped you learn vim .. was that what vim adventures was?

Edit: lol yep it was. I think i remember a space invaders kinda game also?
 
Vim Rocks! This year will by my 30th year of Vim. Do I know "everything" about it Oh !@#$%^&*! NO! But, I know enough to do what I need to do.
It's not the fastest executing format, but it gets the job done, and it's a whole lot easier than some of the other languages. I did find a script compiler several years back, I lost it in a drive melt down. I just haven't bothered to go looking for it again.
 
Yup, I’ve been using Vim pretty much every single day, for many years. And I regularly discover new functionality. Sometimes I even re-discover functionality I’d completely forgotten about, ha ha!

At this point in time, I’ve probably forgotten more about Vim than some people ever know, ha ha!

BTW: I’m currently drafting a .vimrc related thread, to share some of my favourite, or most useful .vimrc modifications.

I might even turn it into a series of posts. Because as usual, I’m going into a lot of detail on each one.
Hmmmm… It may even be an idea to create a new thread for each modification, so others can ask questions, or discuss each one separately. That might avoid things getting too messy!

I’ll probably add some posts on additional topics. Like recording and using macros in Vim, using copy/paste with different registers etc.

My absolute favourite .vimrc modification is re-mapping jj and JJ to escape, to allow me to quickly return to "normal" mode, from "insert" mode or "command mode".

Note: "command" mode is when you’re entering a command using vim’s command operator :.

I find that jj is much more convenient and less disruptive to my flow, than making my hand leave the home row to reach for the escape key.

And literally less painful than extending both of my pinkies, to reach for the default, alternative escape keybind ctrl [.

Having arthritis and carpel tunnel in my hands, I try not to use too many ctrl keybinds when editing text.

But in my opinion - out of all of the editors, Vim has the most "hand friendly" key-binds. Thanks to vi and vim’s almost unique, modal features, many of its default keybinds do not require painful hand gymnastics.
The default keys are pretty intuitive too.

For beginners:
Learning the basics of Vim may seem like a bit of an uphill struggle at first. I remember some of my early frustrations with Vim. Especially when getting used to switching modes.

But in my experience, once it "clicks" and you get used to the Vim way of doing things, you’ll wonder how you ever lived without it! Ha ha!

Anyway, I’m not going to give away the code for the remap here. You’ll have to wait for my post/s! :p
 
Last edited:
For beginners:
Learning the basics of Vim may seem like a bit of an uphill struggle at first. I remember some of my early frustrations with Vim. Especially when getting used to switching modes.

But in my experience, once it "clicks" and you get used to the Vim way of doing things, you’ll wonder how you ever lived without it! Ha ha!
I went through the book "Mastering Vim Quickly" some time ago but I never actually took what I learned into practice because it seemed hard to add to my work flow. I'm not a vim pro nor a beginner but I can get around it enough to work efficiently enough but I want to get better with vim.
 
@Rob
Another useful, basic key you forgot was:
e - move to end of word.
E - move to the end of the word, skipping over special characters in words.

Also, the uppercase versions of F, B - these go forwards and backwards by one word, ignoring special characters in words.

What do I mean by ignoring special characters in words?
If we have a compound word like "short-term" and the cursor is on the ‘s’.
Pressing f once will move the cursor to the hyphen ‘-‘. A second press takes you to the ‘t’ in ‘term’. A third takes you to the start of the next word.

Whereas pressing F would ignore the hyphen and the rest of the word and skip straight to the next word.

So F, B and E ignore special characters in words. But f, b and e do not.

And some additional "normal" mode motion keys that are useful:
ctrl u - scroll up half a screen
ctrl d - scroll down half a screen
ctrl f - scroll down a whole screen
ctrl b - scroll up a whole screen
ctrl e - scroll the screen up one line, without moving the cursor
ctrl y - scroll the screen down one line, without moving the cursor

Note: Those last two keybinds WILL move the cursor if the current line goes off the screen.

And one criminally overlooked feature of Vim, the dot . operator, which allows you to repeat the last edit you made.

Oh, and zz, which scrolls the page so the current line in the middle of the screen. Be careful with the caps lock for that one though, because as mentioned in your article - ZZ exits without saving (it’s the same as qa!)!!

Also H, M and L, which move the cursor to the top, middle or bottom line that is onscreen. Think high, middle, low.

Then there are the jump lists and buffers. I’ll be touching on buffers in my .vimrc series though. I have some mods in my Vim config that deal with managing buffers, tabs etc.

And vims jump lists are one of the other topics I was going to write a post about.
 
Last edited:
While Vim can be intimidating for those new to the text editor, its features and functionality make it well worth the time to learn.
I personally just don't like the modal style of using it, if i look away from the terminal at firefox for help i normally forget what mode i was previously in, so then it just feels frustrating to use.

However, if i was some sort of a system administrator who basically only had recourse to it in a terminal environment/setting, then i wouldn't get so annoyed with it. Vim is fun to use at times, and you can basically use sed from inside of the program with its search and replace...which is pretty neat.
 
I'm wondering though, what do you veterans do when you quit vim? For me, when i had all those tabs open inside of it, my go to options was:

Code:
:wa | x

I've set it up to do the same thing sooo much faster in nano...but it was those neat commands like that that really made vim special to me. I really liked using "o" to go into insert mode, makes it so it's the same amount of typing to create a new line in any other text editor.
 
I'm wondering though, what do you veterans do when you quit vim? For me, when i had all those tabs open inside of it, my go to options was:

Code:
:wa | x

I've set it up to do the same thing sooo much faster in nano...but it was those neat commands like that that really made vim special to me. I really liked using "o" to go into insert mode, makes it so it's the same amount of typing to create a new line in any other text editor.
You could simply use ZZ or :xa both are pretty much exactly the same!!
 
oh...and now that i have some time to work on things today, i realized that what i probably meant to post in terms of saving and exiting all tabs was this:

Code:
:wa | xa

and silly me, i didn't put any instructions of how to open files with vim in tabs within my help file because it's probably something super easy like "vim -t <file> <file2>", such a complicated text editor!
 
oh...and now that i have some time to work on things today, i realized that what i probably meant to post in terms of saving and exiting all tabs was this:

Code:
:wa | xa

and silly me, i didn't put any instructions of how to open files with vim in tabs within my help file because it's probably something super easy like "vim -t <file> <file2>", such a complicated text editor!
You just need :xa.
No need for :wa before using :xa.

You’d use :wa to save all files, if you’re going to continue editing files.

x is for save and exit. This will exit Vim after saving any unsaved changes to the current file. If there are no changes, it just exits.

But if you have more than one file open, you have to use :xa for save and exit all. Which saves all changed files before exiting.

Re: But does ZZ save all your files?

Oh, crap! I was wrong there.
No, ZZ is the same as :qa! isn’t it?!
So it quits all, without saving….. Ooops!

Also, to open files in separate tabs, you need to use the -p option:
Bash:
vim -p file1 file2 file3 etc

From what I remember, there is no -t option.

There is a -T option, which allows you to specify which type of terminal you’re using. But that’s mostly obsolete nowadays. Vim can automatically detect which terminal is in use. I don’t ever recall needing to use that option.

And if you open multiple files without using -p, each of the files get opened in separate buffers and you can work on a single buffer at a time.
Or once loaded, you could split the screen, or open a new tab and then switch to another buffer to view/edit…. It sounds complicated, but it’s really not!

The posts I’m drafting on .vimrc modifications will give more details on dealing with buffers and tabs AND includes some .vimrc modifications that make buffer and tab management an absolute doddle.

That whole idea is gradually turning into a bit of a monster though. I’m half considering maybe making them a video series. A picture’s worth a thousand words and all of that…. Hmmm, we’ll see!
 
Last edited:
That whole idea is gradually turning into a bit of a monster though. I’m half considering maybe making them a video series. A picture’s worth a thousand words and all of that…. Hmmm, we’ll see!
I've seen a couple videos and i was mostly confused with a lot of un-answered questions, so i would think some sort of linear explanation would be better than a video. The tricky thing is that nothing is going to work the same for everyone.
 
Sometimes while coding I'll have e 7 or 8 files open with a horizontal split and keep a :terminal open in the bottom split.
However, if I want to close one of the buffers (using :bd, for example) it closes the buffer and removes my split window. Is there a better way of doing this so I don't have to re-split, then resize the windows?
 
The easiest way is to switch to another buffer in the split containing the file you want to close and then close the buffer you just switched from.
e.g.
Starting in the split containing the buffer you want to close:
1. Load, or switch to another buffer you want to edit next in the existing split.

So, if you're opening a new file:
- Use something like :e /path/to/next-file.

OR If you're switching to another existing buffer:
- List the loaded buffers using the :buffers, or :ls command (both do the same thing). And take note of the buffer number for the file you want to switch to.

- Use :b n, to switch to the buffer you want to work on next.
- Where n is the number of the buffer you want to switch to.

Or you could even open a new, unnamed buffer in the current split using the :enew command.
For example, if you plan to work on a new file, but you don't know what you're going to call it, or where to save it. Or perhaps the new buffer is just going to be a temporary scratchpad. In which case enew is the command you want.

2. Whatever you chose to do. Now you've loaded/switched to another buffer, you can close the other buffer you were just working on without losing your existing split.
To do this:
- (optionally) list all of the loaded buffers again using :buffers, or :ls and take note of the number of the buffer you want to close.
And finally:
- close the buffer using :bd n
- Again, where n is the number of the buffer you want to unload/delete.

It's also worth knowing that the :b and :bd commands allow you to specify the name of the buffer too. You don't have to use a number. You can tab complete the names. But I just find it quicker and easier to use the buffer numbers. (less typing!)

BTW - I have a couple of handy .vimrc modifications that make these kinds of buffer management tasks a little quicker. But I'll reveal those in my other, pending .vimrc modification related threads!
 

Members online


Top