cabin

Source code for personal website
git clone git://git.dimitrijedobrota.com/cabin.git
Log | Files | Refs

VimShowcase1.md (6356B)


      1 @date: 2021-08-20
      2 @title: Vim Showcase #1 - Text Object
      3 @language: en
      4 @categories: linux, vim
      5 
      6 # Vim Showcase #1 - Text Object
      7  
      8  Welcome to the new series called **"Vim Showcase"**
      9  
     10  In this series, as the title suggests, I am going to showcase vim and all of
     11  its wonderful features as well as plugins>
     12  
     13  Even though I am starting this series off by showcasing plugins, I will try to
     14  cover the vanilla vim as much as possible.
     15  
     16  I restrain myself from using too much plugins as I find vim really usable out
     17  of the box (with some tweaking of course), but sometimes it takes just a
     18  handful of small, lightweight plugins to enhance the vim experience and boost
     19  the writing speed by simplifying repetitive and tedious tasks.
     20  
     21  
     22 ## Introduction
     23  
     24  With vim being the editor with a primary focus on editing text, it's fair to
     25  say that it is filled to the brim with features that support that.
     26 
     27  Today I am going to present to you a few plugins that complement existing
     28  features as well as bust your productivity while writing.
     29  
     30  For the rest of the article I will assume that you know how to install vim plugins.
     31  
     32  If you are new to the world of vim plugins make sure to check out my previous
     33  article on how to install plugins with plugin manager. It can be found [HERE](https://dimitrijedobrota.com/articles/VimPugins.html)
     34  
     35  
     36 ## Textobj User by kana
     37 
     38 This plugin is the base that all of the other plugins that I am about to showcase today are based on.
     39 
     40 This plugin is, in essence, a framework that allows it's users to easily define
     41 new text objects by helping you avoid common pitfalls as well as hiding all of
     42 the tedious implementation details, allowing you to focus on the task at hand.
     43 
     44 You can install it by putting the following line in your `vimrc`:
     45 
     46     Plugin 'kana/vim-textobj-user'
     47     
     48 Instruction on how to use this plugin can be found on [Github](https://github.com/kana/vim-textobj-user),   
     49 
     50 Before do so, it's a good idea to get a feel for what kind of functionality it
     51 is used for  by reading ahead or full checking out the full list of other
     52 plugins that depend on this one [here](https://github.com/kana/vim-textobj-user/wiki).
     53     
     54     
     55 # Textobj Brace and Textobj Quotes
     56 
     57 If you want to quickly change all of the parameters of a function, array
     58 initialization or anything else that revolves around quotes or braces, vim has
     59 you covered.
     60 
     61 Text object 'Inside' or `i` is very powerful utility. It works with:
     62 
     63 - Brackets: `(`, `[` and `{`
     64 - Quotes: `'`, `"` and ```
     65 - Tag: `t`
     66 
     67 It allow you to use any motion and the contents of the brackets or quotes. You can delete, change, yank and more.
     68 
     69 After a while, it gets tedious to always have to specify the exactly what you want to change. That's where these two plugins step in, and go one step further. The plugins provide the following set of binding:
     70 
     71 - Brace:
     72     - `ij`: operate on the content between the closest pair of braces
     73     - `aj`: operate on the content between the closest pair of braces, including the braces
     74 - Quotes:
     75     - `iq`: operate on the content between the closest pair of quotes
     76     - `aq`: operate on the content between the closest pair of quotes, including the quotes
     77 
     78 Where these two plugins shine, apart from being able to target the surrounding itself, is that you don't even need to be inside the area to do the change.
     79 
     80 It's easier to understand by looking at examples (the cursor is shown with `|`):
     81 
     82 1. Before: `foo '1, |2, 3' bar`; after pressing `diq`: `foo '|' bar`
     83 2. Before: `foo| '1, 2, 3' bar`; after pressing `diq`: `foo '|' bar`
     84 3. Before: `foo '1, 2, 3' |bar`; after pressing `daq`: `foo | bar`
     85 
     86 These plugins can be installed with:
     87 
     88     Plugin 'Julian/vim-textobj-brace'
     89     Plugin 'beloglazov/vim-textobj-quotes'
     90     
     91     
     92 # Textobj Variable Segment
     93 
     94 Renaming is an unavoidable part of any project, whether it be a CSS attribute
     95 or a variable name, you often have to deal with `snake case` and `title case`
     96 text, which can be a pain to do in vim. This plugin is here to solve that
     97 problem.
     98 
     99 It provides two bindings `iv` and `av` and their usage is illustrated in the example:
    100 
    101 - Before: `foo_ba|r_baz`; after pressing `civ`: `foo_|_baz`
    102 - Before: `QU|UX_SPAM`; after pressing `civ`: `|_SPAM`
    103 - Before: `eggsAn|dCheese`; after pressing `civ`: `eggs|Cheese`
    104 - Before: `_privat|e_thing`; after pressing `civ`: `_|_thing`
    105 
    106 - Before: `foo_ba|r_baz`; after pressing `dav`: `foo_baz`
    107 - Before: `QU|UX_SPAM`; after pressing `dav`: `SPAM`
    108 - Before: `eggsAn|dCheese`; after pressing `dav`: `eggsCheese`
    109 - Before: `_privat|e_thing`; after pressing `dav`: `_thing`
    110 
    111 
    112 This plugin is installed with:
    113 
    114         Plugin 'Julian/vim-textobj-variable-segment'
    115         
    116         
    117 # Textobj Parameter
    118 
    119 When dealing with functions, changing names and types of the parameters is inevitable. To make that process more easy consider using this plugin.
    120 
    121 Following the logic of the previous ones, this one allows you to change each of the parameter one by one. Look at the example:
    122 
    123 `i,` to inner parameter object
    124 
    125 ```
    126 function(param_a, param_b, param_c)
    127          |<--->|  |<--->|  |<--->|
    128 ```
    129 
    130 `a,` to a parameter object including whitespaces and comma
    131 
    132 ```
    133 function(param_a, param_b, param_c)
    134          |<----->|
    135 function(param_a, param_b, param_c)
    136                 |<----->|
    137 function(param_a, param_b, param_c)
    138                          |<----->|
    139 ```
    140 
    141 In addition, 'i2,' is similar to `a,` except trailing whitespace characters (especially for first parameter)
    142 
    143 ```
    144 function(param_a, param_b, param_c)
    145          |<---->|
    146 ```
    147 
    148 This plugin can be installed with:
    149 
    150         Plugin 'sgur/vim-textobj-parameter'
    151         
    152         
    153 ## Conclusion
    154 
    155 Plugins showcased today are the great introductory plugins, as their are both
    156 lightweight and provide only a handful of biding to remember while still
    157 packing a good punch.
    158 
    159 As you can see, all of these plugins share the same philosophy. They provide a
    160 simple textobject to be used with any existing motion such as `d`, `c` or `y`.
    161 
    162 After you feel comfortable with using these plugins I encourage you to check
    163 out [this](https://github.com/kana/vim-textobj-user/wiki) link for even more plugins that provide similar functionality
    164 
    165 If you feel particularly brave, you can try writing your own using Textobject
    166 User API detailed
    167 [here](https://github.com/kana/vim-textobj-user/blob/master/doc/textobj-user.txt)