<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>vim on Digi Hunch</title><link>https://static.digihunch.com/tag/vim/</link><description>Recent content in vim on Digi Hunch</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Sat, 20 Jul 2024 16:36:56 -0400</lastBuildDate><atom:link href="https://static.digihunch.com/tag/vim/index.xml" rel="self" type="application/rss+xml"/><item><title>Personal Vim cheatsheet</title><link>https://static.digihunch.com/2019/10/personal-vim-cheatsheet/</link><pubDate>Mon, 07 Oct 2019 17:52:00 -0400</pubDate><guid>https://static.digihunch.com/2019/10/personal-vim-cheatsheet/</guid><description>&lt;p class="wp-block-paragraph"&gt;This is my personal cheatsheet as intermediate Vim user so I skipped the ones that I consider basic. All the commands listed are used in command mode for fast editing.&lt;/p&gt;&#10;&lt;h4 class="wp-block-heading"&gt;Command execution&lt;/h4&gt;&#10;&lt;p class="wp-block-paragraph"&gt;In command mode, use colon to start ex command. Here are some examples of ex commands:&lt;/p&gt;&#10;&lt;ul class="wp-block-list"&gt;&lt;li&gt;e: edit file&lt;/li&gt;&lt;li&gt;g: global command&lt;/li&gt;&lt;li&gt;q: quit&lt;/li&gt;&lt;li&gt;w: write&lt;/li&gt;&lt;li&gt;s: substitute&lt;/li&gt;&lt;/ul&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The commands (g)lobal and (s)ubstitute are heavily used in string manipulation. The rest are summarized here:&lt;/p&gt;&#10;&lt;figure class="wp-block-table is-style-stripes"&gt;&lt;table class="has-background" style="background-color:#e9fbe5"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;:e!&lt;/td&gt;&lt;td&gt;reload current file discarding all unsaved changes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:e newfile.txt&lt;/td&gt;&lt;td&gt;open file newfile.txt for editing &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:e .&lt;/td&gt;&lt;td&gt;load current directory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:w!&lt;/td&gt;&lt;td&gt;force write (if permission allows)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;shift + zz&lt;/td&gt;&lt;td&gt;equivalent to :wq!&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;shift + zq&lt;/td&gt;&lt;td&gt;equivalent to :q!&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/figure&gt;&#10;&lt;h4 class="wp-block-heading"&gt;Operator&lt;/h4&gt;&#10;&lt;figure class="wp-block-table is-style-stripes"&gt;&lt;table class="has-background" style="background-color:#e9fbe5"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;.&lt;/td&gt;&lt;td&gt;repeat last operation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;fx&lt;/td&gt;&lt;td&gt;find next character x on the same line&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/figure&gt;&#10;&lt;h4 class="wp-block-heading"&gt;Text Editing&lt;/h4&gt;&#10;&lt;p class="wp-block-paragraph"&gt;y for yank(copy), i for inside, a for around, d for delete, w for word, p for paragraph or paste. Examples:&lt;/p&gt;&#10;&lt;figure class="wp-block-table is-style-stripes"&gt;&lt;table class="has-background" style="background-color:#e9fbe5"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;yyp&lt;/td&gt;&lt;td&gt;copy current line and insert after&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;diw&lt;/td&gt;&lt;td&gt;delete the entire word where the cursor sits in (dw deletes from cursor to end of word; db deletes from cursor to beginning of word)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;shift + V&lt;/td&gt;&lt;td&gt;select entire line in visual mode (v selects character in visual mode) &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&amp;gt;&lt;/td&gt;&lt;td&gt;visual mode: indent twice on all selected lines&lt;br&gt;edit mode: indent once for 2 lines&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;3&amp;lt;&lt;/td&gt;&lt;td&gt;visual mode: outdent three times on all selected lines&lt;br&gt;edit mode: outdent once for 3 lines&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;di&amp;#8221;&lt;/td&gt;&lt;td&gt;delete everything between the double quotes surrounding the cursor (exclusive); use c instead of d to finish the same effect with insert mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;di&amp;gt;&lt;/td&gt;&lt;td&gt;delete everything between &amp;lt; and &amp;gt; surrounding the cursor (exclusive); use (c)hange instead of (d)elete to finish the same effect with insert mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;dit&lt;/td&gt;&lt;td&gt;delete everything between tags. e.g. &amp;lt;xml&amp;gt;contenttodelete&amp;lt;/xml&amp;gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;dip&lt;/td&gt;&lt;td&gt;delete the entire paragraph&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;da&amp;#8217;&lt;/td&gt;&lt;td&gt;delete everything between the single quote surrounding the cursor (inclusive); use c instead of d to finish the same effect with insert mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;da}&lt;/td&gt;&lt;td&gt;delete everything between { and } surrounding the cursor (inclusive); use c instead of d to finish the same effect with insert mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;dt.&lt;/td&gt;&lt;td&gt;delete all characters until the next .&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;i&lt;/td&gt;&lt;td&gt;insert at cursor location&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;shift + I&lt;/td&gt;&lt;td&gt;move cursor to first non-blank character of line and start in insert mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;a&lt;/td&gt;&lt;td&gt;insert at the location next to cursor&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;shift + A&lt;/td&gt;&lt;td&gt;move cursor to last non-blank character of line and start in insert mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;move to beginning of line. ^ moves to first non-blank character in the line. $ moves to the end of line&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Note: wherever d is used in this table, c can be used instead for the same effect but switch to editing mode at the end.&lt;/p&gt;&#10;&lt;h4 class="wp-block-heading"&gt;String Manipulation&lt;/h4&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The general patterns are:&lt;/p&gt;&#10;&lt;ul class="wp-block-list"&gt;&lt;li&gt;[range]g/pattern/cmd&lt;/li&gt;&lt;li&gt;[range]s/match/replacement/option&lt;/li&gt;&lt;/ul&gt;&#10;&lt;p class="wp-block-paragraph"&gt;If range is not specified, it applies to current line only! To specify the whole file, use % range. You may also specify line range such as &amp;#8220;10,20&amp;#8221;. Here are some examples: &lt;/p&gt;&#10;&lt;figure class="wp-block-table is-style-stripes"&gt;&lt;table class="has-background" style="background-color:#e9fbe5"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;:%s/bacon/lettuce&lt;/td&gt;&lt;td&gt;For every line of the file, replace the first occurrence of bacon in each line to lettuce&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:%s/bacon/lettuce/g&lt;/td&gt;&lt;td&gt;For every line of the file, replace all occurrences of bacon to lettuce&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:s/bacon/lettuce&lt;/td&gt;&lt;td&gt;For current line, replace the first occurrence of bacon to lettuce&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:s/bacon/lettuce/gi&lt;/td&gt;&lt;td&gt;For current line, replace all occurrences of bacon to lettuce, case insensitive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:g/bacon/d&lt;/td&gt;&lt;td&gt;delete all lines that contain pattern &amp;#8216;bacon&amp;#8217;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:g!/lettuce/d&lt;/td&gt;&lt;td&gt;delete all lines that do not contain pattern &amp;#8216;lettice&amp;#8217;; or use :v/lettuce/d instead&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;:g/^\s*$/d&lt;/td&gt;&lt;td&gt;delete all blank lines. \s* represents zero or more white spaces&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/figure&gt;&#10;&lt;h4 class="wp-block-heading"&gt;Bookmarking&lt;/h4&gt;&#10;&lt;figure class="wp-block-table is-style-stripes"&gt;&lt;table class="has-background" style="background-color:#e9fbe5"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;ma&lt;/td&gt;&lt;td&gt;mark cursor line as bookmark a&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;`a&lt;/td&gt;&lt;td&gt;jump to cursor position at line a&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;#8216;a&lt;/td&gt;&lt;td&gt;jump to beginning of line a&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;`.&lt;/td&gt;&lt;td&gt;jump to last line where change occurred&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;#8220;&lt;/td&gt;&lt;td&gt;jump back &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/figure&gt;&#10;&lt;h4 class="wp-block-heading"&gt;Insert Mode&lt;/h4&gt;&#10;&lt;figure class="wp-block-table is-style-stripes"&gt;&lt;table class="has-background" style="background-color:#e9fbe5"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Ctrl + N&lt;/td&gt;&lt;td&gt;Auto complete&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/figure&gt;&#10;&lt;h4 class="wp-block-heading"&gt;Edit and run&lt;/h4&gt;&#10;&lt;p class="wp-block-paragraph"&gt;While tmux and screen can help split screen in Bash, we sometimes need to split a bash screen to run a quick command when we&amp;#8217;re already in vim. This can be done with some simple commands.&lt;/p&gt;&#10;&lt;figure class="wp-block-table is-style-stripes"&gt;&lt;table class="has-background" style="background-color:#e9fbe5"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;:term&lt;/td&gt;&lt;td&gt;Open up a terminal above vim. You can also spell :ter or :terminal&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ctrl+W; Ctrl+W&lt;/td&gt;&lt;td&gt;Press Ctrl+W twice can help you toggle between the terminal and vim buffer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ctrl+D&lt;/td&gt;&lt;td&gt;Close the terminal&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;This can be very helpful when you are debugging code and need it run repeatedly. You don&amp;#8217;t need to exit vim just to run a command and come back. Note that while you&amp;#8217;re in terminal, you can&amp;#8217;t use Ctrl+W as a shortcut key to backspace a word. Use Alt + Delete instead.&lt;/p&gt;&#10;&lt;nav class="wp-post-navigation" aria-label="Post navigation"&gt;&#10;&lt;a rel="prev" href="https://static.digihunch.com/2019/09/cryptographic-concepts-for-busy-it-professionals-2-of-2/"&gt;&lt;span class="wp-post-navigation-label"&gt;Previous Post&lt;/span&gt;&lt;strong class="wp-post-navigation-title"&gt;Cryptography basics 2 of 2&lt;/strong&gt;&lt;/a&gt;&#10;&lt;a rel="next" href="https://static.digihunch.com/2019/10/storage-nitty-gritty-4-of-5-backup-and-archive-solutions/"&gt;&lt;span class="wp-post-navigation-label"&gt;Next Post&lt;/span&gt;&lt;strong class="wp-post-navigation-title"&gt;Storage Nitty-Gritty 4 of 5 – Backup and Archive Solutions&lt;/strong&gt;&lt;/a&gt;&#10;&lt;/nav&gt;&#10;</description></item></channel></rss>