Skip to main content
Only purpose of code is needed in title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Learning Map command in VIM and would like critiques of a map

Background

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

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

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 <buffer> g ^i**<space><esc>$a<space>**<esc>:let linelength = virtcol('.')<CR>O<esc>:exec "normal! " . linelength . "i*"<CR><esc><esc>jo<esc>:exec "normal! " . linelength . "i*"<cr><esc><esc>

Question

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. Thanks.

Learning VIM and would like critiques of a map

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 <buffer> g ^i**<space><esc>$a<space>**<esc>:let linelength = virtcol('.')<CR>O<esc>:exec "normal! " . linelength . "i*"<CR><esc><esc>jo<esc>:exec "normal! " . linelength . "i*"<cr><esc><esc>

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. Thanks.

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 <buffer> g ^i**<space><esc>$a<space>**<esc>:let linelength = virtcol('.')<CR>O<esc>:exec "normal! " . linelength . "i*"<CR><esc><esc>jo<esc>:exec "normal! " . linelength . "i*"<cr><esc><esc>

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.

Source Link
Erds
  • 225
  • 1
  • 3

Learning VIM and would like critiques of a map

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 <buffer> g ^i**<space><esc>$a<space>**<esc>:let linelength = virtcol('.')<CR>O<esc>:exec "normal! " . linelength . "i*"<CR><esc><esc>jo<esc>:exec "normal! " . linelength . "i*"<cr><esc><esc>

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. Thanks.