欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

vim 各个字符的含义 有大用 有大大用 有大大大用







Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 462 Printable Monobook Previous Next

created 2003 · complexity basic · author Nitya · version 6.0


Place the cursor on any variable in your program.

  • gd will take you to the local declaration.

  • gD will take you to the global declaration.

  • g* search for the word under the cursor (like *, but g* on 'rain' will find words like 'rainbow').

  • g# same as g* but in backward direction.

  • gg goes to the first line in the buffer (or provide a count before the command for a specific line).

  • G goes to the last line (or provide a count before the command for a specific line).

See alsoedit | edit source




来自 https://vim.fandom.com/wiki/Go_to_definition_using_g




My Favorite Commands from a Year of Vim


I’ve been using Vim as as my primary text editor for a little over a year now, and I’d like to share some of my favorite commands that I’ve discovered during that time. I’ll focus on sharing commands over configuration, so they can be used in any Vim environment.

Movement Commands

t_ | T_

t will move you to the next occurrence of the character afterwards, placing your cursor right before the character searched. T is very similar, except it will move backwards to the next occurrence, placing your cursor to the right of the character searched.

f_ | F_

The f command is very similar to the t command, but it places your cursor on top of the character. The F command works how you might expect, moving your cursor backwards and on top of the next occurrence of the selected character.

;

The ; command is very helpful, allowing you to repeat your last f or t command.

gg | G

These commands let you move around the entire file. gg will move you to the first line in the file, and G will move you to the last line in the file. Alternatively, if you type nG where n is a number, you’ll jump to that to that line.

* | #

The * command will search forward for the word under your cursor, while the # command will search backwards. These two are useful for jumping around a file to find a local function or variable.

Indentation Commands

< | > | =

These commands are all related to indentation. < and > will increase or decrease indentation respectively. The = command is similar except that Vim sets the indentation to what it thinks it should be, given the lines contextually. All of these commands can be applied to just the current line by repeating them twice, instead of combining them with a movement.

gg=G
This is a handy command that will tell Vim to redo the indentation on your current buffer.

Moving Text to/from System Clipboard

After using Vim for a little while, you might wonder how to get the text from your clipboard into Vim, and vice versa.

Vim’s command to access any registers is . After that, you can use + to access the system clipboard register. Therefore, ”+p would be the equivalent of a paste command, and "+y would be the equivalent of a copy command (if you’re in visual mode or you use some movement with the y command).

Text Objects

Many Vim commands can be applied to text objects instead of movements, and they often allow for much easier text manipulation. A few text objects and their corresponding Vim keys are word, tag, paragraph, sentence, and others like parentheses, quotes, and the varieties of brackets.

One example of how to work with these text objects is diw. I read this command as delete inside word. The effect is to delete the current word from anywhere within it. Instead of i, you can use a, which will include the surrounding whitespace in the text object.

Visual Selections

I think most Vim users know about the v command to enter visual selection mode. As you might expect, this mode lets you select the lines between the starting point and your cursor, wrapping the selection across any line breaks encountered.

There are two other modes to visual selection: line selection and block selection. Line selection is entered with V, allowing you to select entire lines at a time. Block selection, which you can enter with cmd-v, will select a rectangular area from your starting cursor to your end cursor. This allows you to insert the same text on multiple lines or replace parts of content on multiple lines.

What commands do you find most useful? I’d love to hear about them in the comments.

来自 https://spin.atomicobject.com/2017/09/24/favorite-vim-commands/


Popular Vim Commands - Comprehensive Vim Cheat Sheet

By Cody Arsenault
Updated on July 13, 2020


Popular Vim Commands - Comprehensive Vim Cheat Sheet


Vim was made available in 1991 and is a free, open source software. Available both as a command line interface and as a standalone program with a GUI, Vim is a text editor that is a modal version of the vi editor created for Unix in the 1970s; Vim stands for vi improved. While it was designed with Unix in mind, versions of it are available for most operating systems and Vim is also available for Android and iOS smartphones.

While you may be familiar with the concept of a text editor, the modal part may throw you. A modal editor is one that allows you to edit text in different modes, and in the case of Vim, the mode determines what the alphanumeric keys on your keyboard do and how Vim editor commands work.

