vim命令

元修然
2023-12-01

vim命令

学习vim的时候找到的一个文档,存一下

			       Lesson 1 SUMMARY


  1. The cursor is moved using either the arrow keys or the hjkl keys.
	 h (left)	j (down)       k (up)	    l (right)

  2. To start Vim from the shell prompt type:  vim FILENAME <ENTER>

  3. To exit Vim type:	   <ESC>   :q!	 <ENTER>  to trash all changes.
	     OR type:	   <ESC>   :wq	 <ENTER>  to save the changes.

  4. To delete the character at the cursor type:  x

  5. To insert or append text type:
	 i   type inserted text   <ESC>		insert before the cursor
	 A   type appended text   <ESC>         append after the line

NOTE: Pressing <ESC> will place you in Normal mode or will cancel
      an unwanted and partially completed command.

Now continue with Lesson 2.

			       Lesson 2 SUMMARY


  1. To delete from the cursor up to the next word type:    dw
  2. To delete from the cursor to the end of a line type:    d$
  3. To delete a whole line type:    dd

  4. To repeat a motion prepend it with a number:   2w
  5. The format for a change command is:
               operator   [number]   motion
     where:
       operator - is what to do, such as  d  for delete
       [number] - is an optional count to repeat the motion
       motion   - moves over the text to operate on, such as  w (word),
		  $ (to the end of line), etc.

  6. To move to the start of the line use a zero:  0

  7. To undo previous actions, type: 	       u  (lowercase u)
     To undo all the changes on a line, type:  U  (capital U)
     To undo the undo's, type:		       CTRL-R

			       Lesson 3 SUMMARY


  1. To put back text that has just been deleted, type   p .  This puts the
     deleted text AFTER the cursor (if a line was deleted it will go on the
     line below the cursor).

  2. To replace the character under the cursor, type   r   and then the
     character you want to have there.

  3. The change operator allows you to change from the cursor to where the
     motion takes you.  eg. Type  ce  to change from the cursor to the end of
     the word,  c$  to change to the end of a line.

  4. The format for change is:

	 c   [number]   motion

Now go on to the next lesson.



			       Lesson 4 SUMMARY


  1. CTRL-G  displays your location in the file and the file status.
             G  moves to the end of the file.
     number  G  moves to that line number.
            gg  moves to the first line.

  2. Typing  /	followed by a phrase searches FORWARD for the phrase.
     Typing  ?	followed by a phrase searches BACKWARD for the phrase.
     After a search type  n  to find the next occurrence in the same direction
     or  N  to search in the opposite direction.
     CTRL-O takes you back to older positions, CTRL-I to newer positions.

  3. Typing  %	while the cursor is on a (,),[,],{, or } goes to its match.

  4. To substitute new for the first old in a line type    :s/old/new
     To substitute new for all 'old's on a line type	   :s/old/new/g
     To substitute phrases between two line #'s type	   :#,#s/old/new/g
     To substitute all occurrences in the file type	   :%s/old/new/g
     To ask for confirmation each time add 'c'		   :%s/old/new/gc

			       Lesson 5 SUMMARY


  1.  :!command  executes an external command.

      Some useful examples are:
	 (MS-DOS)	  (Unix)
	  :!dir		   :!ls		   -  shows a directory listing.
	  :!del FILENAME   :!rm FILENAME   -  removes file FILENAME.

  2.  :w FILENAME  writes the current Vim file to disk with name FILENAME.

  3.  v  motion  :w FILENAME  saves the Visually selected lines in file
      FILENAME.

  4.  :r FILENAME  retrieves disk file FILENAME and puts it below the
      cursor position.

  5.  :r !dir  reads the output of the dir command and puts it below the
      cursor position.


			       Lesson 6 SUMMARY

  1. Type  o  to open a line BELOW the cursor and start Insert mode.
     Type  O  to open a line ABOVE the cursor.

  2. Type  a  to insert text AFTER the cursor.
     Type  A  to insert text after the end of the line.

  3. The  e  command moves to the end of a word.

  4. The  y  operator yanks (copies) text,  p  puts (pastes) it.

  5. Typing a capital  R  enters Replace mode until  <ESC>  is pressed.

  6. Typing ":set xxx" sets the option "xxx".  Some options are:
  	'ic' 'ignorecase'	ignore upper/lower case when searching
	'is' 'incsearch'	show partial matches for a search phrase
	'hls' 'hlsearch'	highlight all matching phrases 
	You can either use the long or the short option name.

  7. Prepend "no" to switch an option off:   :set noic

			       Lesson 7 SUMMARY


  1. Type  :help  or press <F1> or <Help>  to open a help window.

  2. Type  :help cmd  to find help on  cmd .

  3. Type  CTRL-W CTRL-W  to jump to another window

  4. Type  :q  to close the help window

  5. Create a vimrc startup script to keep your preferred settings.

  6. When typing a  :  command, press CTRL-D to see possible completions.
     Press <TAB> to use one completion.

                           COMMAND SUMMARY
Navigate Command (also motions to make a command)

