How I can move or shift the words in the entire file to the specified column?
For example like below:
Before :
123 ABC
112 XYZS
15925 asdf
1111 25asd
1 qwer
After :
123 ABC
112 XYZS
15925 asdf
1111 25asd
1 qwer
How it can be done using command mode?
Here the thing is we need to shift the 2nd word to the specified column
Here the specified column is 8
except for vim-plugins mentioned by others, if you were working on a linux box with column command available, you could just :
%!column -t
% could be vim ranges, e.g. visual selections etc..
Approach with built-in commands
First :substitute the whitespace with a Tab character, and then :retab to a tab stop to column 8, expanding to spaces (for your given example):
:.,.+4substitute/\s\+/\t/ | set tabstop=7 expandtab | '[,']retab
(I'm omitting the resetting of the modified options, should that matter to you.)
Approach with plugin
My AlignFromCursor plugin has commands that align text to the right of the cursor to a certain column. Combine that with a :global command that invokes this for all lines in the range, and a W motion to go to the second word in each, and you'll get:
.,.+4global/^/exe 'normal! W' | LeftAlignFromCursor 8
I use the Tabular plugin. After installing it, you run the following command:
:%Tab/\s
where \s means whitespace character
I have made two functions for this problem.
I have posted it here : https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim
We need to call this function in vim editor and give the Number of Occurrence of the Character or Space that you wants to move and the character inside the '' and the column number.
The number of occurrence can be from the starting of each line (MCCB function) or can be at the end of each line (MCCE function).
for the above example mentioned in the question we can use the MCCB function and the character we can use space, so the usage will be like this in the vim editor.
:1,5call MCCB(1,' ',8)
So this will move the first space (' ') to the 8th column from line number 1 to 5.
I don't want the 'Delete' button to yank the characters I delete.
I can give an example:
When cutting and pasting some rows to elsewhere.
code
code
code
code2
code2
code2
I want to cut code and paste it below code2. It is my habbit to first do x to the 3 lines of code and after that to delete the blank lines between code and code2 with the delete button => when doing this my register changes to empty row.
Does anyone has a suggestion? Thanks
Use the "black hole register":
"_d
To delete those blank lines, select them in visual mode and use the command above.
Whenever you cut or copy something in Vim, with 'x' and 'y' for instance, it's copied into a register. When you paste something, with 'p', you always paste the top element of the stack of registers ("").
To see all elements in the register stack type :reg
"" abc
"0 efg
"1 hij
A register starts with the " symbol. To paste register "1 do: "1p (will copy 'hij').
I believe, the only thing you can do is prefix your yank and put with, for instance "a and your delete with "b, so that they operate on different registers.
I have a txt file containing multiple rows of identical size:
(Examples)
0123456 789 AND abcdefg hij
For all rows in the file I want to add a space after the 4th character shifting the following characters to the right by 1 character. I also want to remove the space from the 8th character (which would be 9th after the initial space is added).
I have cygwin installed so sed is an option.
I also have php and visual studio 2010 installed.
Any help on this would be greatly appreciated.
sed 's/^\(....\)\(...\) /\1 \2/'
I ended up just using Cygwin -> VIM.
Open input file in Vim Editor.
Go to first line, first character using ":1"
Start recording using "qa" (Where a is the name of your macro)
Move to the 4th character of line.
Enter into edit mode by pressing "insert" or "i"
Type your space character.
Press Esc.
Move to first character by pressing "Shift+^".
Move to next line's first character.
Press q to quit from recording mode.
Now play whatever you have recorded for any number of times you want.
If you want to play it once press #a
If you want to repeat this 10 times then type 10#a
(where a is the macro name you defined earlier)
Deleting a space follows the same steps except you don't need to go into edit mode just go to the space you want to remove and hit x and move on with the instructions.
Suppose I have the piece of text below with the cursor staying at the first A currently,
AAAA
BBB
CC
D
How can I add spaces in front of each line to make it like, and it would be great if the number of columns of spaces can be specified on-the-fly, e.g., two here.
AAAA
BBB
CC
D
I would imagine there is a way to do it quickly in visual mode, but any ideas?
Currently I'm copying the first column of text in visual mode twice, and replace the entire two column to spaces, which involves > 5 keystrokes, too cumbersome.
Constraint:
Sorry that I didn't state the question clearly and might create some confusions.
The target is only part of a larger file, so it would be great if the number of rows and columns starting from the first A can be specified.
Edit:
Thank both #DeepYellow and #Johnsyweb, apparently >} and >ap are all great tips that I was not aware of, and they both could be valid answers before I clarified on the specific requirement for the answer to my question, but in any case, #luser droog 's answer stands out as the only viable answer. Thank you everyone!
I'd use :%s/^/ /
You could also specify a range of lines :10,15s/^/ /
Or a relative range :.,+5s/^/ /
Or use regular expressions for the locations :/A/,/D/>.
For copying code to paste on SO, I usually use sed from the terminal sed 's/^/ /' filename
Shortcut
I just learned a new trick for this. You enter visual mode v, select the region (with regular movement commands), then hit : which gives you this:
:'<,'>
ready for you to type just the command part of the above commands, the marks '< and '> being automatically set to the bounds of the visual selection.
To select and indent the current paragraph:
vip>
or
vip:>
followed by enter.
Edit:
As requested in the comments, you can also add spaces to the middle of a line using a regex quantifier \{n} on the any meta-character ..
:%s/^.\{14}/& /
This adds a space 14 chars from the left on each line. Of course % could be replaced by any of the above options for specifying the range of an ex command.
When on the first A, I'd go in block visual mode ctrl-v, select the lines you want to modify, press I (insert mode with capital i), and apply any changes I want for the first line. Leaving visual mode esc will apply all changes on the first line to all lines.
Probably not the most efficient on number of key-strokes, but gives you all the freedom you want before leaving visual mode. I don't like it when I have to specify by hand the line and column range in a regex command.
I'd use >}.
Where...
>: Shifts right and
}: means until the end of the paragraph
Hope this helps.
Ctrl + v (to enter in visual mode)
Use the arrow keys to select the lines
Shift + i (takes you to insert mode)
Hit space keys or whatever you want to type in front of the selected lines.
Save the changes (Use :w) and now you will see the changes in all the selected lines.
I would do like Nigu. Another solution is to use :normal:
<S-v> to enter VISUAL-LINE mode
3j or jjj or /D<CR> to select the lines
:norm I<Space><Space>, the correct range ('<,'>) being inserted automatically
:normal is probably a bit overkill for this specific case but sometimes you may want to perform a bunch of complex operations on a range of lines.
You can select the lines in visual mode, and type >. This assumes that you've set your tabs up to insert spaces, e.g.:
setl expandtab
setl shiftwidth=4
setl tabstop=4
(replace 4 with your preference in indentation)
If the lines form a paragraph, >ap in normal mode will shift the whole paragraph above and below the current position.
Let's assume you want to shift a block of code:
setup the count of spaces used by each shift command, :set shiftwidth=1, default is 8.
press Ctrl+v in appropriate place and move cursor up k or down j to select some area.
press > to shift the block and . to repeat the action until desired position (if cursor is missed, turn back with h or b).
Another thing you could try is a macro. If you do not know already, you start a macro with q and select the register to save the macro... so to save your macro in register a you would type qa in normal mode.
At the bottom there should be something that says recording. Now just do your movement as you would like.
So in this case you wanted 2 spaces in front of every line, so with your cursor already at the beginning of the first line, go into insert mode, and hit space twice. Now hit escape to go to normal mode, then down to the next line, then to the beginning of that line, and press q. This ends and saves the macro
(so that it is all in one place, this is the full list of key combinations you would do, where <esc> is when you press the escape key, and <space> is where you hit the space bar: qai<space><space><esc>j0q This saves the macro in register a )
Now to play the macro back you do # followed by the register you saved it in... so in this example #a. Now the second line will also have 2 spaces in front of them.
Macros can also run multiple times, so if I did 3#a the macro would run 3 times, and you would be done with this.
I like using macros for this like this because it is more intuitive to me, because I can do exactly what I want it to do, and just replay it multiple times.
I was looking for similar solution, and use this variation
VG:norm[N]I
N = numbers of spaces to insert.
V=Crtl-V
*** Notice *** put space immediate after I.
I am a bit of a beginner when it comes to Vim and it is currently irritating me in many ways. One of which is the following:
Say i have the following text in a file
one
two
three
four
dog
frog
log
mog
and I have used visual mode to select the number words (4 lines) if I then use P to paste at the 'd' in dog i get the following:
one
two
three
four
one dog
two frog
threelog
four mog
My desired output would be:
one
two
three
four
one
two
three
four
dog
frog
log
mog
I've noticed that it behaves as I expect if i do a y4y instead of visually selecting the lines. So what is causing the difference in behavior that I am seeing? and how can I get my visually selected block to paste as I would like?
Seems that you are entering into the Visual Block selection mode (Ctrl-V).
To get your desired output, enter into a Linewise Selection mode, just by pressing V.
Use Shift+V, it selects line by line
try
:set paste
before pasting.