Roopal Jain
Roopal Jain

Reputation: 23

How can I enter similar lines of code with sublime text with just one char different?

I have to write code which is like

apple1 =1
banana1 =10
cat1 =100
dog1 =1000

apple2 =2
banana2 =20
cat2 =200
dog2 =2000
.
.
.
<to be done till>
apple50 =50
banana50 =500
cat50 =5000
dog50 =50000

Is there any shortcut to copy paste the first 4 line and keep pasting with running sequence ?

Any level of short cut is appreciated to do this partially or completely.

Thanks

Upvotes: 2

Views: 72

Answers (3)

r-stein
r-stein

Reputation: 4847

As already mentioned the easiest way to do it is using a programming language, but you can use python in Sublime Text.

Open the ST console ctrl+` and paste:

view.run_command("insert", {"characters": "\n\n".join("apple{0} ={0}\nbanana{0} ={0}0\ncat{0} ={0}00\ndog{0} ={0}000".format(i) for i in range(1, 51))})

this will insert the requested content.

You could also write a plugin using Tools >> New Plugin... and paste:

import sublime
import sublime_plugin


class PasteSequenceCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        content = sublime.get_clipboard()
        content, sequence_number = content.replace("1", "{0}"), 2
        if content == view.settings().get("ps_content"):
            sequence_number = view.settings().get("ps_sequence_number") + 1

        view.settings().set("ps_content", content)
        view.settings().set("ps_sequence_number", sequence_number)

        view.run_command("insert", {"characters": content.format(sequence_number)})

Afterwards add keybinding:

{
    "keys": ["ctrl+shift+v"],
    "command": "paste_sequence"
},

Then you can copy the block containing the 1 and each 1 will increase each time you use the paste sequence command.

Upvotes: 1

Xenwar
Xenwar

Reputation: 192

You need to redirect the output to a file.

#!/bin/bash
cntr=1
banana_cntr=10
cat_cntr=100
dog_cntr=1000

for i in `seq 1 1 50`
do
        echo "apple${cntr}=$[$cntr * 1]"
        echo "banana${cntr}=`expr $cntr \* $banana_cntr`"
        echo "cat${cntr}=`expr $cntr \* $cat_cntr`"
        echo "dog${cntr}=`expr $cntr \* $dog_cntr`"
        cntr="$[cntr + 1]"
        echo " "
done

Upvotes: 0

streetturtle
streetturtle

Reputation: 5850

For me it seems that this task is not for text editor. It looks more like task for a script. For example in bash it would be like following:

#!/bin/bash
for i in `seq 1 50`;
do
    echo "apple$i .. ${i}=${i}" >> text.txt
    echo "banana$i =${i}0" >> text.txt
    echo "cat$i =${i}00" >> text.txt
    echo "dog$i =${i}000" >> text.txt
done

To run it:

  • create file, say inserter.sh
  • make it executable by chmod +x inserter.sh
  • run it ./inserter.sh

Result will be in text.txt file in the same folder.

Upvotes: 0

Related Questions