ctrl + F	: one page Foreward
ctrl + B	: one page Backward
z<Enter>	: scroll a half of one page without moving your cusor
ctrl + o	: return to the last position
ctrl+ i		: contrast to ctrl + i
w		: one Word forword
b		: one word Backword
0		: begin of the line
^		: first character of the line, ^ is the begin symbol in regular expression
$		: end of the line, $ is the end symbol in regular expression
gg		: begin of the file
G		: end of the file
[N]gg/G	: move to Nth line of the file
:[N]		: move to Nth line of the file
:$		: move to the end of the file, again, $ represents end in regular expression
:^		: move to the begining of the file, ^ alse means begin in regular expression
ctrl + G	: show information of the file, like how much lines in this file, and which line you are in now
:set ruler/noruler: turn the ruler on or off, ruler is the position information displayed on the bottom-right of your screen

Deleting Command

x		: delete the character in your current cusor
dw		: delete until end of the word, this command consist of an OPERATION d(delete), and a MOTION w(move to end of word)
[NUM]dd		: delete NUM lines, NUM means count, it's another way to comprise a command, a count means repeating the command several times
[ONUM]d[MNUM]w	: delete MNUM words and repeat for ONUM times, there's two counts, first represents the repeating times of operation, and the second motion 
.		: repeat last command(not a motion), if you want to delete multiple lines without knowing exactly how much lines you will delete, just use this command, instead of repeating dd

Getting help

:help [command]	: if you want get details of a command, use the :help command to get information
:help [partial command] <table>: if you can only remember part of the command, use this command and press <tab> to let vim complete the command for you

Cut, Copy and Paste Command

p/P		: put the content of the default register after/before your cusor
y		: yank(copy) text into the default register, it's a operator, you can use it the same way as operator d
u		: undo the last command
ctrl + r	: in the contrast to u, redo the last command
"		: specify which register to operate, the content last yanked is stored in regisger 0, and the using format is "[REG][OPERATOR]
:reg		: see the content in registers, if you want to see contents in a certain register, just append the register after the command, like :reg 0
O/o		: open a new line above/below the cursor, and go into the edit mode
I		: move the cusor to the beginning of the line, and go into the edit mode, COUNT also works for insert operation. you can use COUNTi to insert multiple times the same value
A		: move the cusor to the end of the line, and go into the edit mode
R		: change to replace mode, anything you type will overwrite the original content
r		: unlike R command, lowercase r command only let you replace one character at a time
c		: change command, it's an operator, you can use in counjection with motion. it will delete the certain content and put you in the insert mode.

Search, Find and Replace command

f/F[CHAR]	: find the CHAR forward/backword in the current line, you can use ; command to repeat the same search command, and , command repeat the command in the opposite direction. This is a MOTION, you can use it with operator like d or y.
/[WORD]		: find the WORD in the file, you can press n to search for the next, and N for the opposite direction
?[WORD]		: like the / motion, but in the opposite direction
*		: find the next appearance of the current word, you can also repeat this motion using n and N.
#		: like the * motion, but in the opposite direction
:[RANGE]s/OLD/NEW/[FLAG]: substitude the first occurence of OLD with NEW in the current line, you can set the FLAG to g to change every occurence of the current line, you can set the RANGE to make vim substitude in this range, and the default range is the current line. The format is :START,ENDs/OLD/NEW/[FLAG], % represents the entire file for range. START and END can be a pattern start with /, like this: s:/in,/the/OLD/NEW.
Text Object

Text object is a string of text, like a word, sentence or a block of text enclosed by "" or {}. If you use operator to a text object, you can edit a chunk of text at a time.

For example, if you want to delete a word, you can use the command diw/daw. Here i/a means the operate mode to the text object inner/arround. and w means the text object of word. Inner means operation only apply to the inner content of the text object excluding the delimitor and Around means opposite. For word object the delimitor is space. For " object, delimitor is ".

There're various of text object in vim, such as word(w), sentence(s), paragraph(p).Also in coding environment, you might encounter a block of code enclosed by {} or "", and the corresponding object can be represented by it's enclose token, like ", {, etc.

The general format of these command is this:

	[OPERATOR][i/a][OBJECT]
Macros

Macros are a set of key stroke. If you record a set of key stroke as a macro, you can do all the same operation with just a few types. So if you want to repeat a set of complex commands, you can record a macro first, and use the macro to do the rest of the jobs.

If you want record a macor, use @[MACRO], then the any following key stroke would be recorded into the register MACRO. When you are done with recording, just use command q to end the recording.

To reuse those macros, you can type @[MACRO].
Visual Mode

Visual mode makes you can select content, and do some operations on them.After you selected some content, type the operator so you can perform operation on the content you have selected

In visual mode, lowercase command doesn't work, only uppercase does.

