I like the Markdown style underlining, but I want it to line up with the above line.
So for example if I have this:
heading one
_
^ cursor here
I could (in normal mode) just type (something)i=<ESC><ESC>, and the result would be:
heading one
===========
^ cursor here
Does anyone know what I can use for (something)?
It doesn't really matter to me where my cursor is/ends up, so for example I could be on the last position of the heading one line and do some operation to achieve the same result. I'm picky, but not that picky.
If you use visual selections you can then use r to replace every character in said visual selection.
So if you start with your cursor on the "heading one" line type.
yypVr=
Would copy the line and then replace every character with an equal sign.
kyypv$r=j
go up one line
yank it
paste it below
visually select the line
replace every character with =
Turn it into a mapping if you ned it often:
nnoremap <key> kyypv$r=j
I'd suggest you do this on the line to underline itself and not on the line below, though:
nnoremap <key> yypv$r=
Related
This question already has answers here:
How to yank the text on a line and paste it inline in Vim?
(4 answers)
Closed 8 years ago.
I couldn't find this answer anywhere. I cut some line in Vim with either dd or V+d and I want to paste it inside brackets let's say. How do I do that?
p is paste after, and P is paste before, but I want it to paste at my cursor position.
Edit: I want to cut from certain cursor position (not whole line) to end of line and paste to cursor position in another line.
When you use either of those methods for cutting, you get the newline at the end of the line. With the newline, vim has to either put the line before or after.
To get the behavior you want, you should delete using 0D instead to delete, and then use a normal p to put it into the cursor.
If you want to cut from current cursor position to the end of line and then paste it inside another line at cursor position, then navigate do the first line, do D to cut to end of line, then move your cursor to the place where you want to paste it and use p.
try this, do Y or dd as usually, and create this mapping:
nnoremap <leader>p :let #"=substitute(#","\n","","g")<cr>p
nnoremap <leader>P :let #"=substitute(#","\n","","g")<cr>P
when you press p/P it pastes in default way(with newline). when you press <leader>p /<leader>P it pastes "in-line".
This is not so clean, because it changed the #", next time you press p the newline is not there any longer. I was a bit lazy, put the sub() there, you can make a little function, remove the newline then paste, without touching (or restore after paste) the #". and in mapping call that function.
I don't know if you did a 10dd, what output do you want to have when you do an "in-line" paste. but you can do quite a lot thing in your function, to reach your needs.
EDIT,
I don't know how did you create the mapping, and "didn't work". here I put an animation:
You can do something like this:
v$d
(move to where you want to put what you just cut)
p or P
Intro
In gVim under Windows i have to replace insert and mod in each string of file some character in a specifics position.
Example
QWAER;PA 0X000055;123WAAAA
TYUIO;PA 0Y000056;123VAAAA
need to become
QWAE#;PAX000055;123;WAAAA
TYUI#;PAY000056;123;VAAAA
modify char 5 in #
delete char 9,10
insert ; in original pos 22 or after delete in pos 20
More Info
Usually I do
Put cursor and beginning of text to select
Press CTRL-V (CTRL-Q in gVim) to begin select of the column
keep press SHIFT and selecte the interested area
the go at the end of file
then do the replace insert or modification.
(This is where I learn about Keyboard-only column block selection in GVim Win32, or why does Ctrl-Q not emulate Ctrl-V when mswin.vim is included?
and here i learn how to do the insert(http://pivotallabs.com/column-edit-mode-in-vi/)
It's not a correct way to do the things.
In vim i can do the replaceof a fange of rows, and so on using commands.
I think that should be possible to use commands to replace a portion of each string but i have no Knoledge about those command.
this is the main idea
Replacing alternate line in vi text file with a particular string
Question
Is there a way to do the operations using commands with absolute position and not selection.
Thanks
:{range}normal 5|r#9|2x20|i;
Does what you want on the lines covered by {range}:
5|r# " go to column 5 and replace the character with #
9|2x " go to column 9 and cut 2 characters
20|i; " go to column 20 and insert a ; to the right
So…
:5,25norm 5|r#9|2x20|i; would apply that transformation to lines 5 to 25,
:.,$norm 5|r#9|2x20|i; would apply that transformation from the current line to the last,
:'<,'>norm 5|r#9|2x20|i; would apply that transformation to the current (or last) visual selection,
:'{,'}norm 5|r#9|2x20|i; would apply that transformation to the current "paragraph",
:%norm 5|r#9|2x20|i; would apply that transformation to the whole buffer,
and so on…
You could also record it, let's say in register q:
qq
5|r#
9|2x
20|i;<Esc>
q
and play it back on {range}:
:{range}#q
Or spend 30 minutes trying to come up with the right :s// command…
Reference:
:help range
:help :normal
:help |
:help r
:help x
:help i
:h recording
Observe a line in a Vim instance:
Now I hit $:
Why does my cursor not go all the way to the end? Once I try inserting, the text gets inserted before the last character! Even if I try to move right again while still in normal mode I get the bell. Oddly, when in edit mode I can move to the actual end of line with the right arrow key!
Does anyone know why Vim does this? On 7.3 by the way. Thanks for the help.
Pressing $ while in command mode causes the cursor to move to the end of the line, effectively highlighting the last character. Hit i here to insert before the last character, or a to append to the line. It is slightly ambiguous here, because you're using a pipe character as a cursor rather than a rectangular block cursor. Have a look at ":help termcap-cursor-shape" if you want to change that.
If the goal is to append to the end of the line, A will jump to the end of the line and enter insert mode with a single keypress.
Use a to append a character after the current.
Or, to go to the end of the line and append in 1 step, use capital A. I.e. shiftA.
Similarly shift-I to insert at the beginning of the line without first having to press ^.
The cursor can't be between two characters, it is always on a character.
If you press $ then x, you will correctly delete the last printable character of the current line.
What you are observing is the fact that using i, you are always inserting your text before the selected character. If you want to insert after the selected character, you have to use a or better A as it has already been mentioned.
In other words:
i means "insert before character under cursor".
a means "insert after character under cursor".
mnemonic for a : a for "append".
How can I fill the remainder of a line with the specified character up to a certain column using Vim? For example, imagine that the cursor is on column four and I want to fill the remainder of the current line with dashes up to column 80. How would I do that?
You can do 80Ax<Esc>d80| for a simpler solution.
Here's a function to implement what you ask, and slightly more.
It fills the line from its current end of line, rather than the cursor position
It forces a single space between what's currently on the line and the repeated chars
It allows you to specify any string to fill the rest of the line with
It uses vim's textwidth setting to decide how long the line should be
(rather than just assuming 80 chars)
The function is defined as follows:
" fill rest of line with characters
function! FillLine( str )
" set tw to the desired total length
let tw = &textwidth
if tw==0 | let tw = 80 | endif
" strip trailing spaces first
.s/[[:space:]]*$//
" calculate total number of 'str's to insert
let reps = (tw - col("$")) / len(a:str)
" insert them, if there's room, removing trailing spaces (though forcing
" there to be one)
if reps > 0
.s/$/\=(' '.repeat(a:str, reps))/
endif
endfunction
Insert that into your .vimrc, and make a mapping to it, e.g.
map <F12> :call FillLine( '-' )
Then you can press F12 to apply the hyphens to the current line
Note: this could probably be easily extended to act on a selection in VISUAL mode, but currently works for single lines only.*
If I understand the question correctly, this can be accomplished like this: in normal mode subtract the cursor's current column position from the desired ending column, then type the result followed by 'i' to enter insert mode, then the character you want to fill the space with. End by returning to normal mode. For example, with the cursor at column four in normal mode, if you wanted to fill the rest of the line up to column 80 with dashes, type 76i- then Esc or Ctrl-[ to return to normal mode. This should result in 76 dashes starting in column 4 and ending in column 79.
If you have the virtualedit option set to block or all, you can create a visual selection (even over empty space) up to the desired column:
v80| (if virtualedit=all) or
<c-v>80| (if virtualedit=block)
Then replace the selected area with dashes:
r-
It's probably helpful to start visual mode after the last character in the line by hitting l to avoid overwriting the last character in the line. If you are not using virtualedit=all, then you need to set virtualedit+=onemore so you can move one character beyond the end of line in normal mode.
One of the other answers here is: 80Ax<Esc>d80|. I initially started using it as a key mapping like this:
nnoremap <leader>- 80A-<Esc>d80<bar>
...but I didn't like how it leaves the cursor at the end of the line. Also, for narrow windows (e.g. just a little wider than 80 cells), it causes the entire window to scroll horizontally because the cursor briefly jumps to the end of the long line before it's trimmed back to 80. This is partially resolved by returning to the beginning of the line:
nnoremap <leader>- 80A-<Esc>d80<bar>0
...but then the screen will "flash" briefly while the cursor jumps offscreen and back (thus scrolling the window briefly to the right and back). To prevent this, we can temporarily use reverse insert mode (:h revins or :h ri) to keep the cursor onscreen while appending. Here's the full command as a key mapping:
nnoremap <leader>- :set ri<cr>80A-<esc>81<bar>d$0:set nori<cr>
This answer answers your question. Just replace len computation with your desired column number (+/- 1 may be, I never remember), and remove the enclosing double-quotes added by the substitution.
Using the textwidth value is also possible. It allows for different maximum line widths depending on filetype (check :h 'tw'). Here is what I now use, it appends a space after existing line content if present and will prompt for the string to use for the pattern:
function! FillLine() abort
if &textwidth
let l:str = input('FillLine>')
.s/\m\(\S\+\)$/\1 /e " Add space after content (if present).
" Calculate how many repetitions will fit.
let l:lastcol = col('$')-1 " See :h col().
if l:lastcol > 1
let l:numstr = float2nr(floor((&textwidth-l:lastcol)/len(l:str)))
else
let l:numstr = float2nr(floor(&textwidth/len(l:str)))
endif
if l:numstr > 0
.s/\m$/\=(repeat(l:str, l:numstr))/ " Append repeated pattern.
endif
else
echohl WarningMsg
echom "FillLine requires nonzero textwidth setting"
echohl None
endif
endfunction
You can map it for quick access of course. I like:
nnoremap <Leader>' :call FillLine()<Cr>
Note that the calculation assumes simple ASCII characters are being inserted. For more complicated strings, len(l:str) might not work. From :h strlen():
If you want to count the number of multi-byte characters use strchars().
Also see len(), strdisplaywidth() and strwidth().
You can insert first your dashes and then go to the first character and enter your text in replace mode: 80i-Esc0R
if you don't want to type the text, first delete the line with 0D, use 80i-Esc to insert the dashes and 0RCTRL+r " to paste the contents of the unamed register in replace mode.
I actually stumbled across this looking to align columns. Just in case anyone else does the same, this thread might be useful: How to insert spaces up to column X to line up things in columns?
I want to give up using mouse for selecting and pasting chunks of text within a buffer. Whats the most efficient way to do this with just kb? I mean navigate to arbitrary line, copy the substring, return to the previous position and paste.
Very simple method:
Select the lines with Shift-V
"Yank" (=copy) the text with y
Paste the text with p at the position you want to.
There are of course many other ways to copy and paste, yy copies the current line for example.
Do the some VIM tutorials, it is better than learning everything bit by bit.
If you want to go quickly to a line use the search by typing
/SUBSTRING and then Enter after you have found the correct substring.
Make sure to use hlsearch and incsearch
:set incsearch and :set hlsearch
When you are at the correct line, yank the whole line with yy or the whole word with yaw.
Then go back to where you started the search by typing two backticks ``
Then you can paste your yanked line/string with p
Mark your current position by typing ma (you can use any other letter instead of a, this is just a "named position register".
navigate to the line and substring for example by using a / search
yank text with y<movement> or mark it with shift/ctrl-v and then y
move back to your previously marked position with ```a`` (backtick)
paste your buffer with p or P
My normal method would be:
Use visual mode to select the text with v, V, or Ctrl+v
Yank using y
Go to the line you want to be on using 123G or :123
Navigate where I want to be within that line with t or f
Put the text with p or P
If you need to jump back and forth between the spots, I'd cycle through jumps using g, and g;.
Use "p" to paste after the current line, and "P" to paste above the current line.
Not sure what you mean by 'the substring'. If you want to copy line 50 to the current position, use:
:50t.
If you want to move line 50 to the current cursor position, use:
:50m.