Bmetuno Solutions
Bmetuno Solutions

Reputation: 19

keyboard shortcut for vim

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

Upvotes: 1

Views: 6977

Answers (5)

Ave
Ave

Reputation: 4430

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.

Upvotes: 2

Kent
Kent

Reputation: 195269

ZZ does save and close

ZQ does close without save

both work in normal mode.

Upvotes: 5

pradeep1991singh
pradeep1991singh

Reputation: 8385

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

Upvotes: 3

Federico klez Culloca
Federico klez Culloca

Reputation: 27149

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...

Upvotes: 2

logc
logc

Reputation: 3923

Those are very basic questions. It is better for you to start vim in tutorial mode, like this:

$ vimtutor

Upvotes: 0

Related Questions