cabin

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

VimShowcase2.md (4466B)


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