copy less wrapped line results in broken lines - word-wrap

In my less (and also vi, for all that matters), when I try copy a wrapped line by marking it with the mouse, only the part of the line where I double click is copied, e.g.
lets say that this
is a a very long line
wrapped 3 times
So trying to copy the entire line results in either 'lets say that this', 'is a a very long line' or 'wrapped 3 times'.
When marking the 3 lines together and copying them will result with 3 different lines with line breaks between them (so copy to shell is a big no-no).
The problem doesn't occur when opening files with emacs and I couldn't find anything in the less man pages to stop it.
Any idea how to make the less give me the entire wrapped line when copying?

The terminal normally doesn't know you have a wrapped line. Performing a copy with the mouse will result in a rectangular area being selected (a number of lines and columns of characters).That being said, different implementation of terminals might "know" you've got a wrapped line.

Related

How can I refactor an existing source code file to normalize all use of tab?

Sometimes I find myself editing a C source file which sees both use of tab as four spaces, and regular tab.
Is there any tool that attempts to parse the file and "normalize" this, i.e. convert all occurrences of four spaces to regular tab, or all occurrences of tab to four spaces, to keep it consistent?
I assume something like this can be done even with just a simple vim one-liner?
There's :retab and :retab! which can help, but there are caveats.
It's easier if you're using spaces for indentation, then just set 'expandtab' and execute :retab, then all your tabs will be converted to spaces at the appropriate tab stops (which default to 8.) That's easy and there are no traps in this method!
If you want to use 4 space indentation, then keep 'expandtab' enabled and set 'softtabstop' to 4. (Avoid modifying the 'tabstop' option, it should always stay at 8.)
If you want to do the inverse and convert to tabs instead, you could set 'noexpandtab' and then use :retab! (which will also look at sequences of spaces and try to convert them back to tabs.) The main problem with this approach is that it won't just consider indentation for conversion, but also sequences of spaces in the middle of lines, which can cause the operation to affect strings inside your code, which would be highly undesirable.
Perhaps a better approach for replacing spaces with tabs for indentation is to use the following substitute command:
:%s#^\s\+#\=repeat("\t", indent('.') / &tabstop).repeat(" ", indent('.') % &tabstop)#
Yeah it's a mouthful... It's matching whitespace at the beginning of the lines, then using the indent() function to find the total indentation (that function calculates indentation taking tab stops in consideration), then dividing that by the 'tabstop' to decide how many tabs and how many spaces a specific line needs.
If this command works for you, you might want to consider adding a mapping or :command for it, to keep it handy. For example:
command! -range=% Retab <line1>,<line2>s#^\s\+#\=repeat("\t", indent('.') / &tabstop).repeat(" ", indent('.') % &tabstop)
This also allows you to "Retab" a range of the file, including one you select with a visual selection.
Finally, one last alternative to :retab is that to ask Vim to "reformat" your code completely, using the = command, which will use the current 'indentexpr' or other indentation configurations such as 'cindent' to completely reindent the block. That typically respects your 'noexpandtab' and 'smarttabstop' options, so it use tabs and spaces for indentation consistently. The downside of this approach is that it will completely reformat your code, including changing indentation in places. The upside is that it typically has a semantic understanding of the language and will be able to take that in consideration when reindenting the code block.

How to compare a lines of a text file with a line of different text file and append the first one if they are not same

I want to compare particular line of two text file and update one of the file if they are not same.
Updating a line in a text file is technically not possible (unless the replacing line is of exactly the same length). You have to create a new file, which you can, in the end, move to the old one.
From your tags, I assume that you are looking for a shell solution, which is maybe not a good idea. It's probably more convenient to do it in, for instance, Perl or Ruby or Python.
One possibility is to use the commands head and tail, which allow you to dissect a file into parts. You can split your file into three parts: The part before the line in question, the line itself, and the lines which come after.
Another possibility is to use a loop and the read command of the shell to process a file line by line, like this:
while read line
do
... # Decide here, whether to write $line or the replacement line
done <your_file

Sublime Text: Find word on same line as each cursor

I have multiple cursors on line with some content that is of variable length and some that's the same content. Due to the variable length content, the lines don't align with each other.
I start with a cursor on each line. I know that only by moving one word at a time to the right, I might not end up at the same spot on each line due to variable content.
How can I do an incremental find for each cursor on the same line and end up with multiple cursors that match my find expression?
Here's a possible solution:
Start with your multiple cursors (done manually, Alt+Enter after a search, etc.)
Select the whole line for each cursor (using Home then Shift+End; etc.)
Open up the search and enable the "In Selection" option (to the right of the "Wrap" option)
Perform your search on those lines, then Alt+Enter to select that match
This would work unless you have multiple matches on the same line. In this case you'll have multiple cursors on the same line

Properly overwrite current line in terminal

In most terminals, if you haven't printed a newline character (or line feed; \n), printing a carriage return (\r) will reset your cursor to the beginning of the line so that subsequent characters overwrite what you've already output on the current line.
However, if you don't output enough characters to fully overwrite the previous contents of the line, the remaining characters will stay there. So, for example, the following pseudocode:
print "goodbye"
print "\rhello"
would result in helloye.
I'm wondering: is there any way to actually clear these remaining characters? I could simply keep track of them and then overwrite them with spaces, but that would, a) require me to keep track of them, and, b) still have trailing space characters, which isn't ideal, and I'd prefer not to do (I'm looking for a general solution that I can use whenever I come across this problem in the future). Any advice would be great; thanks!
Try using terminal escape
To clear from beginning of line to cursor: echo -e "\033[1K"
To clear line: echo -e "\033[2K"
Assuming you have VT100-compatible terminal or emulator
I used a leading carriage return a long time ago and it worked pretty well. I just tried it again on Linux Gnome Terminal program and it doesn't seem to work: nothing shows up on the screen. Changed it back to using a trailing line feed and every line I print gets displayed, but not overwritten. I suspect the lack of a line feed is what is keeping it from getting actually sent to the display.
See this about flushing.

Inserting characters before whatever is on a line, for many lines

I have been looking at regular expressions to try and do this, but the most I can do is find the start of a line with ^, but not replace it.
I can then find the first characters on a line to replace, but can not do it in such a way with keeping it intact.
Unfortunately I donĀ“t have access to a tool like cut since I am on a windows machine...so is there any way to do what I want with just regexp?
Use notepad++. It offers a way to record an sequence of actions which then can be repeated for all lines in the file.
Did you try replacing the regular expression ^ with the text you want to put at the start of each line? Also you should use the multiline option (also called m in some regex dialects) if you want ^ to match the start of every line in your input rather than just the first.
string s = "test test\ntest2 test2";
s = Regex.Replace(s, "^", "foo", RegexOptions.Multiline);
Console.WriteLine(s);
Result:
footest test
footest2 test2
I used to program on the mainframe and got used to SPF panels. I was thrilled to find a Windows version of the same editor at Command Technology. Makes problems like this drop-dead simple. You can use expressions to exclude or include lines, then apply transforms on just the excluded or included lines and do so inside of column boundaries. You can even take the contents of one set of lines and overlay the contents of another set of lines entirely or within column boundaries which makes it very easy to generate mass assignments of values to variables and similar tasks. I use Notepad++ for most stuff but keep a copy of SPFSE around for special-purpose editing like this. It's not cheap but once you figure out how to use it, it pays for itself in time saved.

Resources