For example, in insert mode, your keyboard behaves normally, so what you type in is what you see, just like with a standard text editor. However, if you switch to command mode, the letters on your keyboard will allow you use Vim commands to move within the text. If you play video games that use the left-hand keys on the keyboard to move your character around, this concept is probably familiar to you.

To open a file using Vim you can use the following command (simply replace filename.css with your actual file name).

vim filename.css

The idea behind having a modal text editor is that it allows you to write and edit text, including code, without requiring your hands leave the keyboard. Vim isn't for everyone, and it requires you to learn a variety of Vim editor commands to get the most out of it. That said, many people who have started using it and become comfortable with it won't even consider another editor. The image below shows an exampe of what a css file looks like when viewed using Vim.


vim editor


This text editor is particularly well-suited for people who are programmers, coders, system administrators or individuals looking for a streamlined way to edit text. The editor allows you to edit text in multiple windows, which can be helpful to programmers and editors alike. If you're interested in giving Vim a shot, the following is a basic explanation of Vim modes and a list of frequently used Vim commands, along with a definition of what they do.

Vim modes#

There are some arguments as to how many modes that Vim has, but the modes you're most likely to use are command mode and insert mode. These modes will allow you to do just about anything you need, including creating your document, saving your document and doing advanced editing, including taking advantage of search and replace functions.

Command mode#

This is the default mode that you'll be in once you open Vim. If you're in a different mode and want to go back to command mode, just hit the Escape key. This mode allows you to use Vim commands and move through your document. From command mode, you can also use last-line commands, which generally start with the use of a colon. For example, :w saves your file and :q allows you to exit Vim.

Insert mode#

This mode allows you to enter text into your document. You can enter insert mode by pressing the i key. Keep in mind that to save your document, you'll need to go back to command mode since only text input is allowed in this mode.

Installing Vim#

There are a few ways to install Vim and the one you end up using will sometimes depend on which system you're using.

Install Vim using Git:

According to Vim themselves, install Vim via Git is the simplest and most efficient method. Simply use the following commands:

git clone https://github.com/vim/vim.git
cd vim/src
make

If you already have Vim installed but want to update to the latest version, you may need to use additional Git commands which can be found here.

Install Vim on Ubuntu/Debian:

If you're using Ubuntu or Debian use apt-get to install Vim, like so:

sudo apt-get install vim

Install Vim on CentOS/Fedora:

If you're using CentOS or Fedora, use yum to install Vim:

sudo yum install vim

If you want a more advanced set of features on CentOS/Fedora, you'll need to install vim-enhanced, to do this, run the following command instead:

sudo yum install -y vim-enhanced

Vim commands#

The following is a list of frequently used commands and what they do. Many of the commands can be made to repeat by adding a number to the command. This is not an exhaustive list because more advanced commands, such as how to use multiple buffers, are not included. However, just about all of the basic commands for opening, editing and saving documents are included as well as commands that enable you to find and replace text and work with multiple documents.

1. Basic Vim commands#

The most simple commands allow you to open and close documents as well as saving them. As with most other text editors, there are protections in place to help you avoid exiting the editor without having saved what you're working on.

:help [keyword] - Performs a search of help documentation for whatever keyword you enter

:e [file] - Opens a file, where [file] is the name of the file you want opened

:w - Saves the file you are working on

:w [filename] - Allows you to save your file with the name you've defined

:wq - Save your file and close Vim

:q! - Quit without first saving the file you were working on

2. Vim commands for movement#

When using movement commands, you can put a number in front of them to make Vim complete a command multiple times. For example, 5h will move your cursor five spaces to the left, and 90j will put your cursor at the beginning of the 90th line down from where your cursor currently is.

h - Moves the cursor to the left

l - Moves the cursor to the right

j - Moves the cursor down one line

k - Moves the cursor up one line

H - Puts the cursor at the top of the screen

M - Puts the cursor in the middle of the screen

L - Puts the cursor at the bottom of the screen

w - Puts the cursor at the start of the next word

b - Puts the cursor at the start of the previous word

e - Puts the cursor at the end of a word

0 - Places the cursor at the beginning of a line