v		: start characterwise visual mode. The smallest selection you can make is a character
V		: start linewise visual mode. The smallest selection you can make is a line
ctrl + v	: start blockwise visual mode. In this mode, you can select vertical text. So you can edit multiple line at a time.
o		: make the cursor go to the opposite direction
[i/a][OBJECT]	: select contents by text object
$		: select until end of every line
OBJECT		: you can also use visual mode to select A Text object. For example, command vip select a paragraph.
>/<		: After selecting a block of text, you can use >/< to shift the text right/left. Or you can do this without using visual mode, like >ip.
>>/<<		: Simply shift current line right/left.
:s		: After you selected a block of text, you specified a range in which you can perform substitude command s
:[RANGE]ce COLS	: center the range of text in the certain COLS size
:[RANGE]le IND	: left the range of text with left indent size of IND
:[RANGE]ri IND	: right the range of text with right indent size of IND
Vimrc File

The rc means Run Commands. This file stores the commands to be executed every time the vim starts. In the system, we have two vimrc files, they are System Vimrc file and Personal Vimrc file.
Normally, we leave the system vimrc file alone, and do our own configuration in our personal vimrc file.

Vimrc file are made of series of command, which begins with :, but unlike in vim editor, commands in vimrc file don't begin with :. So, you have to know some of the most common used commands.

You can enter a line beginning with " to comment, just like # token in shell.

:set		: This command can set up options of vim, for example, :set hls. You can type the set command without the option followed to check out all the options different from its default value. You can toggle the option by adding an no in front of an option or a ! right after the option, like :set nohls, or :set hls!
:h option-list	: List all the options you can set in vim
:option		: List all the options with a brief description

You can use set command to configure your Vim, Here're some common option you will usually want to set

ai		: auto indent, when you go to the next line, vim will keep you in the same indent with the last line
si		: smart indent, when you are writting code, and get into a code block, it will smartly indent the line for you
bg		: set background to light or dark

:map KEY KEYSTROKES: map a set of key strokes to a key. So when you press the key you will get the set of commands executed, For example, 
		map <F2> i<ul><CR><Space><Space><li></li><CR><Esc>0i</ul><Esc>kcit
		Leader key, use leader key to ensure you don't conflict with existing vim commands when you band keys. Such as (map <leader>w <Esc>:w!<Enter>). When you want to use the binging leader key, type \w. back slash means the leader key. You can also set the leader key token to others using (let mapleader="."). It makes the leader key token . instead of \.
:map		: You can use map command without any argument to check out all the current mappings
:source FILE	: execute every command in the FILE
:mkvimrc PATH	: create a vimrc file, and write all the settings and mappings in the current vim program into it

Buffer

When you open a file, system will load the data from the disk into the memory. The chunk of memory is the so called buffer. When you use the command :w to write the file, system will flush the buffer into the disk.

If you open multiple file at a time, vim will create multiple corresponding buffers for each file. There are two ways to open multiple file, type <vim FILE1 FILE2> in shell, or use <:e FILE> command in vim

You can use <:buffers> command to view all the buffers, which the currently opening buffers. For every buffer, there are some indicators to specify the buffer's condition
	%	: This buffer is currently being displayed in the window
	a	: This buffer is currently being active
	#	: Last active buffer
	h	: This buffer is hidden. Hidden means the buffer is loaded in the memory but not displayed in the window
	+	: This buffer has unsaved changes

If you make some changes in a file without writting it. You will get an error when you switch to other buffers, unless you force it with the command !. By doing so, you will leave the unsaved file hidden. Hidden means the file is not displayed in the window, but it still stays in the buffer. You can change this behaviour by setting the option [hidden] using <:set hidden>. Then you can switch to other buffers from an unsaved file without getting an error.

Here are some commands you can use to work with  buffers:
	:buffer[IND]	: switch to buffer IND, or you can use the short version :b[IND]
	:ls		: list all the buffers
	:bf		: switch to first buffer
	:bl		: swtich to last buffer
	:bn		: switch to next buffer
	:bp		: switch to previous buffer
	ctrl + 6	: switch to last opened buffer
	:qall!		: abandon all the changes in every buffer
	:wall		: write all buffers
	:badd FILE	: add another buffer without displaying it immediatly
	:bd BUFFER	: buffer delete, unload a buffer and delete it
	:bufdo C1 | C2 | ...: run COMMANDS in every buffer
	:E		: explore file, you can explore all the files in you directory and open a file

Open multiple window

:sp	: split,horizontally open another window with the same file. or you can give it a file to open, like <:sp file>. You can also do this by <ctrl+w s>
:vs	: vertically split, vertically open another window with the given file or the same file if not given file. You can also do the same with <ctrl+w v>
:on	: only, make the current window the only opening window, close all the other windows. You can also do the same by <ctrl+w o>
ctrl+w w: switch to next window
ctrl+w h/j/k/l:	switch to the left/down/up/right window
ctrl+w +/-: increase/decrease the height of the current window
ctrl+w >/<: increase/decrease the width of the current window
ctrl+w _: maximize the height of the current window
ctrl+w |: maximize the width of the current window
ctrl+w =: make every window the same size
ctrl+w H/J/K/L: move the current window to left/bottom/top/right with maximum height/width
:ball	: buffer all, open every buffer with their own window
:windo C1 | C2 ...: like <:bufdo> command, run commands in all windows
 类似资料: