Showing posts with label VIM. Show all posts
Showing posts with label VIM. Show all posts

8/06/2020

How to exit from VIM (VI) ?

Have you or a loved one ever had to reboot your computer to get out of a read-only file in VIM?

  • Are you responsible for a Linux system or set of systems where a graphical editor or adding new editors is prohibited?
  • Are you taking an online training course where all the labs use the VIM editor, and it’s driving you crazy?
  • Do other Open Source Pros tease you about not knowing VIM?
This short tutorial will help you get past the main issues you might encounter in quitting VIM. 

Use this tutorial to safely quit VIM, without the headaches

First off, you need to know what user you are when you edit files with VIM, so at the command line enter the id command:

IMPORTANT: One of the keys to not getting stuck in VIM is to never edit a root-only file as a regular user.  If you are the system admin, look into using the sudo command to allow editing root-only files from a regular account safely!

If you are in VIM and cannot seem to quit, we’ve got your back!  Relax, grab a drink of water, and let’s walk through a few very common scenarios.
Scenario #1
You are editing a root-only file as a regular user, have made changes to the file/buffer and it won’t let you out, showing you a message like this:
Solution #1
Press the ESC key twice or until you hear the “error bell”, then press :qa! which will force quit all open files/buffers and take you back to the command line.
Scenario #2
While trying to quit VIM with the keystrokes :q, you suddenly find the lower portion of your screen looks like a history list of the commands you’ve issued recently in VIM, and it freaks you out.
Solution #2
You have accidentally used the keystrokes q:, which opens up a Lastline or CommandLine history mode window that you could choose previous commands from to execute them again.  You can close this by carefully pressing :q again, which puts you back into Command/Normal Mode and you can quit as you would normally.
Scenario #3
You have tried to force-quit the file/buffer using :q! and suddenly you find yourself in what appears to be the command line, but it’s telling you that the q: command is not found.
Solution #3
You’ve flipped the keystrokes around and entered :!q, which is telling the VIM editor that you want to run an external command (:!command) and of course it won’t find the q: command, so all you get is an error!  Press ENTER and you’ll be back inside VIM and can exit normally, just be careful to type exactly what you want!

VI Shortcuts & Commands For Newbies

This is a collection of some of the basic VI commands and shortcuts for newbies. You’ll find yourself needing this if you are coding or scripting inside of the unix/linux command line using VI.
Command Mode: Accepts commands, which are usually in the form of individual letters. Example, a and i.
You can use h, j, k and I to navigate in similar ways as the up and down arrow. Not needed if you have an up and down arrow on your keyboard.
Yank – Term used to copy files. Use YY to yank text. Navigate to the line you wish to yank and type yy make sure you are in command mode.
2yy will yank 2 lines, 4yy will yank 4 lines. Precede the YY command with the number of lines you would like to yank.

– append text after cursor
– append text to end of current line
– Command will paste the yanked line(s) contents starting on the line after your cursor.
– Upper case P will paste the yanked line(s) starting on the line before our cursor
– Takes you to specified line
– Takes the cursor to the top of the page
– Takes the cursor to the bottom of the page
– inserts a new line below the line your cursor is on and inserts into insert mode for typing.
– undo any change, must be in command mode and only goes back one change.
– Move over one word at a time
Shift + G– Takes you to the bottom of the page
Page up and down in VI
Shift + f Page down (forward)
Shift + b page up (backward)
note* The keyboard page and and page down keys work the same
Change text – cc-allows you to change the entire line (basically removes the line and inserts into insert mode) cw to replace just one word on the line the current word your mouse is over.
Search – search forward type / and ? searches backwards; both followed immediately by the term to search for.
:%s/foo/bar will replace the first occurrence of foo with bar, operating on every line in the buffer.
:%s/foo/bar/g will replace every occurrence of foo with bar, operating on every line in the buffer.
:s/foo/bar will replace the first occurrence of foo with bar, operating on the current line.
:s/foo/bar/g will replace every occurrence of foo with bar, operating on the current line.

Also very helpful is the /c flag – it will allow you to confirm each replacement match, depending on the above criteria for searching.

– moves to next occurrence of search string
– move to next occurrence of search string in opposite direction
Ex mode: Enter ex mode from command mode with “:”. Use this to save or manipulate files. This mode is also referred to as colon commands
Replace text – :%s/fine/ one – this replaces all occurs of “fine” with “one”
% represents “global changes to all occurrences” replace to a line number, ending line number of comma.
:= – returns number of current line
:w filename – writes contents of file to a new filename.
Insert Mode: Enables you to insert and edit text. The Esc key exits insert mode and returns you back to command mode.
– enters the insert mode to replace text not insert text. It will replace text until it hits a space. After a space you must start typing over the text for it to apply or it will just push text down.
– used to insert text and not replace it. Pushes existing text down.
ZZ– Save and quit in 2 key strokes instead of wq!
:w – Saves changes without quitting
:wq – Saves changes and quits
:q! – Quits without saving changes.
:e /file.txt will load the new file.txt into the vi editor for editing. This will only occur if the current changes to the current file being edited are saved.
:r allows you to bring contents of an old file into a new one.
:! Allows you to run shell commands from within vi. Such as :!mkdir tes

Popular Posts