{"id":125,"date":"2009-04-06T07:46:57","date_gmt":"2009-04-06T07:46:57","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2009\/04\/06\/vi-vim-editor-commands\/"},"modified":"2017-12-26T01:58:10","modified_gmt":"2017-12-26T01:58:10","slug":"vi-vim-editor-commands","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/vi-vim-editor-commands\/","title":{"rendered":"VI\/VIM editor Commands | VI\/VIM editor commands reference | Cheat Sheet"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4018 aligncenter\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2009\/04\/vi-vim-editor-commands.png\" alt=\"vi-vim-editor-commands\" width=\"600\" height=\"400\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2009\/04\/vi-vim-editor-commands.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2009\/04\/vi-vim-editor-commands-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>what will happen ?<\/b><\/span><\/li>\n<\/ul>\n<p>&#8220;man ls |col -b &gt; &#8221; will give the text file same as man ls.<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>what is VI-editor ?<\/b><\/span><\/li>\n<\/ul>\n<p>While in vi you can run AIX commands without exiting the editing session. The! creates a<br \/>\nshell to execute the command that follows.<br \/>\n1. :!ls will create a shell<br \/>\n2. All files in the current directory are listed. Press return to exit the shell and return to the<br \/>\nvi session or&#8230;<br \/>\n3. While still in command mode, issue the :r snacks command<br \/>\n4. The contents of snacks, in this case, are read into the vi file. By default, it will appear<br \/>\nafter the current line.<br \/>\nIf you need to run a series of commands without returning to vi after the first command is<br \/>\nexecuted, enter :sh. When you have run all the commands, press to exit the shell<br \/>\nand return to vi.<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>VI Options:-<\/b><\/span><\/li>\n<\/ul>\n<p>vi has many modes of operation. Some of these will affect the way text is presented, while<br \/>\nothers will make editing easier for novice users.<br \/>\n:set all display all settings<br \/>\n:set display settings different than the default<br \/>\n:set ai sets autoindent on<br \/>\n:set noai turns autoindent mode off<br \/>\n:set nu enables line numbers<br \/>\n:set nonu turns line numbers off<br \/>\n:set list displays non-printable characters<br \/>\n:set nolist hides non-printable characters<br \/>\n:set showmode shows the current mode of operation<br \/>\n:set noshowmode hides mode of operation<br \/>\n:set ts=4 sets tabs to 4-character jumps<br \/>\n:set ic ignores case sensitivity<br \/>\n:set noic case sensitive<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b> Search<\/b><\/span><\/li>\n<\/ul>\n<p>\/word \u00a0\u00a0 \u00a0Search \u201cword\u201d from top to bottom<br \/>\n?word \u00a0\u00a0 \u00a0Search \u201cword\u201d from bottom to top<br \/>\n\/jo[ha]n \u00a0\u00a0 \u00a0Search \u201cjohn\u201d or \u201cjoan\u201d<br \/>\n\/\\&lt; the \u00a0\u00a0 \u00a0Search \u201cthe\u201d, \u201ctheatre\u201d or \u201cthen\u201d<br \/>\n\/the\\&gt; \u00a0\u00a0 \u00a0Search \u201cthe\u201d or \u201cbreathe\u201d<br \/>\n\/\\ \u00a0\u00a0 \u00a0Search \u201cthe\u201d<br \/>\n\/\\ \u00a0\u00a0 \u00a0Search all words of 4 letters<br \/>\n\/\\\/ \u00a0\u00a0 \u00a0Search \u201cfred\u201d but not \u201calfred\u201d or \u201cfrederick\u201d<br \/>\n\/fred\\|joe \u00a0\u00a0 \u00a0Search \u201cfred\u201d or \u201cjoe\u201d<br \/>\n\/\\ \u00a0\u00a0 \u00a0Search exactly 4 digits<br \/>\n\/^\\n\\{3} \u00a0\u00a0 \u00a0Find 3 empty lines<br \/>\n:bufdo \/searchstr\/ \u00a0\u00a0 \u00a0Search in all open files<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Replace<\/b><\/span><\/li>\n<\/ul>\n<p>:%s\/old\/new\/g \u00a0\u00a0 \u00a0Replace all occurences of \u201cold\u201d by \u201cnew\u201d in file<br \/>\n:%s\/old\/new\/gw \u00a0\u00a0 \u00a0Replace all occurences with confirmation<br \/>\n:2,35s\/old\/new\/g \u00a0\u00a0 \u00a0Replace all occurences between lines 2 and 35<br \/>\n:5,$s\/old\/new\/g \u00a0\u00a0 \u00a0Replace all occurences from line 5 to EOF<br \/>\n:%s\/^\/hello\/g \u00a0\u00a0 \u00a0Replace the begining of each line by \u201chello\u201d<br \/>\n:%s\/$\/Harry\/g \u00a0\u00a0 \u00a0Replace the end of each line by \u201cHarry\u201d<br \/>\n:%s\/onward\/forward\/gi \u00a0\u00a0 \u00a0Replace \u201conward\u201d by \u201cforward\u201d , case unsensitive<br \/>\n:%s\/ *$\/\/g \u00a0\u00a0 \u00a0Delete all white spaces<br \/>\n:g\/string\/d \u00a0\u00a0 \u00a0Delete all lines containing \u201cstring\u201d<br \/>\n:v\/string\/d \u00a0\u00a0 \u00a0Delete all lines containing which didn&#8217;t contain \u201cstring\u201d<br \/>\n:s\/Bill\/Steve\/ \u00a0\u00a0 \u00a0Replace the first occurence of \u201cBill\u201d by \u201cSteve\u201d in current line<br \/>\n:s\/Bill\/Steve\/g \u00a0\u00a0 \u00a0Replace \u201cBill\u201d by \u201cSteve\u201d in current line<br \/>\n:%s\/Bill\/Steve\/g \u00a0\u00a0 \u00a0Replace \u201cBill\u201d by \u201cSteve\u201d in all the file<br \/>\n:%s\/\\r\/\/g \u00a0\u00a0 \u00a0Delete DOS carriage returns (^M)<br \/>\n:%s\/\\r\/\\r\/g \u00a0\u00a0 \u00a0Transform DOS carriage returns in returns<br \/>\n:%s#]\\+&gt;##g \u00a0\u00a0 \u00a0Delete HTML tags but keeps text<br \/>\n:%s\/^\\(.*\\)\\n\\1$\/\\1\/ \u00a0\u00a0 \u00a0Delete lines which appears twice<br \/>\nCtrl+a \u00a0\u00a0 \u00a0Increment number under the cursor<br \/>\nCtrl+x \u00a0\u00a0 \u00a0Decrement number under cursor<br \/>\nggVGg? \u00a0\u00a0 \u00a0Change text to Rot13<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Case<\/b><\/span><\/li>\n<\/ul>\n<p>Vu \u00a0\u00a0 \u00a0Lowercase line<br \/>\nVU \u00a0\u00a0 \u00a0Uppercase line<br \/>\ng~~ \u00a0\u00a0 \u00a0Invert case<br \/>\nvEU \u00a0\u00a0 \u00a0Switch word to uppercase<br \/>\nvE~ \u00a0\u00a0 \u00a0Modify word case<br \/>\nggguG \u00a0\u00a0 \u00a0Set all text to lowercase<br \/>\n:set ignorecase \u00a0\u00a0 \u00a0Ignore case in searches<br \/>\n:set smartcase \u00a0\u00a0 \u00a0Ignore case in searches excepted if an uppercase letter is used<br \/>\n:%s\/\\&lt;.\/\\u&amp;\/g \u00a0\u00a0 \u00a0Sets first letter of each word to uppercase<br \/>\n:%s\/\\&lt;.\/\\l&amp;\/g \u00a0\u00a0 \u00a0Sets first letter of each word to lowercase<br \/>\n:%s\/.*\/\\u&amp; \u00a0\u00a0 \u00a0Sets first letter of each line to uppercase<br \/>\n:%s\/.*\/\\l&amp; \u00a0\u00a0 \u00a0Sets first letter of each line to lowercase<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Read\/Write files<\/b><\/span><\/li>\n<\/ul>\n<p>:1,10 w outfile \u00a0\u00a0 \u00a0Saves lines 1 to 10 in outfile<br \/>\n:1,10 w &gt;&gt; outfile \u00a0\u00a0 \u00a0Appends lines 1 to 10 to outfile<br \/>\n:r infile \u00a0\u00a0 \u00a0Insert the content of infile<br \/>\n:23r infile \u00a0\u00a0 \u00a0Insert the content of infile under line 23<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>File explorer<\/b><\/span><\/li>\n<\/ul>\n<p>:e . \u00a0\u00a0 \u00a0Open integrated file explorer<br \/>\n:Sex \u00a0\u00a0 \u00a0Split window and open integrated file explorer<br \/>\n:browse e \u00a0\u00a0 \u00a0Graphical file explorer<br \/>\n:ls \u00a0\u00a0 \u00a0List buffers<br \/>\n:cd .. \u00a0\u00a0 \u00a0Move to parent directory<br \/>\n:args \u00a0\u00a0 \u00a0List files<br \/>\n:args *.php \u00a0\u00a0 \u00a0Open file list<br \/>\n:grep expression *.php \u00a0\u00a0 \u00a0Returns a list of .php files contening expression<br \/>\ngf \u00a0\u00a0 \u00a0Open file name under cursor<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Interact with Unix<\/b><\/span><\/li>\n<\/ul>\n<p>:!pwd \u00a0\u00a0 \u00a0Execute the \u201cpwd\u201d unix command, then returns to Vi<br \/>\n!!pwd \u00a0\u00a0 \u00a0Execute the \u201cpwd\u201d unix command and insert output in file<br \/>\n:sh \u00a0\u00a0 \u00a0Temporary returns to Unix<br \/>\n$exit \u00a0\u00a0 \u00a0Retourns to Vi<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Alignment<\/b><\/span><\/li>\n<\/ul>\n<p>:%!fmt \u00a0\u00a0 \u00a0Align all lines<br \/>\n!}fmt \u00a0\u00a0 \u00a0Align all lines at the current position<br \/>\n5!!fmt \u00a0\u00a0 \u00a0Align the next 5 lines<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Tabs<\/b><\/span><\/li>\n<\/ul>\n<p>:tabnew \u00a0\u00a0 \u00a0Creates a new tab<br \/>\ngt \u00a0\u00a0 \u00a0Show next tab<br \/>\n:tabfirst \u00a0\u00a0 \u00a0Show first tab<br \/>\n:tablast \u00a0\u00a0 \u00a0Show last tab<br \/>\n:tabm n(position) \u00a0\u00a0 \u00a0Rearrange tabs<br \/>\n:tabdo %s\/foo\/bar\/g \u00a0\u00a0 \u00a0Execute a command in all tabs<br \/>\n:tab ball \u00a0\u00a0 \u00a0Puts all open files in tabs<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Window spliting<\/b><\/span><\/li>\n<\/ul>\n<p>:e filename \u00a0\u00a0 \u00a0Edit filename in current window<br \/>\n:split filename \u00a0\u00a0 \u00a0Split the window and open filename<br \/>\nctrl-w up arrow \u00a0\u00a0 \u00a0Puts cursor in top window<br \/>\nctrl-w ctrl-w \u00a0\u00a0 \u00a0Puts cursor in next window<br \/>\nctrl-w_ \u00a0\u00a0 \u00a0Maximise current window<br \/>\nctrl-w= \u00a0\u00a0 \u00a0Gives the same size to all windows<br \/>\n10 ctrl-w+ \u00a0\u00a0 \u00a0Add 10 lines to current window<br \/>\n:vsplit file \u00a0\u00a0 \u00a0Split window vertically<br \/>\n:sview file \u00a0\u00a0 \u00a0Same as :split in readonly mode<br \/>\n:hide \u00a0\u00a0 \u00a0Close current window<br \/>\n:only \u00a0\u00a0 \u00a0Close all windows, excepted current<br \/>\n:b 2 \u00a0\u00a0 \u00a0Open #2 in this window<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Auto-completion<\/b><\/span><\/li>\n<\/ul>\n<p>Ctrl+n Ctrl+p (in insert mode) \u00a0\u00a0 \u00a0Complete word<br \/>\nCtrl+x Ctrl+l \u00a0\u00a0 \u00a0Complete line<br \/>\n:set dictionary=dict \u00a0\u00a0 \u00a0Define dict as a dictionnary<br \/>\nCtrl+x Ctrl+k \u00a0\u00a0 \u00a0Complete with dictionnary<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Marks<\/b><\/span><\/li>\n<\/ul>\n<p>mk \u00a0\u00a0 \u00a0Marks current position as k<br \/>\n\u2018k \u00a0\u00a0 \u00a0Moves cursor to mark k<br \/>\nd\u2019k \u00a0\u00a0 \u00a0Delete all until mark k<br \/>\nAbbreviations<br \/>\n:ab pr printf(&#8220;This is a Demo Ver \\n&#8221;);\u00a0\u00a0 \u00a0Define pr as abbreviation of printf(&#8220;This is a Demo Ver \\n&#8221;);<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Text indent<\/b><\/span><\/li>\n<\/ul>\n<p>:set autoindent \u00a0\u00a0 \u00a0Turn on auto-indent<br \/>\n:set smartindent \u00a0\u00a0 \u00a0Turn on intelligent auto-indent<br \/>\n:set shiftwidth=4 \u00a0\u00a0 \u00a0Defines 4 spaces as indent size<br \/>\nctrl-t, ctrl-d \u00a0\u00a0 \u00a0Indent\/un-indent in insert mode<br \/>\n&gt;&gt; \u00a0\u00a0 \u00a0Indent<br \/>\n&lt;&lt; \u00a0\u00a0 \u00a0Un-indent<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Syntax highlighting<\/b><\/span><\/li>\n<\/ul>\n<p>:syntax on \u00a0\u00a0 \u00a0Turn on syntax highlighting<br \/>\n:syntax off \u00a0\u00a0 \u00a0Turn off syntax highlighting<br \/>\n:set syntax=perl \u00a0\u00a0 \u00a0Force syntax highlighting<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>How to Exit<\/b><\/span><\/li>\n<\/ul>\n<p>:q[uit] \u00a0\u00a0 \u00a0Quit Vim. This fails when changes have been made.<br \/>\n:q[uit]! \u00a0\u00a0 \u00a0Quit without writing.<br \/>\n:cq[uit] \u00a0\u00a0 \u00a0Quit always, without writing.<br \/>\n:wq \u00a0\u00a0 \u00a0Write the current file and exit.<br \/>\n:wq! \u00a0\u00a0 \u00a0Write the current file and exit always.<br \/>\n:wq {file} \u00a0\u00a0 \u00a0Write to {file}. Exit if not editing the last<br \/>\n:wq! {file} \u00a0\u00a0 \u00a0Write to {file} and exit always.<br \/>\n:[range]wq[!] \u00a0\u00a0 \u00a0[file] Same as above, but only write the lines in [range].<br \/>\nZZ \u00a0\u00a0 \u00a0Write current file, if modified, and exit.<br \/>\nZQ \u00a0\u00a0 \u00a0Quit current file and exit (same as &#8220;:q!&#8221;).<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Editing a File<\/b><\/span><\/li>\n<\/ul>\n<p>:e[dit] \u00a0\u00a0 \u00a0Edit the current file. This is useful to re-edit the current file, when it has been changed outside \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0of Vim.<br \/>\n:e[dit]! \u00a0\u00a0 \u00a0Edit the current file always. Discard any changes to the current buffer. This is useful if you want \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0to start all over again.<br \/>\n:e[dit] {file} \u00a0\u00a0 \u00a0Edit {file}.<br \/>\n:e[dit]! {file} \u00a0\u00a0 \u00a0Edit {file} always. Discard any changes to the current buffer.<br \/>\ngf \u00a0\u00a0 \u00a0Edit the file whose name is under or after the cursor. Mnemonic: &#8220;goto file&#8221;.<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Inserting Text<\/b><\/span><\/li>\n<\/ul>\n<p>a \u00a0\u00a0 \u00a0Append text after the cursor [count] times.<br \/>\nA \u00a0\u00a0 \u00a0Append text at the end of the line [count] times.<br \/>\ni \u00a0\u00a0 \u00a0Insert text before the cursor [count] times.<br \/>\nI \u00a0\u00a0 \u00a0Insert text before the first non-blank in the line [count] times.<br \/>\ngI \u00a0\u00a0 \u00a0Insert text in column 1 [count] times.<br \/>\no \u00a0\u00a0 \u00a0Begin a new line below the cursor and insert text, repeat [count] times.<br \/>\nO \u00a0\u00a0 \u00a0Begin a new line above the cursor and insert text, repeat [count] times.<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><b>Inserting a file<\/b><\/span><\/li>\n<\/ul>\n<p>:r[ead] [name] \u00a0\u00a0 \u00a0Insert the file [name] below the cursor.<br \/>\n:r[ead] !{cmd} \u00a0\u00a0 \u00a0Execute {cmd} and insert its standard output below the cursor.<br \/>\n<del>\u00a0<\/del><\/p>\n","protected":false},"excerpt":{"rendered":"<p>what will happen ? &#8220;man ls |col -b &gt; &#8221; will give the text file same as man ls. what is VI-editor ? While in vi you can run AIX&#8230; <\/p>\n","protected":false},"author":1,"featured_media":4018,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[63],"tags":[3066,378,3059,463,3065,3060,3064,3075,3078,3061,3085,3067,3069,3068,3063,3082,3073,3084,3079,3072,3071,3070,3081,3076,3080,3083,3074,3077,459,3062],"class_list":["post-125","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ides-and-editor","tag-all-features-commands-in-vi-vim-editor","tag-commands","tag-editor","tag-vi","tag-vi-and-vim-tutorial","tag-vi-vim-editor","tag-vi-vim-editor-advantages","tag-vi-vim-editor-alignment-command","tag-vi-vim-editor-auto-completion-command","tag-vi-vim-editor-commands","tag-vi-vim-editor-commands-cheatsheet","tag-vi-vim-editor-commands-guide","tag-vi-vim-editor-commands-reference","tag-vi-vim-editor-commands-tutorial","tag-vi-vim-editor-features","tag-vi-vim-editor-file-editing-command","tag-vi-vim-editor-file-explorer-command","tag-vi-vim-editor-file-inserting-command","tag-vi-vim-editor-marks-command","tag-vi-vim-editor-read-write-files-command","tag-vi-vim-editor-replace-command","tag-vi-vim-editor-search-command","tag-vi-vim-editor-syntax-highlighting-command","tag-vi-vim-editor-tabs-command","tag-vi-vim-editor-text-indent-command","tag-vi-vim-editor-text-inserting-command","tag-vi-vim-editor-unix-interaction-command","tag-vi-vim-editor-window-spliting-command","tag-vim","tag-what-is-vi-editor"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":4019,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions\/4019"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/4018"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}