I'm currently studying python to learn one scripting language. Sample data is often needed when learning a programming language.
This time, how to quickly create array data (strictly, list in python) with vim + bash.
I wrote it properly, but I don't think the environment will be a problem.
Open the source file with vim.
$ vim sample.py
I want to assign the data of the numerical string in ascending order to the list array in the following state.
#!/usr/bin/python
array = [
]
for i in array:
print i
At that time, vim's command line mode bursts. Bring the cursor to ```array = [`` `on the 3rd line and execute the following in normal mode.
:r! echo {1..100}, | xargs -n 10
As a result, a string of numbers is inserted as shown below.
array = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
]
Since',' is added to the last element, it may be necessary to delete it depending on the language used. There is no grammatical problem with python. After that, please shape it appropriately such as indentation. (Select a range in normal mode of vim and'>' is easy)
In command line mode, if you do your best with seq instead of echo, you can process it a little more.
For example, in python, the above data can be created easily and concisely by using a built-in function such as range, but this time I wanted to show that "vim + bash can be used for general purposes and easily shorten the time". So please understand that point.
Recommended Posts