Is there an easy way to do wrap this in sublime text? - sublimetext3

names = [Joey
Aurelio
Evan
Donny
Foster
Dwayne
Grady
Quinton
Darin
Mickey
Hank
Kim
Peter
Jeremy
Jess
Jimmie]
I want the names to appear as:
names = ['Joey', 'Aurelio','Evan', 'Donny',.....,'Jimmie']

Manually select all the names inside the brackets.
Select Selection → Split into Lines, which makes each name its own selection.
Press the single quote ' key, which places a quote ' before each selection.
Press the right arrow → key, which creates multiple cursors, one at the end of each word.
Press the single quote ' then the comma , key, which adds the closing single quote and a comma , after each name. If you wish, you can then press Space followed by the forward delete Delete key to erase the newlines and put all the names on the same line.
Manually navigate to the end of the list, remove the last comma, and re-add the closing bracket ].

Related

Replace a word (or many words) with spaces of same length?

In a general sense, my question is how do I do something like "dw" or "dd", but instead of deleting characters, I want to over-write with spaces?
E.g. lets say I have text:
first second third
if the cursor is on the "s" in second, I can hit "dw" to get:
first third
but what if I want:
first third
Is there a simple way to do that? An ideal solution would be to use the "d" style syntax (e.g. dw, daw, d$, etc.) but with whitespace replacement instead of deletion.
From the start of the word,
Ctrl-v to enter visual block mode,
e to move to the end of the word (highlighting the word in the process),
r[SPACE] to replace the highlighted characters with spaces.
Because of their very nature (the next character must be consumed), r and R can't work like operators. If you want to replace a motion, visually select it first, and then do r<Space> or r_ or whatever.
In this very specific case:
ver<Space>
or:
viwr<Space>
NOTE: I used ve and viw because the semantics of w are inconsistent so I prefer to avoid it when possible.

Vim: Quickest way to copy text inside quotes

What is the most efficient way to copy text inside quotes in vim, for example hello in "hello" 'hello' '''hello''' or """hello"""? The quickest I'm able to do is:
v (enter visual mode)
w or e (get to the end of the text, approximately
h or l to get to the exact correct place
y to yank the text
Here's an example: https://gyazo.com/a2bb432dc04de58ac628327740f6c033. While I might be able to improve to get it, perhaps in 3s, doing this with a mouse would take all of 0.25s. What would be the most efficient way to do a copy-paste such as the above?
If the text is surrounded by only one pair of quotes, in this case double quotes, the most efficient way to copy that text is yi". This will copy (y) the text inside the quotes (i"), regardless of where the cursor originally is. To make this work with single quotes, brackets, parentheses, or something else, simply replace the " with the character surrounding the text.
If the text is surrounded by more than one pair of quotes, however, we must first navigate to the innermost quote before we can copy the text inside. The command above will not work, since it will see the first two quotes with nothing in between them ("").
The fastest way to navigate to the first quote is f". Then, press ; until the cursor is on the innermost quote, and we can now use yib (the ib command selects the inner block.) to copy the text inside!
It might be possible to create a mapping that will automatically move the cursor to the innermost quote and copy the text inside, but that is a bit too advanced for me.
If your cursor is inside the word you want to copy, simply press yi followed by a quote char ' or "
Make sure you also read :h text-objects

Surround Several Words With Quotes At Once In vim

I was curious if there is a way to surround several words at once with quotes using vim. I am using tpope surround and repeat but I was wondering if there is a command like
3ysw"
so from
one two three
to
"one" "two" "three"
You can visually select the range with v3e, and then run a substitution command on it: :s/\v(\w+)/"\1"/g (the range '<,'> should automatically be inserted).
Personally though, I'd rather surround one word with ysw", and then do w.w. (repeat as often as needed).
Alternatively, record a macro that does both steps (surrounding and moving on to the next word), then call it n times:
qqysw"3wq
After this is in your q register, you can then call 2#q to perform the surroundings on the remaining words.
When you want to enquote three words, beginning with the one your cursor is currently placed within, you can do:
bv3ec'<Ctrl+r>"'
b places the cursor at the beginning of the current word, v enters visual mode, 3e jumps at the end of the current 3-word sequence, c cuts the selection and enters insert mode, where you insert the left enclosing quote ' and press <Ctrl+r>" in order to paste current contents of the clipboard buffer, before you insert the other enclosing quote '.
Omit the leading b if you start off with the cursor at the first character of the first word.
Another substitution option
s,\w\+,"&",g
s ............. substitute current line (add %s for the whole file)
\w\+ .......... one word or more
"&" ........... & represents the whole match on the search part
g ............. every occurrence on the line
OBS: When using substitution we can use a different delimiter in order to make easy to type. (Also useful when searching for things like "/my/pattern/")

