diff options
author | kballou <kballou@devnulllabs.io> | 2015-04-13 18:44:55 -0600 |
---|---|---|
committer | kballou <kballou@devnulllabs.io> | 2017-09-02 19:31:44 -0600 |
commit | 81a67b408d654e9b279c99f57eabae4aa07b22d8 (patch) | |
tree | 4a75c1b4b12615d6cde3f6d740cf78d7e159bd32 | |
parent | fe2ebb0093023e0e9f10807d21d7b30995915c8e (diff) | |
download | blog.kennyballou.com-81a67b408d654e9b279c99f57eabae4aa07b22d8.tar.gz blog.kennyballou.com-81a67b408d654e9b279c99f57eabae4aa07b22d8.tar.xz |
Vim Tips post # 2
-rw-r--r-- | content/blog/vim_tips_2015_05_07.markdown | 141 | ||||
-rw-r--r-- | static/media/videos/comment.ogg | bin | 0 -> 280358 bytes | |||
-rw-r--r-- | static/media/videos/cw.ogg | bin | 0 -> 221082 bytes | |||
-rw-r--r-- | static/media/videos/indent.ogg | bin | 0 -> 290204 bytes | |||
-rw-r--r-- | static/media/videos/macros.ogg | bin | 0 -> 691466 bytes |
5 files changed, 141 insertions, 0 deletions
diff --git a/content/blog/vim_tips_2015_05_07.markdown b/content/blog/vim_tips_2015_05_07.markdown new file mode 100644 index 0000000..a00c2b6 --- /dev/null +++ b/content/blog/vim_tips_2015_05_07.markdown @@ -0,0 +1,141 @@ +--- +title: "Vim Tips 2015-05-07" +description: "Vim Tips: Visual Mode and Macros" +tags: + - "Vim" + - "Tips and Tricks" + - "Editors" +date: "2015-05-07" +categories: + - "Development" + - "Editors" + - "Tools" +slug: "vim-tips-2015-05-7" +--- + +Many Vim users may have, accidentally or not, discovered the dot (`.`) command +in Vim. It's a main stay for a lot of Vim users and is clearly one of those +actions that should be in more editors. Except when it is the wrong action for +the job. + +More often than not, the visual selection mode and one-off macros are a better +choice. + +## Visual Mode ## + +I won't go into all of the cool things that can be accomplished with Vim's +visual mode, but I will showcase a few examples where the visual editor is +clearly a better choice than the dot (`.`). + +Visual mode offers, in essence, a multi-line cursor for which you can do a lot +of changes, quickly. + +### Visual Mode Basics ### + +To enter visual mode, it is as simple as pressing `^v` or `ctrl-v`. Next you +will want to select what you want to change with your typical movement commands +(`h`, `j`, `k`, `l`, and of course `w`, `e` and all the rest). Finally, you +finish with the action: `I` if you want to insert before the selection, `A` if +you want to append after the selection, `C` if you want to change the +selection, and `d`, `D`, `x` if you want to remove the selection, just to name +a few. + +### Some Examples ### + +For (a bad) example, if you need to comment out a contiguous set of lines, you +can easily accomplish this with the visual mode. + +{{< video "/media/videos/comment.ogg" "video/ogg" 600 400 >}} + +A related example to prefixing is indentation changes, I often use the visual +mode to fix code indentation when changing block-level indentation or when +copying code into a markdown file. + +{{< video "/media/videos/indent.ogg" "video/ogg" 600 400 >}} + +As another example, if you need to change a single word in multiple columns, +visual mode will make easy work of this (especially when the columns are +aligned, if not see macros below or [substitution ranges][2] from the previous +tip). + +{{< video "/media/videos/cw.ogg" "video/ogg" 600 400 >}} + +For more information on Visual Mode, you can check Vim's [visual][1] help +document. + +## Macros ## + +For when visual mode may not be enough or when the change is repetitive in +operations but not in columns or what have you, it's time to bust out the +macros. Vim macros are easily available for use you can use all the registers +to record and store each macro. + +### Macro Basics ### + +To record a macro, it's as simple as `q<register-label><commands>q`. That is, +press `q`, select a register (a-z1-0), enter your commands as if you were using +Vim normally, and finally `q` again to finish. Once your macro is recorded, you +can use it with `@<register-label>`. And, like most Vim commands, you can +attach a repetition to it: `<n>@<register-label>` where `<n>` is the number of +times to repeat the command. + +You can also replay the last macro with `@@`. + +### Some Examples ### + +As a simplistic example, we can use a macro to convert it into, say, JSON (this +example is clearly taken from the [Vim Wikia][3]). + +Let's say we have the following data: + + one first example + two second example + three third example + four fourth example + +And we want to change it to the following: + + data = { + 'one': 'first example', + 'two': 'second example', + 'three': 'third example', + 'four': 'fourth example', + } + +We can do this by performing the following: + +First, we want to start recording our macro. While the cursor is under the 'o' +of 'one', we will press `qd` to record our macro to the `d` register. + +Next, we will want to change the tabbing by performing a substitution: + + :s/\s\+/': ' + +Then, we will insert our first tick with: + + I' + +And append the last tick and comma with: + + A', + +Before we finish recording, one of the more important operations of making +macros repeatable is moving the cursor to the next line and putting it into the +correct position for the next execution. Therefore, move the cursor the begging +of the line and move down one line: + + 0j + +Finally, press `q` to finish recording. + +We should now be able to press `3@d` and watch as the rest of the lines change. + +To finish up the example, we'll manually enter `data = {` and the tailing `}`. + +{{< video "/media/videos/macros.ogg" "video/ogg" 600 400 >}} + +[1]: http://vimdoc.sourceforge.net/htmldoc/visual.html + +[2]: https://kennyballou.com/blog/2015/03/vim-tips-2015-03-17/ + +[3]: http://vim.wikia.com/wiki/Macros diff --git a/static/media/videos/comment.ogg b/static/media/videos/comment.ogg Binary files differnew file mode 100644 index 0000000..44c5185 --- /dev/null +++ b/static/media/videos/comment.ogg diff --git a/static/media/videos/cw.ogg b/static/media/videos/cw.ogg Binary files differnew file mode 100644 index 0000000..836536d --- /dev/null +++ b/static/media/videos/cw.ogg diff --git a/static/media/videos/indent.ogg b/static/media/videos/indent.ogg Binary files differnew file mode 100644 index 0000000..d048e7c --- /dev/null +++ b/static/media/videos/indent.ogg diff --git a/static/media/videos/macros.ogg b/static/media/videos/macros.ogg Binary files differnew file mode 100644 index 0000000..adda898 --- /dev/null +++ b/static/media/videos/macros.ogg |