7

Understanding the Vim. Repeat Function

 1 year ago
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

Search icon
see notifications
Notifications
Happy Weekend, enjoy these top stories from this week, Kaiseki, PIRATES, and more 💚
Last Saturday at 6:00 PM
Happy Weekend, enjoy these top stories from this week, A Duel, ESCAPE, and more 💚
03/11/2023
Happy Weekend, enjoy these top stories from this week, XAXA, ARENA, and more 💚
03/04/2023
Happy Weekend, enjoy these top stories from this week, UTGARD, THUVIA, and more 💚
02/25/2023
Good news Hackers! Our stats graph has leveled up - see how much time daily people spend reading your stories!
02/23/2023
Annotate any hackernoon stories to win a free t-shirt!
02/13/2023
New Contest Alert: Win BIG with the #web-development and #ecommerce writing contests!
02/13/2023
🚨 WINNERS ALERT 🚨 #growth-marketing Writing Contest Announces Round 4 Results 🕺 💃
01/30/2023
#respectthefuckinggreen mug, OG Tee, and other Hacker Merch at 15% Discount until 1/31/2023
01/18/2023
It’s Not Over Until It’s Over! Read our 2022 recap! 📗
12/26/2022
ChatGPT is on 🔥! What do you think is next for A.I?
12/15/2022
New Writing Contest Launch! Win Up To 500 USD Per Month on #MobileDebugging Stories!
12/05/2022
HackerNoon is a Multi-language Platform: All Top Stories Now Available in 8 Languages
11/28/2022
Over 100,000 votes have been casted for this year’s Noonies Nominees. Results will be announced on December 1st.
11/28/2022
Start off your week the right way! Here are some must-read top stories from this week, The Trail to the Sea, Culture, The Philosopher's Public Library and more 💚
11/28/2022
Recently laid off from a tech company? Share your story for free on HackerNoon!
11/14/2022
New Week, New Chance to Win from $18,000! Enter #EnterTheMetaverse Writing Contest Now!
11/08/2022
Stable Diffusion AI Image Generation is Now Available in the HackerNoon Text Editor!
11/03/2022
HN Shareholder Newsletter: Green Clock Strikes Noon :-)
10/26/2022
Vote now on HackerNoon weekly polls!
10/26/2022
Highlight any text on a story and HackerNoon will generate beautiful quote images for you to share!
10/26/2022
Don't miss out on the daily top trending stories on HackerNoon! Subscribe to TechBeat to see what people are currently interested about!
09/20/2022
HackerNoon now publishes sci-fi! Read some of our science fiction stories today and submit your own!
09/05/2022
$200k+ in Committed Writer Payouts for HackerNoon Writing Contests. Enter to Win Monthly Prizes!
08/29/2022
see 17 more
Understanding the Vim. Repeat Function by@andrew.smykovv

Understanding the Vim. Repeat Function

March 21st 2023 New Story
3 min
by @andrew.smykovv

Andrei Smykov

@andrew.smykovv

Passionate Software Developer.

Open TLDRtldt arrow
Read on Terminal Reader
Read this story in a terminal
🖨️
Print this story

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 .
featured image - Understanding the Vim. Repeat Function
Your browser does not support theaudio element.
Read by Dr. One (en-US)
Audio Presented by

@andrew.smykovv

Andrei Smykov

Passionate Software Developer.

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...
Custom|
Hackernoon hq - po box 2206, edwards, colorado 81632, usa

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK