Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Repeat command on each line in visual block
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Repeat command on each line in visual block
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{review}} {{TipImported |id=84 |previous=83 |next=85 |created=June 29, 2001 |complexity=basic |author=Salman Halim |version=5.7 |rating=194/59 |category1= |category2= }} One of the things i do a lot in Vim is to make a change to the beginning or end of the line (such as adding the text '// remove' at the end of java debug code). a quick way of doing this is to use a to append the text to the end of the first line and then move down one, hit . (repeat last edit), move down, hit . etc. etc. the following mapping allows one to simply highlight the region in question and hit . -- it will automatically execute the . once on each line: " allow the . to execute once for each line of a visual selection vnoremap . :normal .<CR> Another thing i do a lot is to record a quick macro in the "a" register and then play it back a number of times. while @@ can be used to repeat the last register used, my recorded macros sometimes use other registers so @@ doesn't necessarily give me the same results as @a. also, i have mapped ' to ` because i like to go to the precise location of my marks -- always -- and never to the beginning of the line. this leaves my ` key unused. so: " make ` execute the contents of the a register nnoremap ` @a then, in keeping with the visual . above, i did the same for the ` -- is thexecutes @a once on each highlighed line. vnoremap ` :normal @a<CR> as an example, say i have the following lines of java code: <pre> public String m_asdf; public String m_lkhj; public int m_hjkhjkh; </pre> and, for some reason, i need to get the following: <pre> "asdf" "lkhj" "hjkhjkh" </pre> I record the following into a: ^cf_"<Esc>$r" the ^ is because my java code is indented and i don't want to go to column 0 and the <Esc> is an actual escape i hit to exit insert mode. then, i simply select (visually) the other lines (only two in case -- admittedly not an overly useful example) and just hit `. ==Comments== i found that a few plugin scripts that i use regularly depended on ` having default functionality, so i switched the ` key to ~ for my use (i couldn't be bothered to change all the scripts to use noremap instead of just map). ---- as a side note, i have the following mapping in my vimrc: noremap v ~ since i find the v easier to hit than the tilde anyway -- and i don't have any plugins that fiddle about with the case of my text so i don't break anything this way. ---- If action is just inserting at a given column, also see [[VimTip194]] ----
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Navigation
(
view source
)
Template:Review
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab