cabin

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

VimShowcase2.md (4466B)


      1 @date: 2021-08-25
      2 @title: Vim Showcase #2 - Text Motion
      3 @language: en
      4 @categories: linux, vim
      5 
      6  # Vim Showcase #2 - Text Motion
      7  
      8  Welcome to the second installment of the **"Vim Showcase"** series.
      9  
     10  Today, I've prepared some interesting vim default capabilities as well as plugins to
     11  enhance those capabilities to he maximum for even better experience. We will
     12  take a look at sorting, interacting with system clipboard and finally
     13  interacting with vim registers.
     14  
     15  ## Vim sort motion
     16  
     17  Vim allows its user to leverage the power of the shell utilities by typing
     18  `:!util-name`. This allows for a numerous possibilities for text editing as
     19  you can write special shell scripts to be used when needed.
     20  
     21  Sorting in a file is extremely useful when you need to order some collection
     22  of data after it has been gathered by some some script or simply to sorting a
     23  header file includes or function declarations for better readability.
     24  
     25  A easy way to sort some lines of text is to enter line-visual mode
     26  with `Shift+v`, select the lines you want to sort, then typing `:!sort`.
     27  
     28 This plugin builds on top of this setup, by providing a sort motion for easy
     29 access, without a need for visual mode, and more. It can be installed with:
     30 
     31         Plugin 'christoomey/vim-sort-motion'
     32         
     33 The primary interface to this plugin is via the `gs` mapping, which stands for `go sort`, for sorting based
     34 on a text object or motion. For example to sort 10 lines down, type: `gs10j`.
     35 
     36 Where this plugin surpasses the shell variant is that it also provides a
     37 capability of sorting comma separated lists of items. For example by typing
     38 `gsi(` you can sort `(b, c, a)` to become `(a, b, c)`.
     39 
     40 One other neat feature of this plugin is that, because it uses sort shell
     41 function under the hood, it's possible to pass user specified flags for
     42 different sort behavior. For example by adding the following line to `vimrc` sort will
     43 become case insensitive and it will also remove duplicates:
     44 
     45         let g:sort_motion_flags = 'ui'
     46         
     47 **Note**: This feature only works for linewise sorting
     48 
     49 By passing an `n` flag it's possible to sort numbers by their value regardless of the length.
     50 
     51 ## System copy
     52 
     53 Another great plugin by christoomey which defines `cp` to be copy motion and
     54 `cv` as paste motion, allows you to easily copy and paste to and from system
     55 clipboard without much hassle. Install it by adding:
     56 
     57     Plugin 'christoomey/vim-system-copy'
     58     
     59 You will also need to have `xsel` installed from your terminal, as the plugin
     60 needs a way to interact with the system clipboard, with `apt-get install xsel`
     61 if you use Debian based distro, or `pacman -S xsel` if you use Arch based
     62 distro. It's possible to change the program used, in case you prefer the other
     63 one, with:
     64 
     65         let g:system_copy#copy_command='xclip -sel clipboard'
     66         let g:system_copy#paste_command='xclip -sel clipboard -o'
     67 
     68 Also make sure to add the following line in order to get rid of the message
     69 every time you use the plugin:
     70 
     71         let g:system_copy_silent = 1
     72         
     73 Some examples:
     74 
     75 - `cpiw` => copy word into system clipboard
     76 - `cpi'` => copy inside single quotes to system clipboard
     77 - `cvi'` => paste inside single quotes from system clipboard
     78 - `cP`   => is mapped to copy the current line directly
     79 - `cV`   => is mapped to paste the content of system clipboard to the next line
     80 
     81 
     82 ## Replace with Register
     83 
     84 In the last section you saw how to interact with system clipboard. Now I am
     85 going to talk about vim's built in registers.
     86 
     87 Out of the box, vim provides place to store yanked text called register. To yank
     88 the text use `y`, and to later put it use `p`. Vim provides multiple registers
     89 whose content can be inspected with `:register`. To specify the register to be
     90 used, prefix any yank or put command with `"name`, where name is a character
     91 representing a specific register.
     92 
     93 This plugin provides a motion to replace content of a text object with selected
     94 register. The default biding is `gr`, which stands for `go replace`.
     95 
     96 Some examples:
     97 
     98 - `"2gri(` => replace the contents of the brackets with the content of register 2
     99 - `griw` => replace the current word with the content of default register
    100 
    101 This can also work with visual selections.
    102 
    103 ## Conclusion
    104 
    105 Today, we had a look at some simple but powerful plugins. Because these plugins
    106 are just new text motions they can be utilized with any text object showcased in the
    107 previous article, so their use is truly limitless.