Understanding the Vim. Repeat Function
source link: https://hackernoon.com/understanding-the-vim-repeat-function
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Understanding the Vim. Repeat Function
Understanding the Vim. Repeat Function
3 min
by @andrew.smykovv
Andrei Smykov
@andrew.smykovv
Passionate Software Developer.
Too Long; Didn't Read
Vim is a powerful text editor that can make your work with code faster and more efficient. It comes with a variety of built-in functions that simplify everyday actions (: help builtin). We'll look into one of them: the built-in function .audio
element.Vim is a powerful feature-rich text editor that can make your work with code faster and more efficient. It comes with a variety of built-in functions that simplify everyday actions (: help builtin).
We'll look into one of them: the built-in function repeat (: help repeat) that repeats an expression a specified number of times and returns a concatenated result.
It has the following syntax:
repeat({expr}, {count})
We will look into 2 important cases where the function may come in handy:
- repeat() in the Command Line
- repeat() in Insert Mode
Repeat in the Command Line
Let's see how to use the repeat() function in the context of variables in the command line.
:let separator = repeat('-', 100) # set the variable
:echo separator # echoes separator
When an expression is a List, it's concatenated a specified number of times:
:let names = repeat(['michael', 'joseph'], 3) # expr is a list
:echo names # echoes names
The function can also be used as a method:
:echo [1, 2, 3]->repeat(2) # use repeat as a method
If you have some data you need to insert that is stored in variables, you may use :put to insert them after the cursor (:help :put)
:let names = repeat(['michael', 'joseph'], 3)
:put =names->string()
I’d like to point out that we’ve transformed the result into string names ->string() to insert it as a list in one line.
If you remove ->string(), each item of the list will be on a separate line.
Repeat in Insert Mode
While working with code, we often need to repeat some expressions or fill in structures with some data.
Let's see how we can use repeat() in Insert Mode. When you are in Insert Mode:
<C-r>=repeat('[]', 10)<CR>
It means: Press Ctrl-r, type =repeat('[]', 10), Pres Enter
Press Ctrl-r in Insert Mode
type =repeat('[]', 10)
Press Enter to display the result
We’ve added '[][][][][][][][][][]'
and stayed in Insert Mode.
repeat(range(5), 3)->string() # repeats [0, 1, 2, 3, 4] three times
string(repeat(range(5), 3)) # use built-in string()
repeat(tempname() . ': ', 3)->string()
tempname() # generates a temporary file name that doesn't exist
. # concatenate two strings
The built-in repeat() makes working with the code quick and convenient, especially in Insert Mode. It saves us from switching between different modes, which increases the overall flow's speed. Thanks to a variety of built-in functions in Vim, you can generate data of various types for {expr} in a wide range. Put the function into action, and make your life easier.
Comments
loading...Recommend
-
200
Responses (1)There are currently no responses for this story.Be the first to respond.One of the great joys of Vim is its endless learni...
-
106
README.markdown repeat.vim If you've ever tried using the . command after a plugin map, you were likely disappointed to discover it only re...
-
17
Repeat tests. Repeat tests. Repeat tests. Repeats tests using different inputs (
-
41
I have recently decided to update my vim configuration for Haskell which had been set up back in 2017 following the instructions from the “Vim and Haskell in 2016...
-
27
Photo by @marcusloke on Unsplash ...
-
22
Different Ways To Repeat Your Last Command In LinuxToday, I will be teaching you how to repeat your last command in Linux. You don't have to re-type the previously executed commands in your Shell. Of...
-
8
Vim's 'single-repeat' dot command Okay, so first things first, let me get this off my chest. Vim, I’ve been cheating on you. I know that I’ve said you’re all t...
-
3
Understanding the init Function in Go Search The...
-
9
Sony Airpeak S1 drone firmware update adds direct repeat flight, Remote ID functions ...
-
4
Functions are the small pieces of logic that together form applications. If you write applications in TypeScript, knowing function types is a must. This guide covers everything you need to know to start using TypeScript function types....
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK