Any ideas on how to format these lines in vim? - vim

I have the following lines that I've already formatted quite a bit, but I can't seem to figure out how to format these jagged lines so they are all in the correct position. I've been trying using macros and also using /s but it just seems the tabs or spaces after the word is a bit different for each. What might be a good way to format this here?

You can select the entire part you want to format and then use the normal command
'<,'>s/\([^: ]\+\)[ ]*:[ ]*\([^: ]*\)/\=printf('%-8s%8s%s%8s%8s',submatch(1),' ',':',' ',submatch(2))/g
which will pad the result evenly around the :.
You can select in vim using v then enter normal mode with esc followed by entering command mode with :. Paste the above line in command mode once the selection is made and hit enter.

Use vim-easy-align. And Select the text you need to align. Input ga*:. Done.

Related

Prevent automatic tab insertion or conversion of spaces to tabs

Google Docs has a "feature" that sometimes converts four spaces to one tab.
Copying and pasting text does not solve this problem, because the spaces in that text are converted to tabs automatically.
Is there a way to turn this off?
No way to turn of that I know of. So annoying.
You can work-around using normal copy-paste, then a search-and-replace.
Copy-Paste you content into the Google Doc
In a text-editor, enter a tab character then cut it to your clipboard
Back in Google Docs, highlight the content you wish to fix
Hit Ctrl + H to open Find and replace dialogue
Paste the tab character into the Find field
Insert 4 space characters into the Replace with field
Click Replace all
The approach that caused me the least headaches was to replace all spaces by another character (say underscore) in the original text, copy/paste it, then replace the underscore using find+replace. This was in Google slides.
i use cmd+shift+v (edit -> paste without formatting) to paste.
The spaces are not converted to tabs.
I did find one solution: there is a Chrome plugin called "Drive Notepad" which edits google drive files and has an option "Tabs: hard"

vim: highlight words in visual block mode

Below is a screenshot of me entering visual block mode and pressing "w" to select by word:
How can I select every word in the rows I have selected? Meaning I want the full word in the rows highlighted instead of it getting cut off as shown in the screenshot.
edit: What I want to be able to do is delete a column of words of varying length. In the example screenshot I want to delete the words between the tags, But it could be any column of words.
There are a bunch of plugins for multiple selection, look them up on vim.org.
But I must remind you that visually selecting text is more often than not an unnecessary step. Why don't you explain what you actually want to achieve instead of your failed attempts? Maybe there's a better way...
[edit]
:'<,'>norm dit
seems to be the simplest way to achieve your goal without selecting every word:
and :,+7norm dit would be even better because you don't select anything.
The highlight modes can only select blocks (by cursor, by line, or by rectangular block). You can use a plugin such as vim-multiple-cursors to do what you are trying to do.
The only place where Vim allows a non-rectangular, "jagged edge" visual selection is at the end of the lines, i.e. by extending the blockwise selection with $. Therefore, you'd need to (temporarily) get rid of the trailing </th> (or include it in the selection, but operate in such a way that they are kept intact).
You shouldn't need a selection to work with the text. For example, to delete the text inside the tags, you can use a substitution:
:%s#<th>\zs.*\ze</th>##
You can't. You can only select rectangular blocks in block select mode. Maybe a plugin solves this?

Paste multiple times

What is the best way replace multiple lines with the contents of the clipboard?
The problem I'm having is when I yank a line and paste it over another line the "yank" is replaced with the line I just replace. Now, if I want to replace another line with the same line I have to go back up and yank it again.
There's got to be a better way to do this.
I have this in my .vimrc:
xnoremap p pgvy
(note: this will work only with the default register, but this mapping is easy to remember). Writing a more elaborate version would be possible. Also, you still can use P to get the old behaviour.
"0 should have the contents of your yank. It's a bit more tedious to type, but "0p should do what you want.
Alternatively, don't select-and-replace the old lines up front. If you find those lines with a search, just hit n. over and over (after an initial p), then when they're all pasted, do ndd followed by as many n.s as necessary.
The biggest mental switch I've needed to make when moving to Vim is to figure out how to apply group edits sequentially. I.e. rather than doing a bunch of edits on a line and then doing a bunch of the same edits on another line, I'll do the first edit on a bunch of lines (using . to great effect), then the second edit on a bunch of lines, etc. Alternatively, the use of macros may help as they are fantastic, but sometimes a little more tedious to get working correctly with "complex" changes.
I often use another registry, copy the line you need to some named registry "ay and then paste from there "ap
When you paste over a selection in Vim it will replace the default register with the contents of the selection. If pasting over a selection is wiping out the contents of the clipboard register then very likely you have the following line in your .vimrc
set clipboard=unnamed
One option is to remove that and use the explicit clipboard register "+
Another option is to use any of the other explicitly named registers (a-z). After the first paste yank the line back into "c for example and then use "cp to paste from there on out.
Instead of using copy/paste, it is often better to use a text object command such as ciw to change the inner word. This method has the advantage of being easily repeatable using the . repeat command.
yiw Yank inner word (copy word under cursor, say "first").
... Move the cursor to another word (say "second").
ciw<C-r>0 Change "second", replacing it with "first" ( is Ctrl-R).
... Move the cursor to another word (say "third").
. Change "third", replacing it with "first".
use np where n is the number of how much time you want to paste the lines eg 3p will paste 3 lines.