How to edit multiple locations simultaneously in Vim

In certain text editors, like E, I can select multiple locations and, as I type, all the selected locations get replaced with the characters I am typing.
For example if I have:
<tag1 class=""></tag1>
<tag2><tag3 class=""></tag3></tag2>
In E I could select two locations inside sets of quotations marks then start typing and both locations would be updated simulataneously. In Vim, I can select several connected columns at once and then edit them, but I'm wondering if there is any way to select multiple locations that aren't lined up.
Here's how I would probably edit those particular lines (there are many ways):
/""<enter>
aText to replace...<esc>
n
.
First, search for the empty quotes to put the cursor on the first one. Using the "a" (append) command, type the new text to put inside the quotes. When you're done, use "n" (next) to go to the next instance, and "." (repeat last command) to insert the same text again. Repeat the "n ." as many times as necessary.
This method takes less up-front preparation and lets you get started right away without identifying ahead of time all the locations where you might want to add the text.
You may be looking for visual mode blockwise, which will allow insertion, deletion etc on several lines at once.
Blockwise mode will allow square selections with the column and line of the initial point in one corner, and the current cursor position defining the column and line of the other corner. This, as opposed to the line based selection that is the default.
CTRL-v will place you in blockwise visual mode.
If you have several lines like so:
INSERT INTO Users VALUES(1, 'Jim');
INSERT INTO Users VALUES(2, 'Jack');
INSERT INTO Users VALUES(3, 'Joseph');
And wanted to insert "0," after the id for each line, then place the cursor after the comma in the first line:
INSERT INTO Users VALUES(1,* 'Jim');
With the asterisk representing the cursor the command sequence would be:
CTRL-v # Put into blockwise visual mode
j # Down a line
j # Down a line
CTRL-I # Captial I for insert
0, # the text to insert
Esc # escape
The text should now look like:
INSERT INTO Users VALUES(1, 0, 'Jim');
INSERT INTO Users VALUES(2, 0, 'Jack');
INSERT INTO Users VALUES(3, 0, 'Joseph');
Also blockwise visual mode, x will delete a selection, y will yank it.
:help CTRL-V will give further documentation.
Have a look at SnippetsEmu. It should be doing something very similar to what you need.
It emulates TextMates snippets. You should be able to have one snippet with the same tag repeated, and editing will do the right thing, updating the same tag in all locations, as you type.
For your example I would use a substitution:
:%s/class=""/class="something"/g
Try vim-multiple-cursors.
Press Ctrl+N as many times as necessary to select the multiple occurences.
I'm also looking for something like that, more specifically a very useful functionality from ST2, where you press CTRL+D to select next occurrence and then replace both occurrence by just typing it.

Align columns in VI

