please could someone give me a list of some keyboard shortcut editor vim in particular to save and close?
I try ctrl + s orders or ctrl + q or ctrl + x and then ctrl + q but no success until a present
ZZ does save and close
ZQ does close without save
both work in normal mode.
For quiting vim shell,
hit ESC and
then :q for quit without any changes.
Enter
Some commands are here:
:q[uit] Quit Vim. This fails when changes have been made.
:q[uit]! Quit without writing.
:cq[uit] Quit always, without writing.
:wq Write the current file and exit.
:wq! Write the current file and exit always.
:wq {file} Write to {file}. Exit if not editing the last
:wq! {file} Write to {file} and exit always.
:[range]wq[!] [file] Same as above, but only write the lines in [range].
ZZ Write current file, if modified, and exit.
ZQ Quit current file and exit (same as ":q!").
For quick overview : Vim commands
The quick answer is:
Hit ESC
type :wq [name of the file, if it's a new file]
type Enter
The long answer, well, it's really long...
Some commands in this guide start with a colon: pressing it will display the command prompt where the subsequent command is written.
Commands without a colon are more like hotkeys - they can be used in the Vim default mode (which is the mode Vim starts in).
Commands written in CAPITAL LETTERS are specific keys: for example, ESC means the escape key on your keyboard.
All commands in Vim are case-sensitive.
EXITING VIM
To quit, discarding any changes you might have made:
:q!
Memorize: quit dammit!
To quit, saving any changes you've made:
:wq
Memorize: write to disk and quit
NAVIGATING THE EDITOR
To move around the currently open file, use your arrow keys.
To move to line 285:
:285
To search for the word import:
/import
EDITING TEXT
To start inserting text on the current cursor location:
i
Memorize: insert
To start inserting at the end of the current line:
A
Memorize: Append
To exit insert mode, and return to the default mode:
ESC
SELECTING TEXT
To start selecting, enter the visual mode:
v
Memorize: visual
Select text by moving with your arrow keys.
To exit visual mode:
ESC
COPY, CUT, PASTE
To copy the current selection into the buffer (think of it as a clipboard):
y
Memorize: yank
To cut the current selection:
d
Memorize: delete
To copy the current line into the buffer:
yy
Memorize: yank yank
To copy 3 lines including the current line into the buffer:
3yy
To cut the current line and place it into the buffer:
dd
Memorize: delete delete
To cut 5 lines including the current line:
5dd
To paste the buffer before the current line:
P
Note: Uppercase P
To paste the buffer after the current line:
p
UNDO AND REDO
To undo the last change:
u
Memorize: uh-oh :)
To redo the last change you just undid:
CTRL + R
To see the number of changes:
:undolist
To undo the last two changes:
2u
The Vim multi-level undo tree is very powerful. Read more about it here.
OPENING FILES
To open the file index.html instead of the current one:
:edit index.html
SAVING FILES
To save the file you're currently editing:
:w
Memorize: write to disk
To save the file with a different name, here changes.txt (ie. Save As):
:w changes.txt
Searching and Replacing
To search and replace all occurences of a string in the file:
:%s/typo/corrected/g
To search and replace, but prompt before replacing:
:%s/typo/corrected/gc
Memorize: confirm
Syntax highlighting and Indentation
Turn on syntax highlighting:
:syntax on
Enable automatic indentation:
:set autoindent
Increase indentation on multiple lines by selecting them in visual mode, and pressing:
>
Working with multiple files
TABS
To open server.py in a new tab:
:tabe server.py
Memorize: tab edit
To move to the next tab on the right:
:tabn
Memorize: tab next
To move to the previous tab on the left:
:tabp
Memorize: tab previous
To close a tab, move to it and use :q or :wq as you would normally.
SPLIT VIEW
To open templates/base.html in a vertical split screen:
:vs templates/base.html
Memorize: vertical split
To open shared.js in a horizontal split screen:
:sp shared.js
Memorize: the 'default' horizontal split
To move between split screens:
CTRL + W + ARROW KEYS
To close a split screen, move to it and use :q or :wq as you would normally.
More information at here.
Those are very basic questions. It is better for you to start vim in tutorial mode, like this:
$ vimtutor
Related
I opened up two windows in vim and was trying to switch between windows, but accidentally pressed Ctrl+w+Tab (instead of Ctrl + w + h). If the cursor is currently on #include <boost/multi_array.hpp>, vim would open a new horizontal new window and open the file /usr/include/boost/multi_array/extent_range.hpp, and leave the cursor on the line namespace multi_array{.
If I am in .bashrrc file and do the same thing on a export, it would open another window and open .bashrc again, with the cursor located in the place where export first appears.
I tried to search for what the use is for this shortcut, but cannot find any. I also checked my .vimrc but don't see any key mappings for this.
The thing that you need to know is that in a terminal, Tab and Ctrl+I are indistinguishable (both are ASCII character 9).
So we check the docs (:help CTRL-W) and see
CTRL-W CTRL-I same as "CTRL-W i"
and looking under "CTRL-W i" says:
CTRL-W i split window and jump to declaration of identifier under the cursor.
So there we are — it does a jump to definition (same as :tag or Ctrl+]), only in a split.
This question already has answers here:
Copy and paste content from one file to another file in vi
(19 answers)
Closed 9 years ago.
For example, when I yank some text from vim, then :wq, and open a new file. When I try to paste the text I just yanked, it doesn't work. So how to yank text between files?
use "+y to send the yanked text to the clipboard, and "+p to paste it into the new file.
So if I have a file named A that contains abcdef and I (in visual mode) select def and then press "+y while in normal mode. I have just copied def to the system clipboard. Now I can open up a file named B and (while in normal mode) press "+p the text def will be pasted into file B.
Don't quit the editor after writing with :wq
Instead, just write the file with :w and then edit the new file with :e file. Or Edit both at the same time by splitting with :sp file and ^W to switch between the screens.
if you have X11/windows, you could use "+y or "*y to yank. and same register to paste.
don't do :wq and then vim newfile. do :e newfile in same vim. then you could yank and paste between buffers. just press y and p
if you work on tty or via ssh, and don't want to do (2) either, you could save yanked part to a file, in newfile read the file.
I recommend the (2) option.
Vim has the capability of viewing multiple files at once. Use the :sp *file_name* "split" or :vs *file_name* "vertical split" commands to view another file in the same terminal.
Navigation between open file windows is simple: press Control_key + wand then either 'h', 'j', 'k', or 'l' to move the cursor to the file window to either the left, the bottom, the top, or the right of the current window. Simply 'yank' the text that you mean to copy in one terminal window, move to the terminal window containing the file that you mean to copy into as described above, and 'put' the text at the cursor location as usual.
By the way, you may use the :wqa command to close all open terminal windows and write changes made to the open files to the disk.
There is a line in Shell script
f) SCREEN = TRUE
and I want to make it
f)
SCREEN = TRUE
I cannot do this very easily in vi. Obviously in a normal text editor enter command can shift the text after f) to the next line. What is the best way to do it in vi?
Press l to move the cursor right and j to move the cursor down to get the cursor to the S. Then press i to "insert" and hit enter to create a new line. Then hit esc to stop inserting and :wq to save and quit.
There are lots of basic vi command lists on the internet that might help.
I was trying to figure out if there was a way to do this in normal mode without pressing 3 buttons to insert, move the down line with enter and then escape to normal. I believe this is what OP may have been asking for. Not sure why there aren't any built in shortcuts for this.. Anyways I just ended up making a quick mapping in my .vimrc.
nmap <leader>j i<cr><esc>
With your cursor on the line, 0fSi<ENTER><ESC>
(0 Go to beginning of line, fS find 'S', i enter insert mode, <ENTER> insert some kind of newline, <ESC> exit insert mode)
I am stuck and cannot escape. It says:
type :quit<Enter> to quit VIM
But when I type that it simply appears in the object body.
Hit the Esc key to enter "Normal mode". Then you can type : to enter "Command-line mode". A colon (:) will appear at the bottom of the screen and you can type in one of the following commands. To execute a command, press the Enter key.
:q to quit (short for :quit)
:q! to quit without saving (short for :quit!)
:wq to write and quit
:wq! to write and quit, attempting to force the write if the file lacks write permission
:x to write and quit; like :wq but writes only if modified (short for :exit)
:qa to quit all (short for :quitall)
:cq to quit, without saving, with a nonzero exit code to indicate failure (short for :cquit)
You can also quit Vim directly from "Normal mode" by typing ZZ to save and quit (same as :x) or ZQ to just quit (same as :q!). (Note that case is important here. ZZ and zz do not mean the same thing.)
Vim has extensive help - that you can access with the :help command - where you can find answers to all your questions and a tutorial for beginners.
Pictures are worth a thousand Unix commands and options:
I draw this to my students each semester and they seem to grasp vi afterwards.
Vi is a finite state machine with three main states.
It starts in COMMAND mode, where you perform editor functions using very short keystroke sequences, blindly. You know what you are doing; this isn't for amateurs.
When you want to actually edit text, you should go to INSERT mode with some keystroke; common ones include:
i: insert just before the cursor
I: move to beginning of line and (i)nsert
a: append just after the cursor
A: move to end of line and (a)ppend
o: open a new line just below the current line
O: open a new line just above the current line
R: enter REPLACE mode (similar to INSERT mode)
Now, answering the question: exiting.
You can exit vi from EX mode:
q - if you haven't made any modifications, or saved them beforehand
q! - ignores any modifications and quit
wq - save and quit
x - this is similar to wq
w and x accept a file name parameter. If vi already knows the filename to use (e.g. it was started with vi file), you need not give it here again.
At last, the most important: how can you reach EX mode?
EX mode is for long commands that you can see typing at the bottom line of the screen. From COMMAND mode, you press colon, :, and a colon will appear at the bottom line, where you can type the above commands.
From INSERT mode, you need to push ESC, i.e. the Escape button, going to COMMAND mode, and then: to go to EX mode.
If you are unsure, push ESC and that will bring you to command mode.
The robust method is ESC-:-x-Enter which saves your file and quits.
Before you enter a command, hit the Esc key. After you enter it, hit the Return to confirm.
Esc finishes the current command and switches Vim to normal mode. Now if you press :, the : will appear at the bottom of the screen. This confirms that you're actually typing a command and not editing the file.
Most commands have abbreviations, with optional part enclosed in brackets: c[ommand].
Commands marked with '*' are Vim-only (not implemented in Vi).
Safe-quit (fails if there are unsaved changes):
:q[uit] Quit the current window. Quit Vim if this is the last window. This fails when changes have been made in current buffer.
:qa[ll]* Quit all windows and Vim, unless there are some buffers which have been changed.
Prompt-quit (prompts if there are unsaved changes)
:conf[irm] q[uit]* Quit, but give prompt when there are some buffers which have been changed.
:conf[irm] xa[ll]* Write all changed buffers and exit Vim. Bring up a prompt when some buffers cannot be written.
Write (save) changes and quit:
:wq Write the current file (even if it was not changed) and quit. Writing fails when the file is read-only or the buffer does not have a name. :wqa[ll]* for all windows.
:wq! The same, but writes even read-only files. :wqa[ll]!* for all windows.
:x[it], ZZ(with details). Write the file only if it was changed and quit, :xa[ll]* for all windows.
Discard changes and quit:
:q[uit]! ZQ* Quit without writing, also when visible buffers have changes. Does not exit when there are changed hidden buffers.
:qa[ll]!*, :quita[ll][!]* Quit Vim, all changes to the buffers (including hidden) are lost.
Press Return to confirm the command.
This answer doesn't reference all Vim write and quit commands and arguments. Indeed, they are referenced in the Vim documentation.
Vim has extensive built-in help, type Esc:helpReturn to open it.
This answer was inspired by the other one, originally authored by #dirvine and edited by other SO users. I've included more information from Vim reference, SO comments and some other sources. Differences for Vi and Vim are reflected too.
If you want to quit without saving in Vim and have Vim return a non-zero exit code, you can use :cq.
I use this all the time because I can't be bothered to pinky shift for !. I often pipe things to Vim which don't need to be saved in a file. We also have an odd SVN wrapper at work which must be exited with a non-zero value in order to abort a checkin.
This is the ultimate, no-nonsense, hardcore exit command for the worst-case scenarios of exiting Vim if you want out, have no idea what you've done and you don't care what will happen to the files you opened.
Ctrl-cEnterCtrl-\Ctrl-nEnter:qa!Enter
This will get you out most of the time. Most.
You might think, naively, that you can just bang Esc a few times and then do :qa!. Oh, how you would be mistaken.
See these interesting cases where hitting Esc is not enough:
iCtrl-ovg (you enter insert mode, then visual mode and then operator pending mode)
QappendEnter
iCtrl-ogQCtrl-r=Ctrl-k (thanks to porges for this case)
iCtrl-ogQCtrl-r=inputdialog('foo','bar')Enter
:set insertmode (this is a case when Ctrl-\Ctrl-n returns you to normal mode)
In case you need to exit Vim in easy mode (while using -y option) you can enter normal Vim mode by hitting Ctrl + L and then any of the normal exiting options will work.
Vim has three modes of operation: Input mode, Command mode & Ex mode.
Input mode - everything that you type, all keystrokes are echoed on the screen.
Command mode or Escape mode - everything that you type in this mode is interpreted as a command.
Ex mode - this is another editor, ex. It is a line editor. It works per line or based on a range of lines. In this mode, a : appears at the bottom of the screen. This is the ex editor.
In order to exit Vim, you can exit while you are in either the ex mode or in the command mode. You cannot exit Vim when you are in input mode.
Exiting from ex mode
You need to be sure that you are in the Command mode. To do that, simply press the Esc key.
Go to the ex mode by pressing the : key
Use any of the following combinations in ex mode to exit:
:q - quit
:q! - quit without saving
:wq - save & quit or write & quit
:wq! - same as wq, but force write in case file permissions are readonly
:x - write & quit
:qa - quit all. useful when multiple files are opened like: vim abc.txt xyz.txt
Exiting from command mode
Press the escape key. You probably have done this already if you are in command mode.
Press capital ZZ (shift zz) - save & exit
Press capital ZQ (shift zq) - exit without saving.
After hitting ESC (or cmd + C on my computer) you must hit : for the command prompt to appear. Then, you may enter quit.
You may find that the machine will not allow you to quit because your information hasn't been saved. If you'd like to quit anyway, enter ! directly after the quit (i.e. :quit!).
I got Vim by installing a Git client on Windows. :q wouldn't exit Vim for me. :exit did however...
The q command with a number closes the given split in that position.
:q<split position> or :<split position>q will close the split in that position.
Let's say your Vim window layout is as follows:
-------------------------------------------------
| | | |
-------------------------------------------------
| | | |
| | | |
| Split 1 | Split 2 | Split 3 |
| | | |
-------------------------------------------------
If you run the q1 command, it will close the first split. q2 will close the second split and vice versa.
The order of split position in the quit command does not matter. :2q or :q2 will close the second split.
If the split position you pass to the command is greater than the number of current splits, it will simply close the last split.
For example, if you run the q100 on the above window setup where there are only three splits, it will close the last split (Split 3).
The question has been asked here.
One guaranteed way is to kill the port that runs vim
! kill - 9 $(ps | grep vim | cut -d " " -f 1)
From any of four modes: insert, visual, command or command-line (ex) mode, press this to save if changed then exit vim:
Alt-Z Z
That is, press: alt + shift + z then shift + z
Why this works: From insert mode and apparently other modes, you can exit to command mode and do a command by the combination of holding Alt with any command character. The second Alt+Shift+Z is ignored, so you must let go of the Alt but keep the Shift. Work that muscle memory! :-)
Q-first vs. Colon-first
Many people know about the colon-q exit strategy. But for extra strategery and posterity, here is the q-colon-i-q method:
In Normal mode you can type:
q:iq<enter>
If you look closely and squint, you can almost read that command aloud as "quick," since this is the slow way to exit.
(Note: my attempt at humor notwithstanding, this does work!)
I want to search for $maximumTotalAllowedAfterFinish and replace it with $minimumTotalAllowedAfterFinish. Instead of typing the long text:
:%s/$maximumTotalAllowedAfterFinish/$minimumTotalAllowedAfterFinish/g
Is there a way to COPY these long variable names down into the search line, since, on the command line I can't type "p" to paste?
You can insert the contents of a numbered or named register by typing CTRLR {0-9a-z"%#:-=.}. By typing CTRL-R CTRL-W you can paste the current word under the cursor. See:
:he cmdline-editing
for more information.
Copy it as normal, then do CtrlR" to paste. There are lots of other CtrlR shortcuts (e.g, a calculator, current filename, clipboard contents). Type :help c_<C-R> to see the full list.
Copy:
1) v (or highlight with mouse, in visual mode)
2) y (yank)
Paste:
1) / (search mode)
2) Ctrl + R + 0 (paste from yanked register)
Type q: to get into history editing mode in a new buffer. Then edit the last line of the buffer and press Enter to execute it.
Or create the command in a vim buffer , e.g. type it in the buffer:
s/foo/bar/gci
And copy it to a named register, with "ayy (if the cursor is on that line!).
Now you can execute the contents of the "a" register from Vim's Ex command line with:
:[OPTIONAL_RANGE]#a
I use it all the time.
You can place the cursor on the word that you want to add to your pattern and then press / or : to enter either the search or the command mode, and then press CtrlRCtrlW to copy the word. Source
Typically, you would do that with mouse selecting (perhaps CtrlIns or CtrlC after selecting) and then, when in the command/search line, middle-clicking (or ShiftIns or CtrlV).
Another way, is to write your command/search line in the text buffer with all the editing available in text buffers, starting with : and all, then, on the line, do:
"add#a
which will store the whole command line in buffer a, and then execute it. It won't be stored in the command history, though.
Try creating the following line in the text buffer as an example for the key presses above:
:%s/$maximumTotalAllowedAfterFinish/$minimumTotalAllowedAfterFinish/g
Finally, you can enter q: to enter history editing in a text buffer.
add a line: cnoremap <c-v> <c-r>+ in your vimrc, then you can use ctrl-v to paste.