How to wrap each line in quotes in SublimeText? - sublimetext3

Input:
boston beach summer figural yellow blue
boston floral flowers still still-life food pink figural
boston horse pink purple house flowers floral figural
Expected output:
"boston beach summer figural yellow blue"
"boston floral flowers still still-life food pink figural"
"boston horse pink purple house flowers floral figural"
The actual input file has 600+ lines, and I'm looking at a quick way to wrap each line in quotes? Does the method involve using multiple cursors? How about macros?

I would use a multiple cursors approach like this:
Windows
Ctrl + A (Select everything)
Ctrl + Shift + L (Split into lines)
End (Put the cursor at the end of the line)
" (Add the quote at the end of the line)
Home (Go to the first character of the line)
Home (Go to the beginning of the line... like if you have tabs or spaces)
" (Add the quote at the beginning of the line)
Mac
Cmd + A (Select everything)
Cmd + Shift + L (Split into lines)
Cmd + → (Put the cursor at the end of the line)
" (Add the quote at the end of the line)
Cmd + ← (Go to the first character of the line)
Cmd + ← (Go to the beginning of the line... like if you have tabs or spaces)
" (Add the quote at the beginning of the line)

Method 1:
no multiple cursors
+ best performance (use for large files)
- slightly clumsy
Replace (.*) with "\1"
Method 2:
multiple cursors
+ best in regards to usability/comfort
- slower for bigger files
- wont work if the file has empty lines
Ctrl+a, Ctrl+Shift+l, "
Method 3:
multiple cursors
+ close to #2 as usability, but works always
- slower for bigger files
Ctrl+a, Ctrl+Shift+l, End, ", Home, "

None of these worked in Sublime Text 3 for a multi-column TSV file with tab spacing.
I found this worked for column 1:
Find: ^\s*\S+
Replace: "$0"

Related

Vim: unindent multiple lines to the beginning

Suppose I have something like this:
line 1 with text
line 2 with text
line 3 with text
line 4 with text
I want to unindent all of these lines to the beginning, like this:
line 1 with text
line 2 with text
line 3 with text
line 4 with text
Shift + V < gives me ONE level of un-indentation. How can I get them all to the beginning? Sorry, I'm having trouble phrasing this...
There are two different ways you could do this:
Visually select all of the lines, press <, and then press . as many times as you need until there is no indent left. Or if there are a specific number of lines you would like this on, you could do something like
5<< (unindent 5 lines)
<j (unindent this line and the next)
<ip (unindent inside this paragraph)
followed by as many . as you need.
Select all of the lines, and then type either :norm d^ or :s/^\s*
Also, Shift-V + V + < is basically the same as <<.

convert vertical text to horizontal text in vim

I want to convert the vertical text in my file to horizontal. like
1
2
3
to
1 2 3
I can do this using the tr command tr '\n' ' ' <file
but I want to do this using vim
An easy one. Use a range from first line until last one and join them with an space between them:
:0,$join
Select the lines and join them with J.
From :h J :
*J*
J Join [count] lines, with a minimum of two lines.
Remove the indent and insert up to two spaces (see
below).
*v_J*
{Visual}J Join the highlighted lines, with a minimum of two
lines. Remove the indent and insert up to two spaces
(see below). {not in Vi}
And, just for the fun of it:
:{fromLine},{toLine}!tr '\n' ' '
Another way:
By replacing \n with
:{fromLine},{toLine}s/\n/ /g

Keep duplicate lines notepad++

I need to remove the unique lines and keep the duplicates in my text file(read the articles written to remove duplicate lines but I want to do the opposite). Is there any way I could do that using expressions or textfx?
E.g:
file1.txt
hello
world
hello
After operation, output should be
hello
hello
Thanks in advance
In the Replace dialogue:
Find:
^(.+)\r?\n(?!(.|\r?\n)*\1)
Replace:
*leave empty!*
Options:
Select radio button "Regular Expression"
Leave checkbox ". matches newline" unselected
Pros:
Duplicate line doesn't need to be immediately after the 1st occurrence
Cons:
If a line appears x times in your data, after the regex x-1 occurrences will be left and not x as asked in OP.
This finds all lines followed by a line repetition (it does NOT find the last line, though):
.+\r\n(?=(.+\r\n)\1)
000000 111111 22
This matches a non-empty line 0, but only if it is followed by (a non-empty line \1, which is followed by \1).
Note that this assumes \r\n (Windows) line separations. On a Unix text file, just \n, on a Mac text file, just \r.
In the search box, mark Regular expression, unmark . matches newline, Replace with = "".
Example:
"Zulu
Alpha
Alpha
Bravo
Charlie
Charlie
Delta
Echo
Echo
Foxtrott
"
(file ends with empty line)
-->
"Alpha
Alpha
Charlie
Charlie
Echo
Echo
Foxtrott
"