$ - Places the cursor at the end of a line

) - Takes you to the start of the next sentence

( - Takes you to the start of the previous sentence

} - Takes you to the start of the next paragraph or block of text

{ - Takes you to the start of the previous paragraph or block of text

Ctrl + f - Takes you one page forward

Ctrl + b - Takes you one page back

gg - Places the cursor at the start of the file

G - Places the cursor at the end of the file

# - Where # is the number of a line, this command takes you to the line specified

3. Vim commands for editing#

Those who use Vim tend to use the term "yank" where most people would use the terms copy and paste. Therefore, the command for copying a word is yw, which stands for yank word, and the command for pasting whatever has been copied is p, meaning put. If you look up additional commands in the future, it can be confusing if you don't know what yank and put mean when using Vim.

You also have two options for how to select text. You can either use commands like dd, which deletes a single line, and yy, which copies a single line, or you can highlight text and then copy it to the unnamed register. The paste commands work the same whether you've highlighted text or used a command to automatically copy it.

As with movement commands, putting a number in front of the command can increase the number of times a task is completed. For instance, putting a number in front of yy will increase the number of lines copied, so 5yy will copy five lines.

yy - Copies a line

yw - Copies a word

y$ - Copies from where your cursor is to the end of a line

v - Highlight one character at a time using arrow buttons or the h, k, j, l buttons

V - Highlights one line, and movement keys can allow you to highlight additional lines

p - Paste whatever has been copied to the unnamed register

d - Deletes highlighted text

dd - Deletes a line of text

dw - Deletes a word

D - Deletes everything from where your cursor is to the end of the line

d0 - Deletes everything from where your cursor is to the beginning of the line

dgg - Deletes everything from where your cursor is to the beginning of the file

dG - Deletes everything from where your cursor is to the end of the file

x - Deletes a single character

u - Undo the last operation; u# allows you to undo multiple actions

Ctrl + r - Redo the last undo

. - Repeats the last action

4. Vim commands for searching text#

Like many other text editors, Vim allows you to search your text and find and replace text within your document. If you opt to replace multiple instances of the same keyword or phrase, you can set Vim up to require or not require you to confirm each replacement depending on how you put in the command.

/[keyword] - Searches for text in the document where keyword is whatever keyword, phrase or string of characters you're looking for

?[keyword] - Searches previous text for your keyword, phrase or character string

n - Searches your text again in whatever direction your last search was

N - Searches your text again in the opposite direction

:%s/[pattern]/[replacement]/g - This replaces all occurrences of a pattern without confirming each one

:%s/[pattern]/[replacement]/gc - Replaces all occurrences of a pattern and confirms each one

5. Vim commands for working with multiple files#

You can also edit more than one text file at a time. Vim gives you the ability to either split your screen to show more than one file at a time or you can switch back and forth between documents. As with other functions, commands make going between documents or buffers, as they're referred to with Vim, as simple as a few keystrokes.

:bn - Switch to next buffer

:bp - Switch to previous buffer

:bd - Close a buffer

:sp [filename] - Opens a new file and splits your screen horizontally to show more than one buffer

:vsp [filename] - Opens a new file and splits your screen vertically to show more than one buffer

:ls - Lists all open buffers

Ctrl + ws - Split windows horizontally

Ctrl + wv - Split windows vertically

Ctrl + ww - Switch between windows

Ctrl + wq - Quit a window

Ctrl + wh - Moves your cursor to the window to the left

Ctrl + wl - Moves your cursor to the window to the right

Ctrl + wj - Moves your cursor to the window below the one you're in

Ctrl + wk - Moves your cursor to the window above the one you're in

6. Marking text (visual mode)#

Visual mode allows you to select a block of text in Vim. Once a block of text is selected you can use visual commands to perform actions on the selected text such as deleting it, copying it, etc.

v - starts visual mode, you can then select a range of text, and run a command

V - starts linewise visual mode (selects entire lines)

Ctrl + v - starts visual block mode (selects columns)

ab - a block with ()

aB - a block with {}

ib - inner block with ()

iB - inner block with {}

aw - mark a word

Esc - exit visual mode

Once you've selected a particular range of text, you can then run a command on that text such as the following:

d - delete marked text

y - yank (copy) marked text

> - shift text right

< - shift text left

~ - swap case (upper or lower)

7. Tab pages

Just like any browser, you can also use tabs within Vim. This makes it incredibly easy to switch between multiple files while you're making some code changes instead of working in one single file, closing it, and opening a new one. Below are some useful Vim commands for using tab pages:

:tabedit file - opens a new tab and will take you to edit "file"

gt - move to the next tab

gT - move to the previous tab

#gt - move to a specific tab number (e.g. 2gt takes you to the second tab)

:tabs - list all open tabs

:tabclose - close a single tab

Simple Vim workflow example#

If you haven't had a chance to play around with Vim much yet, you might be wondering what a simple workflow looks like when using it. It's relatively simple:

  1. Open a new or existing file with vim filename.

  2. Type i to switch into insert mode so that you can start editing the file.

  3. Enter or modify the text with your file.

  4. Once you're done, press the escape key Esc to get out of insert mode and back to command mode.

  5. Type :wq to save and exit your file.

Of course, there is so much more you can do with Vim, however as a beginner, the above steps are what a simple Vim workflow looks like.

Summary#

Vim is quite easy to use, it just involves memorizing Vim editor commands and remembering what mode you're in. If you're used to using keyboard shortcuts like Ctrl + C and Ctrl + S, you shouldn't have too much difficulty getting used to the way that Vim works. While there is a bit of a breaking in period with the editor, you don't have to worry too much about accidentally deleting large swathes of text without being able to recover them since you can use the undo command multiple times.

While not for everyone, functionality like being able to work on more than one document at a time in windowed screens and the ability to do major editing without a mouse is what makes Vim so popular. You can download the editor for free, and there are a variety of plugins and extensions that can improve its functionality and add additional Vim commands.


来自  https://www.keycdn.com/blog/vim-commands


Global

  • :h[elp] keyword - open help for keyword

  • :sav[eas] file - save file as

  • :clo[se] - close current pane

  • :ter[minal] - open a terminal window

  • K - open man page for word under the cursor

Tip Run vimtutor in a terminal to learn the first Vim commands.

Cursor movement

  • h - move cursor left

  • j - move cursor down

  • k - move cursor up

  • l - move cursor right

  • H - move to top of screen

  • M - move to middle of screen

  • L - move to bottom of screen

  • w - jump forwards to the start of a word

  • W - jump forwards to the start of a word (words can contain punctuation)

  • e - jump forwards to the end of a word

  • E - jump forwards to the end of a word (words can contain punctuation)

  • b - jump backwards to the start of a word

  • B - jump backwards to the start of a word (words can contain punctuation)

  • % - move to matching character (default supported pairs: '()', '{}', '[]' - use :h matchpairs in vim for more info)

  • 0 - jump to the start of the line

  • ^ - jump to the first non-blank character of the line

  • $ - jump to the end of the line

  • g_ - jump to the last non-blank character of the line

  • gg - go to the first line of the document

  • G - go to the last line of the document

  • 5gg or 5G - go to line 5

  • gd - move to local declaration

  • gD - move to global declaration

  • fx - jump to next occurrence of character x

  • tx - jump to before next occurrence of character x

  • Fx - jump to previous occurence of character x

  • Tx - jump to after previous occurence of character x

  • ; - repeat previous f, t, F or T movement

  • , - repeat previous f, t, F or T movement, backwards

  • } - jump to next paragraph (or function/block, when editing code)

  • { - jump to previous paragraph (or function/block, when editing code)

  • zz - center cursor on screen

  • Ctrl + e - move screen down one line (without moving cursor)

  • Ctrl + y - move screen up one line (without moving cursor)

  • Ctrl + b - move back one full screen

  • Ctrl + f - move forward one full screen

  • Ctrl + d - move forward 1/2 a screen

  • Ctrl + u - move back 1/2 a screen

Tip Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

Insert mode - inserting/appending text

  • i - insert before the cursor

  • I - insert at the beginning of the line

  • a - insert (append) after the cursor

  • A - insert (append) at the end of the line

  • o - append (open) a new line below the current line

  • O - append (open) a new line above the current line

  • ea - insert (append) at the end of the word

  • Ctrl + h - delete the character before the cursor during insert mode

  • Ctrl + w - delete word before the cursor during insert mode

  • Ctrl + j - begin new line during insert mode

  • Ctrl + t - indent (move right) line one shiftwidth during insert mode

  • Ctrl + d - de-indent (move left) line one shiftwidth during insert mode

  • Ctrl + n - insert (auto-complete) next match before the cursor during insert mode

  • Ctrl + p - insert (auto-complete) previous match before the cursor during insert mode

  • Ctrl + rx - insert the contents of register x

  • Esc - exit insert mode

Editing

  • r - replace a single character

  • J - join line below to the current one with one space in between

  • gJ - join line below to the current one without space in between

  • gwip - reflow paragraph

  • g~ - switch case up to motion

  • gu - change to lowercase up to motion

  • gU - change to uppercase up to motion

  • cc - change (replace) entire line

  • C - change (replace) to the end of the line

  • c$ - change (replace) to the end of the line

  • ciw - change (replace) entire word

  • cw - change (replace) to the end of the word

  • s - delete character and substitute text

  • S - delete line and substitute text (same as cc)

  • xp - transpose two letters (delete and paste)

  • u - undo

  • U - restore (undo) last changed line

  • Ctrl + r - redo

  • . - repeat last command

Marking text (visual mode)

  • v - start visual mode, mark lines, then do a command (like y-yank)

  • V - start linewise visual mode

  • o - move to other end of marked area

  • Ctrl + v - start visual block mode

  • O - move to other corner of block

  • aw - mark a word

  • ab - a block with ()

  • aB - a block with {}

  • at - a block with <> tags

  • ib - inner block with ()

  • iB - inner block with {}

  • it - inner block with <> tags

  • Esc - exit visual mode

Tip Instead of b or B one can also use ( or { respectively.

Visual commands

  • > - shift text right

  • < - shift text left

  • y - yank (copy) marked text

  • d - delete marked text

  • ~ - switch case

  • u - change marked text to lowercase

  • U - change marked text to uppercase

Registers

  • :reg[isters] - show registers content

  • "xy - yank into register x

  • "xp - paste contents of register x

  • "+y - yank into the system clipboard register

  • "+p - paste from the system clipboard register

Tip Registers are being stored in ~/.viminfo, and will be loaded again on next restart of vim.
Tip Special registers:

0 - last yank
" - unnamed register, last delete or yank
% - current file name
# - alternate file name
* - clipboard contents (X11 primary)
+ - clipboard contents (X11 clipboard)
/ - last search pattern
: - last command-line
. - last inserted text
- - last small (less than a line) delete
= - expression register
_ - black hole register

Marks and positions

  • :marks - list of marks

  • ma - set current position for mark A

  • `a - jump to position of mark A

  • y`a - yank text to position of mark A

  • `0 - go to the position where Vim was previously exited

  • `" - go to the position when last editing this file

  • `. - go to the position of the last change in this file

  • `` - go to the position before the last jump

  • :ju[mps] - list of jumps

  • Ctrl + i - go to newer position in jump list

  • Ctrl + o - go to older position in jump list

  • :changes - list of changes

  • g, - go to newer position in change list

  • g; - go to older position in change list

  • Ctrl + ] - jump to the tag under cursor

Tip To jump to a mark you can either use a backtick (`) or an apostrophe ('). Using an apostrophe jumps to the beginning (first non-black) of the line holding the mark.

Macros

  • qa - record macro a

  • q - stop recording macro

  • @a - run macro a

  • @@ - rerun last run macro

Cut and paste

  • yy - yank (copy) a line

  • 2yy - yank (copy) 2 lines

  • yw - yank (copy) the characters of the word from the cursor position to the start of the next word

  • y$ - yank (copy) to end of line

  • p - put (paste) the clipboard after cursor

  • P - put (paste) before cursor

  • dd - delete (cut) a line

  • 2dd - delete (cut) 2 lines

  • dw - delete (cut) the characters of the word from the cursor position to the start of the next word

  • D - delete (cut) to the end of the line

  • d$ - delete (cut) to the end of the line

  • x - delete (cut) character

Indent text

  • >> - indent (move right) line one shiftwidth

  • << - de-indent (move left) line one shiftwidth

  • >% - indent a block with () or {} (cursor on brace)

  • >ib - indent inner block with ()

  • >at - indent a block with <> tags

  • 3== - re-indent 3 lines

  • =% - re-indent a block with () or {} (cursor on brace)

  • =iB - re-indent inner block with {}

  • gg=G - re-indent entire buffer

  • ]p - paste and adjust indent to current line

Exiting

  • :w - write (save) the file, but don't exit

  • :w !sudo tee % - write out the current file using sudo

  • :wq or :x or ZZ - write (save) and quit

  • :q - quit (fails if there are unsaved changes)

  • :q! or ZQ - quit and throw away unsaved changes

  • :wqa - write (save) and quit on all tabs

Search and replace

  • /pattern - search for pattern

  • ?pattern - search backward for pattern

  • \vpattern - 'very magic' pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)

  • n - repeat search in same direction

  • N - repeat search in opposite direction

  • :%s/old/new/g - replace all old with new throughout file

  • :%s/old/new/gc - replace all old with new throughout file with confirmations

  • :noh[lsearch] - remove highlighting of search matches

Search in multiple files

  • :vim[grep] /pattern/ {`{file}`} - search for pattern in multiple files

e.g. :vim[grep] /foo/ **/*
  • :cn[ext] - jump to the next match

  • :cp[revious] - jump to the previous match

  • :cope[n] - open a window containing the list of matches

  • :ccl[ose] - close the quickfix window

Tabs

  • :tabnew or :tabnew {page.words.file} - open a file in a new tab

  • Ctrl + wT - move the current split window into its own tab

  • gt or :tabn[ext] - move to the next tab

  • gT or :tabp[revious] - move to the previous tab

  • #gt - move to tab number #

  • :tabm[ove] # - move current tab to the #th position (indexed from 0)

  • :tabc[lose] - close the current tab and all its windows

  • :tabo[nly] - close all tabs except for the current one

  • :tabdo command - run the command on all tabs (e.g. :tabdo q - closes all opened tabs)

Working with multiple files

  • :e[dit] file - edit a file in a new buffer

  • :bn[ext] - go to the next buffer

  • :bp[revious] - go to the previous buffer

  • :bd[elete] - delete a buffer (close a file)

  • :b[uffer]# - go to a buffer by index #

  • :b[uffer] file - go to a buffer by file

  • :ls or :buffers - list all open buffers

  • :sp[lit] file - open a file in a new buffer and split window

  • :vs[plit] file - open a file in a new buffer and vertically split window

  • :vert[ical] ba[ll] - edit all buffers as vertical windows

  • :tab ba[ll] - edit all buffers as tabs

  • Ctrl + ws - split window

  • Ctrl + wv - split window vertically

  • Ctrl + ww - switch windows

  • Ctrl + wq - quit a window

  • Ctrl + wx - exchange current window with next one

  • Ctrl + w= - make all windows equal height & width

  • Ctrl + wh - move cursor to the left window (vertical split)

  • Ctrl + wl - move cursor to the right window (vertical split)

  • Ctrl + wj - move cursor to the window below (horizontal split)

  • Ctrl + wk - move cursor to the window above (horizontal split)

Diff

  • zf - manually define a fold up to motion

  • zd - delete fold under the cursor

  • za - toggle fold under the cursor

  • zo - open fold under the cursor

  • zc - close fold under the cursor

  • zr - reduce (open) all folds by one level

  • zm - fold more (close) all folds by one level

  • zi - toggle folding functionality

  • ]c - jump to start of next change

  • [c - jump to start of previous change

  • do or :diffg[et] - obtain (get) difference (from other buffer)

  • dp or :diffpu[t] - put difference (to other buffer)

  • :diffthis - make current window part of diff

  • :dif[fupdate] - update differences

  • :diffo[ff] - switch off diff mode for current window

Tip The commands for folding (e.g. za) operate on one level. To operate on all levels, use uppercase letters (e.g. zA).

Tip To view the differences of files, one can directly start Vim in diff mode by running vimdiff in a terminal. One can even set this as git difftool.



来自  https://vim.rtorr.com/




普通分类: