Reload the current document
:e
Search and replace a selected chunk of text
1
| :'<,'>s/default/newtext/g
|
go to closing html tag
% (shift 5)
select everything from the cursor up to line 35.
v35G (v puts you in select mode, 35 specifies the line number that you want to G go to.)
spilt diff
:vert diffsplit native_ours.php
gg=G
reload vimrc
:source ~/.vimrc
Basic movement
h l k j
character left, right; line up, down b w word/token left, right ge e end of word/token left, right { } beginning of previous, next paragraph ( ) beginning of previous, next sentence 0 gm beginning, middle of line ^ $ first, last character of line nG ngg line n, default the last, first n% percentage n of the file (n must be provided) n| column n of current line % match of next brace, bracket, comment, #define nH nL line n from start, bottom of window M middle line of window
Insertion & replace → insert mode
i a insert before, after cursor I A insert at beginning, end of line gI insert text in first column o O open a new line below, above the current line rc replace character under cursor with c grc like r, but without affecting layout R replace characters starting at the cursor gR like R, but without affecting layout cm change text of movement command m cc or S change current line C change to the end of line s change one character and insert ~ switch case and advance cursor g~m switch case of movement command m gum gUm lowercase, uppercase text of movement m
m shift left, right text of movement m n<< n>> shift n lines left, right ## Deletion x X delete character under, before cursor dm delete text of movement command m dd D delete current line, to the end of line J gJ join current line with next, without space :rd↵ delete range r lines :rdx↵ delete range r lines into register x ## Insert mode `^Vc ^Vn` insert char c literally, decimal value n ^A insert previously inserted text ^@ same as ^A and stop insert → command mode ^Rx ^R^Rx insert content of register x, literally ^N ^P text completion before, after cursor ^W delete word before cursor ^U delete all inserted character in current line ^D ^T shift left, right one shift width ^Kc1c2 or c1←c2 enter digraph \c1,c2\ ^Oc execute c in temporary command mode ^X^E ^X^Y scroll up, down or ^[ abandon edition → command mode ## Copying "x use register x for next delete, yank, put :reg↵ show the content of all registers :reg x↵ show the content of registers x ym yank the text of movement command m yy or Y yank current line into register p P put register after, before cursor position ]p [p like p, P with indent adjusted gp gP like p, P leaving cursor after new text ## Advanced insertion g?m perform rot13 encoding on movement m n^A n^X +n, -n to number under cursor gqm format lines of movement m to fixed width :rce w↵ center lines in range r to width w :rle i↵ left align lines in range r with indent i :rri w↵ right align lines in range r to width w !mc↵ filter lines of movement m through command c n!!c↵ filter n lines through command c :r!c↵ filter range r lines through command c ## Visual mode v V ^V start/stop highlighting characters, lines, block o exchange cursor position with start of highlighting gv start highlighting on previous visual area aw as ap select a word, a sentence, a paragraph ab aB select a block ( ), a block { } ## Undoing, repeating & registers u U undo last command, restore last changed line . ^R repeat last changes, redo last undo n. repeat last changes with count replaced by n qc qC record, append typed characters in register c q stop recording @c execute the content of register c @@ repeat previous @ command :@c↵ execute register c as an Ex command :rg/p/c↵ execute Ex command c on range r where pattern p matches ## Complex movement - + line up, down on first non-blank character B W space-separated word left, right gE E end of space-separated word left, right n_ down n-1 line on first non-blank character g0 beginning of screen line g^ g$ first, last character of screen line gk gj screen line up, down fc Fc next, previous occurence of character c tc Tc before next, previous occurence of c ; , repeat last fFtT, in opposite direction [[ ]] start of section backward, forward [] ][ end of section backward, forward [( ]) unclosed (, ) backward, forward [{ ]} unclosed {, } backward, forward [m ]m start of backward, forward Java method [# ]# unclosed #if, #else, #endif backward, forward [* ]* start, end of /* */ backward, forward ## Search & substitution /s↵ ?s↵ search forward, backward for s /s/o↵ ?s?o↵ search fwd, bwd for s with offset o n or /↵ repeat forward last search N or ?↵ repeat backward last search # * search backward, forward for word under cursor g# g* same, but also find partial matches gd gD local, global definition of symbol under cursor :rs/f/t/x↵ substitute f by t in range r x: g-all occurrences, c-confirm changes :rs x↵ repeat substitution with new r & x ## Special characters in search patterns . ^ $ any single character, start, end of line \< \> start, end of word [c1-c2] a single character in range c1..c2 [^c1-c2] a single character not in range \i \k \I \K an identifier, keyword; excl. digits \f \p \F \P a file name, printable char.; excl. digits \s \S a white space, a non-white space \e \t \r \b , , <↵>, <←> \= * \+ match 0..1, 0..∞, 1..∞ of preceding atoms \| separate two branches ( ≡ or) \( \) group patterns into an atom \& \n the whole matched pattern, nth () group \u \l next character made upper, lowercase \c \C ignore, match case on next pattern ## Offsets in search commands n or +n n line downward in column 1 -n n line upward in column 1 e+n e-n n characters right, left to end of match s+n s-n n characters right, left to start of match ;sc execute search command sc next ## Marks and motions mc mark current position with mark c ∈[a..Z] `c `C go to mark c in current, C in any file `0..9 go to last exit position `` `" go to position before jump, at last edit `[ `] go to start, end of previously operated text :marks↵ print the active marks list :jumps↵ print the jump list n^O go to nth older position in jump list n^I go to nth newer position in jump list ## Key mapping & abbreviations :map c e↵ map c ↦ e in normal & visual mode :map! c e↵ map c ↦ e in insert & cmd-line mode :unmap c↵ :unmap! c↵ remove mapping c :mk f↵ write current mappings, settings... to file f :ab c e↵ add abbreviation for c ↦ e :ab c↵ show abbreviations starting with c :una c↵ remove abbreviation c ## Tags :ta t↵ jump to tag t :nta↵ jump to nth newer tag in list ^] ^T jump to the tag under cursor, return from tag :ts t↵ list matching tags and select one for jump :tj t↵ jump to tag or select one if multiple matches :tags↵ print tag list :npo↵ :n^T↵ jump back from, to nth older tag :tl↵ jump to last matching tag ^W} :pt t↵ preview tag under cursor, tag t ^W] split window and show tag under cursor ^Wz or :pc↵ close tag preview window ## Scrolling & multi-windowing ^E ^Y scroll line up, down ^D ^U scroll half a page up, down ^F ^B scroll page up, down zt or z↵ set current line at top of window zz or z. set current line at center of window zb or z- set current line at bottom of window zh zl scroll one character to the right, left zH zL scroll half a screen to the right, left ^Ws or :split↵ split window in two ^Wn or :new↵ create new empty window ^Wo or :on↵ make current window one on screen ^Wj ^Wk move to window below, above ^Ww ^W^W move to window below, above (wrap) ## Ex commands (↵) :e f edit file f, unless changes have been made :e! f edit file f always (by default reload current) :wn :wN write file and edit next, previous one :n :N edit next, previous file in list :rw write range r to current file :rw f write range r to file f :rw>>f append range r to file f :q :q! quit and confirm, quit and discard changes :wq or :x or ZZ write to current file and exit recall commands starting with current :r f insert content of file f below cursor :r! c insert output of command c below cursor :args display the argument list :rco a :rm a copy, move range r below line a ## Ex ranges , ; separates two lines numbers, set to first line n an absolute line number n . $ the current line, the last line in file % * entire file, visual area 't position of mark t /p/ ?p? the next, previous line where p matches +n -n +n, -n to the preceding line number ## Folding zfm create fold of movement m :rfo create fold for range r zd zE delete fold at cursor, all in window zo zc zO zC open, close one fold; recursively [z ]z move to start, end of current open fold zj zk move down, up to start, end of next fold ## Miscellaneous :sh↵ :!c↵ start shell, execute command c in shell K lookup keyword under cursor with man :make↵ start make, read errors and jump to first :cn↵ :cp↵ display the next, previous error :cl↵ :cf↵ list all errors, read errors from file ^L ^G redraw screen, show filename and position g^G show cursor column, line, and character position ga show ASCII value of character under cursor gf open file which filename is under cursor :redir>f↵ redirect output to file f :mkview [f] save view configuration [to file f] :loadview [f] load view configuration [from file f] ^@ ^K ^_ \ Fn ^Fn unmapped keys ref http://tnerual.eriogerg.free.fr/vimqrc.html 1. i Pressing i in the console gets you to the insert mode. Now you can start typing your texts inside Vim. The text you type will appear in front of the cursor key. 2. a It’s among another commonly used Vim commands you can use to insert text. The texts will be appended right after the cursor. 3. I Want to place your texts at the beginning of the current line? The l command lets you insert text right at your desired location. 4. A This is one of my favorite Vim commands to append text at the end of my current line. It also falls under those Vim commands used for inserting text. 5. o Pressing the lowercase o creates a new line just after the current line you’re in. 6. O The uppercase variant creates the new line before the current line. 7. gf You can use this handy little command to open your files under the cursor. 8. gi One of my most favorite Vim commands, gi restarts the insert mode in the last insert point you used. 9. wq Typing in long strings of text doesn’t matter if you can’t save them for future usage. Enter the wq command in the command mode to save your file and exit Vim. 10. q! Often you’ll find yourself in a position where you’re not satisfied with the texts you have typed and would like to close the editor without saving your contents. Entering q! in the command mode lets you do exactly so. Vim Commands for Moving Around The Console One of the foremost reason behind Vim’s ultra-fast user experience is its fast movement features. Vim features and supports numerous convenient Vim commands to make your life in front of the terminal blazing fast. Below are some of the most useful Vim commands for faster navigation. vim-movement-commands 11. h Use the h command to navigate to the left side of your Vim console. This is analogous to the left arrow of your keyboard. 12. l The l commands help you navigate to the right and is a substitute for the right arrow key. 13. k Use the lowercase k command to move upwards. Similar to the arrow up key. 14. j The j command takes you to downward and is a substitute for the down arrow key. The h j k l keys help you move across the screen much faster than the standard arrow keys. Prefix them with a number to get the most out of these Vim commands. So, for going down ten lines, instead of holding the key for a prolonged period of time, just type 10j. Mastering these passive Vim commands for navigation will enhance your productivity much faster. 15. H This command takes the cursor at the topmost region of your screen. 16. M It will put the cursor in the middle of your Vim screen. 17. L The capital L puts the cursor at the end of the screen. The H M L Vim commands come in handy whenever you want to get to an approximate portion of your console screen. 18. 0 or Entering either 0 or the button will take you to the start of the line. 19. ^ This is among the most commonly used Vim commands to get to the first non-blank character in a line. 20. $ This command will take the cursor at the end of the current line. 21. b The lowercase b command lets you move back by tokens. 22. w This command helps you move forward by tokens. 23. B The uppercase variant of b lets you move back by words. 24. W This command will let you move forward by words. A token is any sequence of digits, letters, or underscores; whereas a word is any non-blank sequence that is separated by whitespace. 25. ctrl+u This Vim command is basically a Page Up command. However, it moves the screen by half and holds onto the current cursor position. 26. ctrl+d The Page Down variant of the above command. Functions in the same way. 27. G Prefix the G command with a number to jump directly to a specific line number. Suppose, if you have line numbering enabled and are writing a shell script, you can go to the 10th line by merely typing 10G. 28. # Given, # is the number of a specific line, entering this command will take you to that line directly. It’s one of the most used Vim commands to jump from line to line. 29. “ This command (two back ticks) takes you wherever you just were. 30. ) You can go-to at the start of the following sentence with this command. 31. ( Go-to at the beginning of the earlier sentence. 32. } Used for going to the beginning of the next text block. 33. { Takes cursor at the beginning of the previous text block. Vim Commands for Editing Texts Vim combines a significant number of powerful yet flexible editing features that lets users edit their documents seamlessly while paving the path to a versatile workflow. The power of Vim comes from the fact that you can combine many of such Vim commands together to solve your custom needs. This section of the guide covers some elementary Vim commands used in everyday text editing. Continue reading to find out more useful Vim commands for editing. favorite vim commands 34. yy The yy command lets you copy an entire line. It’s something you’ll often employ during your time with Vim. 35. yw This is one of the most widely used Vim commands for copying a word across the editor. 36. y$ One of my all-time favorite Vim commands, it gives users the ability to copy texts from the current position of the cursor to the end of a line. 37. v The v command can be used to highlight a single character at a time in combination with the movement keys (arrows/ h j k l). 38. V Almost the same as the above one, but instead of a word, this command will highlight an entire line. 39. p You can paste the contents of your clipboard to the unnamed register with this handy little command. 40. d This is among the most used Vim commands for deleting highlighted texts. 41. dd Want to delete an entire line with a single Vim command? The dd command is specially created for this purpose. 42. dw You can delete a single word pretty quickly with the dw command. 43. D One of the most powerful Vim commands ever, the D command deletes everything from the current cursor location to the end of the line. 44. d0 This command is used for deleting everything from your cursor’s current position to the beginning of the line. 45. dgg You can use this command to delete everything from your cursor’s current position to the beginning of the file. 46. dG This command will wipe out everything from your cursor’s current position to the end of the file. 47. x Use the x command whenever you need to delete a single character. 48. u The u command is among the most widely used Vim commands for many to undo the last operation. Combining with a postfix allows users to undo multiple actions. So, u# undo’s the last # number of actions. 49. ctrl+r Use the above command to redo the last undo operation. 50. . The dot (.) command is one of those useful Vim commands that decreases your workload significantly by repeating the last action whenever you need such functionality. 51. cc You can use the cc command to change line(s) by clearing and entering the insert mode at the same time. The cursor is placed at the current level of indentation. Useful Vim Commands for Replacing Texts Apart from the generic command and insert modes, Vim has another mode called the visual mode. You can highlight and change a particular portion of your text document in this mode. But where’s the fun in that, huh? Using a select set of Vim commands can achieve you the same result, just a bit faster. Below are some of my favorite Vim commands for changing texts on the fly. 52. r The r command is a pretty handy tool when it comes to changing a single character. Follow it with , and it will change the current character under the cursor with . 53. R The capital R opens the insert mode, but instead of inserting texts, you can replace them with this command. 54. ~ The tilda(~) command is pretty useful when you need to change the case of a character in your document. Follow it with a number to flip that many characters. 55. t Enter t to select up to but not including the next on a particular line. 56. f Press f to select up to and including the next on a line. 57. i Want to select everything in between a set of parentheses or some other unique character? Type i to select everything that sits between two consecutive . 58. a This command is identical to the previous one but includes the at both ends of text. Most Used Vim Commands for Searching Through A Document The searching functionality is of greater importance to any versatile Linux text editor. Vim offers exceptionally powerful commands for searching through a document. It’s one of the most powerful areas of this innovative text editor. Glance over the below Vim commands to search through your document effectively. Vim Code Editor 59. / The slash command is the single most used command for searching through huge text files in Vim. Just type / and follow it with the texts you want Vim to search for you and look out at the bottom corner of the console. 60. /\c The \c option, when piped to the search (/) command, lets users search for texts in a case sensitive manner. Wise utilization of this command can save hours of hard work. 61. ? This is among the most useful Vim commands for searching previous texts for a given. 62. n The n command searches in the direction of your last search. Use this command if you know in which direction your search item is. 63. N Almost identical to the above command, but searches in the opposite direction of your last search. 64. :%s/[pattern]/[replacement]/g The above command utilizes regular expression for searching every occurrence of [pattern] and replaces it with [replacement] without asking for confirmation. 65. :%s/[pattern]/[replacement]/gc Same as the previous command but asks for confirmation before replacing each instance of [pattern] with [replacement]. 66. :s/[pattern]/[replacement]/g Instead of replacing every single instance of [pattern] on your file, this Vim command will replace only those [pattern] that are in the current line with [replacement]. 67. :bufdo /[pattern] This is among those powerful Vim commands that let users search for [pattern] in every currently opened buffer. It will enhance your productivity and decrease search time significantly. 68. :g/string/d This is among those useful Vim commands that will come in handy whenever you want to delete every line that contains string from your document. Linux Command Cheat Sheet for Working With Multiple Files in Vim Editing in the terminal is fun and alright. But, where’s the edge of terminal editors if you can’t edit multiple files with them. Luckily, as an iconic text editor, Vim lets you work with multiple documents in a pretty neat way. The ability to split the screen when working with multiple files at the same time is something Vim has been offering even before the concept of the split screen became mainstream. Now, you should slowly start to realize why this legendary editor is hailed in such high regards. vim editor 69. :sp [filename] Use this command to create a new file and split the console screen horizontally in order to show the two different buffers. 70. :vsp [filename] The functionality of this Vim command is, in essence, identical to the above command, but instead of splitting the console horizontally it splits the screen vertically. 71. :bn This Vim command will switch your editor to the next buffer. It’s among those few fundamental Vim commands without which you won’t be able to work with multiple documents in Vim. 72. :bp Identical to the earlier command, but switches to the previous buffer instead of going next. 73. :bd Use this Vim command when closing a specific buffer. Make sure to save your data using appropriate Vim commands. 74. :ls This is one of the handy Vim commands that will present users with a list of all open buffers. 75. ctrl+ws If you want to split your Vim windows horizontally, then this is the command you’re looking for. 76. ctrl+wv Instead of splitting the windows horizontally, this Vim command will split it vertically. 77. ctrl+ww Utilize this command to switch between multiple windows directly from the command mode. 78. ctrl+wq You can use this handy Vim command to quit a particular window. 79. ctrl+wh This command will move the location of your cursor to the left window. 80. ctrl+wl Same as the previous command, but instead of moving the cursor left, this command will point to the right window. 81. ctrl+wj Use this command whenever you want to move a window beneath your existing one. 82. ctrl+wk Same as above, but takes the cursor to the window above the current one. Useful Vim Commands When Working With Multiple Tabs Vim offers a variety of Vim commands to open and navigate a plethora of tabs right from the terminal. You can use these useful Vim commands whenever you are working with multiple files simultaneously. We encourage new Vim users to glance over such commands as they will often come in handy after you’ve mastered the basic Vim commands and start to do heavyweight staffs with it. vim commands in action 83. :tabnew You can use the :tabnew command to create a new tab and work with some other document while not leaving the current file. 84. gt The gt command will show you the next tab opened by you. 85. :tabfirst The above command will show you the first tab you’ve got opened in a particular session. 86. :tablast Same as the previous command, but instead of showing the first tab it will show the last tab. 87. tabm n(position) This powerful Vim command will come in handy whenever you feel the need to re-arrange your existing tabs. 88. tabdo %s/foo/bar/g You can utilize the above command whenever you want to execute a command in every opened tab at the same time. 89. :tab ball This Vim command is one of my favorite Vim commands, and it puts each open files in tabs. ```vim :new abc.txt ``` This is among those Vim commands that lets you open a new file named abc.txt in a new window without exiting the current document. Miscellaneous Vim Commands As Vim offers plenty of Vim commands, we are unable to cover every single one of them categorically. That’s why in this section we’ll overview some useful Vim commands that are used by veteran users for a wide array of purpose. other Vim Commands 91. :w Pressing this Vim command in the command mode saves your current document but do not exist the existing session. 92. :q This command will exit the current session without saving your changes. Note that you will see the E37 error if you have unsaved changes in your document. In such scenarios, you need to override this command and use q! instead. 93. :help [command] The help command performs a search operation on the command you’ve typed and shows relevant information on them right in the console. 94. :e [file] This command will open a file named [file] and create a new one if it doesn’t exist in your filesystem already. 95. :w [filename] Use this command to save your existing document directly into a new file named [filename]. 96. :stop Writing this command in the command mode will suspend your current Vim session. You can also do this by pressing ctrl+z at the same time. 97. :browse e Use this command whenever you want to invoke the graphical file explorer from your Vim console. 98. :%!fmt Writing this command will align each line of your current file. 99. !}fmt Use this whenever you need to align all lines at the present position of your cursor. 100. :set autoindent This is one of the most used Vim commands you will use during your time with Vim. It sets autoindent for your current session.
Comments powered by Disqus.