Vim: Replace n with n+1

How do I replace every number n that matches a certain pattern with n+1? E.g. I want to replace all numbers in a line that are in brackets with the value+1.
1 2 <3> 4 <5> 6 7 <8> <9> <10> 11 12
should become
1 2 <4> 4 <6> 6 7 <9> <10> <11> 11 12
%s/<\zs\d\+\ze>/\=(submatch(0)+1)/g
By way of explanation:
%s " replace command
"""""
< " prefix
\zs " start of the match
\d\+ " match numbers
\ze " end of the match
> " suffix
"""""
\= " replace the match part with the following expression
(
submatch(0) " the match part
+1 " add one
)
"""""
g " replace all numbers, not only the first one
Edit:
If you only want to replace in specific line, move your cursor on that line, and execute
s/<\zs\d\+\ze>/\=(submatch(0)+1)/g
or use
LINENUMs/<\zs\d\+\ze>/\=(submatch(0)+1)/g
(replace LINENUM with the actual line number, eg. 13)
In vim you can increment (decrement) the numeric digit on or after the cursor by pressing
NUMBER<ctrl-a> to add NUMBER to the digit
(NUMBER<ctrl-x> to substract NUMBER from the digit)
If only incrementing (decrementing) by one you don't need to specify NUMBER. In your case I would use a simple macro for this:
qaf<<ctrl-a>q
100<altgr-q>a
Here a brief explanation of the macro: It uses the find (f) commant to place the cursor on the opening < bracket. It is not necessary to position the cursor on the digit. When press the number on the cursor or the nearest number after the cursor will get incremented.
If you want an even shorter series of commands you can position your curser ONCE by pressing f<, increment the number with ctrl-a and then just repeatedly press ;.. The ; command repeats the last cursor movement i.e. the find command. The . command repeats the last text changing command.
Check out this link for further information or use the built in documentation: h: ctrl-a.

how to move a block or column of text

I have the following text as a simple case:
...
abc xxx 123 456
wer xxx 345 678676
...
what I need to move a block of text xxx to another location:
...
abc 123 xxx 456
wer 345 xxx 678676
...
I think I use visual mode to block a column of text, what are the other commands to move the block to another location?
You should use blockwise visual mode (Ctrl+v).
Then d to delete block, p or P to paste block.
Try the link.
Marking text (visual mode)
v - start visual mode, mark lines, then do command (such as y-yank)
V - start Linewise visual mode
o - move to other end of marked area
Ctrl+v - start visual block mode
O - move to Other corner of block
aw - mark a word
ab - a () block (with braces)
aB - a {} block (with brackets)
ib - inner () block
iB - inner {} block
Esc - exit visual mode
Visual commands
> - shift right
< - shift left
y - yank (copy) marked text
d - delete marked text
~ - switch case
Cut and Paste
yy - yank (copy) a line
2yy - yank 2 lines
yw - yank word
y$ - yank to end of line
p - put (paste) the clipboard after cursor
P - put (paste) before cursor
dd - delete (cut) a line
dw - delete (cut) the current word
x - delete (cut) current character
One of the few useful command I learned at the beginning of learning VIM is
:1,3 mo 5
This means move text line 1 through 3 to line 5.
In VIM, press Ctrl+V to go in Visual Block mode
Select the required columns with your arrow keys and press x to cut them in the buffer.
Move cursor to row 1 column 9 and press P (thats capital P) in command mode.
Press Ctrl+Shift+b to get in and out of it.
(source)
Using an external command "awk".
%!awk '{print $1,$3,$2,$4}' test.txt
With pure vim
:%s,\v(\w+) (\w+) (\w+) (\w+),\1 \3 \2 \4,g
Another vim solution using global command
:g/./normal wdwwP

Resources