I have a bunch of lines that I'd like to split into two columns, and get the data from each column. The data looks something like this:
current_well.well_number
current_well.well_name
current_well.well_type_code
well_location.section
well_location.range
Essentially what I'd like to do is split the line based off of the period, turn the data into two columns, and then grab the data for each column. I know this can be done in Excel, but I'm really interested in VIs solution for this problem. I know that
%s/\./
will format the string with empty spaces. But once I have the data looking like:
current_well well_number
current_well well_name
current_well well_type_code
well_location section
well_location range
How do I grab all the values for each column so I can paste it into another application?
The linux column command works well for creating the columns
:%!column -s . -t
Then use block copy.
One option is to use this Align plugin to line up the periods so you can more easily select a column in Visual Block mode. e.g. if you do this:
:%Align \.
You'll end up with:
current_well . well_number
current_well . well_name
current_well . well_type_code
well_location . section
well_location . range
If you don't want to use a plugin, try padding your columns with spaces. e.g. for your text:
:%s/\v(.*)\.(.*)/\=printf("%-16s %s", submatch(1), submatch(2))/
That'll leave you with:
current_well well_number
current_well well_name
current_well well_type_code
well_location section
well_location range
Then you can Ctrl-V and select a column of text to copy. Just make sure you pick a column width wider than your widest value.
I've run into this problem a number of times. Changing the dots to tabs will line them up mostly nice, but not really. It's true that all the columns will start on a tabstop, but there's no guarantee that they'll be on the same tabstop. For instance, if this was your original text:
lt1tab.value1
gt1tabstop.value2
and you do:
%s/\./\t/g
and assuming a tabstop is 8 spaces, you'll get:
lt1tab value1
gt1tabstop value2
What you might want to do instead is remove everything but the last column (or whichever column you want). For instance:
%s/^.*\.//
will leave you with:
value1
value2
Which you can easily copy and paste.
If you don't have to use Vim, you can use unix's cut to do what you want:
cut -f2 -d. input_file > output_file
I do this all the time, simply by padding with a lot of space:
%s/./ /
Then <c-v> to enter block mode in the empty space, draw the column position I want, < to shift the text to the right toward my column, then hold down .. Takes seconds.
Another way you can do it if you know the specific column position you want to line up on: go to the first line and start recording (qq), find your delimeter (f.), insert a bunch of space (20i <esc>), delete space back to a specific column (d15|), then move down a line (j). Then you just hold down shift and roll your fingers across #Q a bunch of times (or use a count 10#q) until all the columns are lined up. :)
Obviously, the above methods with block select and/or Excel are easier. But I'm a masochist and I decided to assume you didn't have access to either of those and try to do it with only vi commands. Here's the horrible thing I thought up:
My basic plan is to turn the list of joined columns into two lists, one after the other. This basically involves breaking each row into two lines, then copying every other line to the end of the file.
So, first we have to break each line into two lines with this command:
:%s/\./^M/
Next, swing by the bottom of the file and create an empty line, then return to the first line. This will help with readability later.
Go[Esc]
:1
Now, you need to map the following sequence to your favorite key:
:map [Key] mkjddGp'kj
(For the record, this marks the current row, deletes the row below it, pastes that row at the bottom of the file, returns to the row you started from, and then moves to the next row.)
Finally, press the mapped key once for every row in your list. So, with the example list, you'd press it 5 times. Make sure you start from the first line in the list!!
What you'll have at the end is the following:
current_well
current_well
current_well
well_location
well_location
well_number
well_name
well_type_code
section
range
You can now easily copy each list to wherever you need to put them.
Ctrl V selects a column
Why not block visual mode ? C-v, select what you want, and paste it in some other application.
For that kind of pasting, you should probably set
:set guioptions+=a
as well.
Hmm, now that I've re-read your question. Are you asking how to divide the data into columns, or how to paste it in some other app ?
I think I got it now (3rd reading).
Do as you've done and get the spaces where the dots used to be. Then do another substitution, and replace the spaces with tabs, each tab being, 15 or something. It will line them up nicely.
Then you can select and copy them to wherever.
You can also match the regex to select the second column for example, but although it is light up like a christmas tree, you won't be able to yank it.
I'd convert the dots to tabs, then block select each column:
:%s/\./CtrlV-Tab/g
CtrlV to then block select the columns.
Or, as you'd have tab separated values, they will paste directly into columns in Excel.
At the top of the file:
!}awk -F. '{print $1}' >> %
You will be told the file has changed. Select the L (load) option.
Add a blank line under the original lines, go back to the top.
for(i=1;i<=NR;i++);print $2 >> %
Again select the L option
Delete the original lines.
You now have the first and second parts in two groups.
One possible solution:
w (or W for WORDS) jumps to next word,
CTRL-R CTRL-W gets the word under cursor and pastes it into command-line
Now you have to chain those commands in a loop and then output them in your own manner.
You can select columns in gvim by typing either Ctrl-VCtrl-G or Ctrl-QCtrl-G (depending on if you are using windows insert key mappings) and then selecting with the mouse.
nnoremap <BS> v"by:%!column -t -s<C-r>b -o' '<CR>
This is a nice solution following on from #NES & #SergioAraujo idea. Put it in your vimrc.
USAGE. Move the cursor to the delimiter. Then hit Backspace.
Explanation we use the named register b to hold our delimiter. Then substitute it as the variable for the --separator option of the column UNIX utility.
And Backspace is a good key because in normal mode is the same as h. But then again you might also be prone to hitting it accidentally.

Resources