pattern minor by @import:stackexchange-codereview 112d ago
Map command in VIM
Background
I've just started using VIM and have become comfortable with navigation. I'm attempting to reduce repetitive tasks and thought I would take a crack at mapping. I was able to accomplish my task below with a map command, but would like to know what the best way is to do what I am doing.
Task
I frequently write up text files with commands I use. In order to help navigate the files, I like to put a descriptive header above each command or step. For instance:
```
Randomly Generate Statsd Counters
```
should be formatted to look like this
```
***************************************
** Randomly Generate Statsd Counters **
***************************************
```
Solution
I probably spent way too much time on this, but came up with this map command in VIM. I should be able to navigate to any point on the Header line and hit a g key (The key doesn't matter), and be able to wrap the text with the header format. Here is the map:
```
nnoremap g ^i**$a**:let linelength = virtcol('.')O:exec "normal! " . linelength . "i*"jo:exec "normal! " . linelength . "i*"
```
Question
I'm sure there is a better way to do this and I thought this would be a good opportunity to first attempt and then have my solution critiqued.
vimscriptcodereviewstackoverflow