How to delete selected text in the vi editor

I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?
Also, how can I select the lines using my keyboard as I can in Windows where I press Shift and move the arrows to select the text? How can I do that in vi?
I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?
Forget the mouse. To remove 5 lines, either:
Go to the first line and type d5d (dd deletes one line, d5d deletes 5 lines) ~or~
Type Shift-v to enter linewise selection mode, then move the cursor down using j (yes, use h, j, k and l to move left, down, up, right respectively, that's much more efficient than using the arrows) and type d to delete the selection.
Also, how can I select the lines using my keyboard as I can in Windows where I press Shift and move the arrows to select the text? How can I do that in vi?
As I said, either use Shift-v to enter linewise selection mode or v to enter characterwise selection mode or Ctrl-v to enter blockwise selection mode. Then move with h, j, k and l.
I suggest spending some time with the Vim Tutor (run vimtutor) to get more familiar with Vim in a very didactic way.
See also
This answer to What is your most productive shortcut with Vim? (one of my favorite answers on SO).
Efficient Editing With vim
Do it the vi way.
To delete 5 lines press: 5dd ( 5 delete )
To select ( actually copy them to the clipboard ) you type: 10yy
It is a bit hard to grasp, but very handy to learn when using those remote terminals
Be aware of the learning curves for some editors:
(source: calver at unix.rulez.org)
If you want to delete using line numbers you can use:
:startingline, last line d
Example:
:7,20 d
This example will delete line 7 to 20.
Highlighting with your mouse only highlights characters on the terminal. VI doesn't really get this information, so you have to highlight differently.
Press 'v' to enter a select mode, and use arrow keys to move that around. To delete, press x.
To select lines at a time, press shift+v.
To select blocks, try ctrl+v. That's good for, say, inserting lots of comment lines in front of your code :).
I'm OK with VI, but it took me a while to improve. My work mates recommended me this cheat sheet. I keep a printout on the wall for those odd moments when I forget something.
Happy hacking!
When using a terminal like PuTTY, usually mouse clicks and selections are not transmitted to the remote system. So, vi has no idea that you just selected some text. (There are exceptions to this, but in general mouse actions aren't transmitted.)
To delete multiple lines in vi, use something like 5dd to delete 5 lines.
If you're not using Vim, I would strongly recommend doing so. You can use visual selection, where you press V to start a visual block, move the cursor to the other end, and press d to delete (or any other editing command, such as y to copy).
If you want to remove all lines in a file from your current line number, use dG, it will delete all lines (shift g) mean end of file

Moving a block of code by a tabspace

I am currently visually selecting the code and typing ">" which moves the code by 2 tabs. But I only want to move it by one tab.
Is there any alternate command in VIM to move the code by a tabspace.
Ideally I would like to put a marker and then move the whole code block by a tabspace.
Thanks
This will set your shifting width to four spaces (default tab size):
:set sw=4
You can also change the size of the tab stop itself (X is any value you like):
:set ts=X
And if you like to use spaces instead of tab characters, use this:
:set expandtab
If you use the same settings in many files, you can put these in your .vimrc.
> moves the code by one shiftwidth. So you need to set that option correctly.
There are easier ways to do what you want, as others have pointed out, but the
following is of more general use:
You can select a column by pressing ctrl+v and then using the up and down keys (or j and k).
Next press I to go to insert mode. Now you can type anthing you like. In your case, type a single tab.
Finish by pressing esc, and see how your edit is applied to all lines.

Resources