I would like to know how to align to the right in a visual block without changing the text before and the block.
I used this code till before:
:<C-U>'<,'>s/\%V\(.\{-}\)\(\s\{-}\)\%(\%V\#!\|$\)/\2\1/
However I noted that it doesn't work when after the visual block is only spaces.
(There has to be text after the visual block in order to make above code work)
Is there no way to align text to the right in a visual block whatever is written after the block?
Example:
text before +align text text after
text before align text text after
text before align text text after
text before align text+ text after
What I want to do is select a block of text from + to + (see example above)
and align it to the right. Output must be:
text before align text text after
text before align text text after
text before align text text after
text before align text text after
Above code does the job but it does not work when there is not something written after align text in every line.
In order to solve the issue correctly handling all corner cases, I would use
the following function.
function! RightAlignVisual() range
let lim = [virtcol("'<"), virtcol("'>")]
let [l, r] = [min(lim), max(lim)]
exe "'<,'>" 's/\%'.l.'v.*\%<'.(r+1).'v./\=StrPadLeft(submatch(0),r-l+1)'
endfunction
function! StrPadLeft(s, w)
let s = substitute(a:s, '^\s\+\|\s\+$', '', 'g')
return repeat(' ', a:w - strwidth(s)) . s
endfunction
I use this. Put the cursor before the text which should be aligned to right. Run :Right
How does it work:
v0 Visualy select the text from current position to the beginning of the line
d Delete selected text and put into the buffer
:right Align the text on the right side of the cursor
0 Put cursor to the first column
gv Visualy select same area which we deleted before
p Replace selection with deleted text
command! Right execute "normal v0d\<CR>:right\<CR>0gvp"
:'<,'>s/\%V.*\%V/\=printf("%*s", col("'>")-col("'<"), substitute(submatch(0), '^\s*\|\s*$', '', 'g'))
Related
I have a snippet of code I copied into my clipboard. Pasting it out looks like this. Where[tab] is an actual tab indent
[tab]<header id="masthead" class="site-header">
[tab][tab]<h1>
[tab][tab][tab]<h2>
[tab][tab][tab][tab]<h3>
[tab][tab][tab][tab][tab]<h4>;
I want to press an autohotkey to automatically normalize the code snippet. So if there's a [tab] on every line, remove it.
Then convert each [tab] into 2 spaces [**]
<header id="masthead" class="site-header">
**<h1>
****<h2>
******<h3>
********<h4>;
So the general workflow is:
Copy code to clipboard
Press an autohotkey
Paste the newly formatted contents
Pseudocode for autohotkey would look like this
Dig through every clipboard content line by line
If every item shares an equal number of [tab] spaces, remove them entirely
Line by line, convert [tab] to [**] 2 spaces
; convert each tab into 2 spaces:
clipboard =
(
<header id="masthead" class="site-header">
<h1>
<h2>
<h3>
<h4>;
)
clipboard := StrReplace(clipboard, A_Tab, A_Space A_Space)
https://autohotkey.com/docs/commands/StringReplace.htm
okay problem solved I have everything I want now. I took the other source code snippet here on autohotkey forum
F9::
clipboard := StrReplace(clipboard, A_Tab, A_Space A_Space)
position := RegExMatch(clipboard, "\S")
test := RegExReplace(clipboard, "m)^[ ]{" . position - 1 . "}")
clipboard := test
As a reference, need to highlight the full line on code snippet like so
So all I have to do now is:
Copy the full code
Press F9
Paste into my notetaking app (dynalist.io with codesnippet support)
For example, suppose I want to implement a plugin that draws a margin line at 80 colums using custom characters (suppose I want the line to made from a column of * characters).
How can that be done in Vim or Neovim?
But more generically, is there a way to draw stuff over the text buffer without affecting the text content?
For example, how can I draw an inner rectangle inside a window which I can make bright colored in order to show the active window? The effect would be that the first line visible line of what is currently a text buffer would be filled with --- characters, the right-most column of the what is currently a text buffer would be filled with |. This would be inside the window, separate from the statuslines or vertical split lines.
Etc. How to do such things?
Some plugins that currently draw over the text buffer in different ways:
https://github.com/easymotion/vim-easymotion
https://github.com/Yggdroot/indentLine
EasyMotion does not draw over the text, i don't think this is possible.
What it does is defined in following function (sourcecode):
function! EasyMotion#helper#VarReset(var, ...) "{{{
if ! exists('s:var_reset')
let s:var_reset = {}
endif
if a:0 == 0 && has_key(s:var_reset, a:var)
" Reset var to original value
" setbufvar( or bufname): '' or '%' can be used for the current buffer
call setbufvar('%', a:var, s:var_reset[a:var])
elseif a:0 == 1
" Save original value and set new var value
let new_value = a:0 == 1 ? a:1 : ''
" Store original value
let s:var_reset[a:var] = getbufvar("", a:var)
" Set new var value
call setbufvar('%', a:var, new_value)
endif
endfunction "}}}
So it saves every replaced char and restores them afterwards.
I haven't looked at indentLine but it probably does intelligent listchars, as there is never text under the indentchar.
Anyway it isn't as if I am an expert on one of the plugin or vim in general. I just write this answer because i think there are easier way to achieve what you want. You could for example highlight the border lines with a certain color, or change the background for the active split. There is also a plugin for dimming inactive splits: https://github.com/blueyed/vim-diminactive
I want to indent a line partially in vim, starting from the cursor. For example: I have the line
var array = []
with cursor positioned on =.
And I want to get something like this as a result
var array = []
My AlignFromCursor plugin does that.
In your example, to align the = and following to column 20, use 20<Leader>le.
For aligning multiple instances, also look into Align - Help folks to align text, eqns, declarations, tables, etc or the alternative Tabular - Vim script for text filtering and alignment.
So I have the following text (==== delimiters denotes selected text):
This is some text
I'm not interested in.
This is indented text
not important.
=====================
This text portion is
selected.
=====================
Also not important
indented text
Other text i'm not
interested in.
Now I want to create a vim function so that when called, it appends at the top and at the bottom some default text. For example I would like to end with:
This is some text
I'm not interested in.
This is indented text
not important.
THIS TEXT WAS APPENDED
AFTER FUNCTION CALL
This text portion is
selected.
THIS OTHER TEXT WAS
APPENDED AFTER THE SAME
FUNCTION CALL
Also not important
indented text
Other text i'm not
interested in.
Please note that indentation should be preserved (thanks benjifisher)
How can I do this?
This is a little sloppy, but it works:
function! Frame() range
'<put!=\"THIS OTHER TEXT WAS\nAPPENDED AFTER\nFUNCTION CALL\"
'>put=\"THIS OTHER TEXT WAS\nAPPENDED AFTER THE SAME\nFUNCTION CALL\"
endfun
Select your lines in Visual mode, and type
:'<,'>call Frame()
(The range '<,'> is inserted automatically.) There are some advantages to using :execute with a:firstline and a:lastlineas in :help function-range-example, instead of the Visual marks '< and '>, but that gets a little complicated. You could also prefix each command with :silent if you do not care to be told that 3 lines have been added (twice).
Here is a version that copies the indentation from the first and last selected lines, as requested in the updated question. This version uses :call append() instead of :put, which makes it more convenient to use a:firstline and a:lastline; possibly, this will be useful if you ever want to call the function with a range other than the Visual one.
" Add lines at the bottom before adding at the top, since the line numbers
" will change when you add lines.
function! Frame() range
" Define a list of lines and prepend the desired indentation.
let botlines = ['THIS OTHER TEXT WAS', 'APPENDED AFTER THE SAME', 'FUNCTION CALL']
let botspaces = matchstr(getline(a:lastline), '\s*')
let lines = []
for line in botlines
call add(lines, botspaces . line)
endfor
" Now add those lines at the bottom.
call append(a:lastline, lines)
" Now do the same thing at the top.
let toplines = ['THIS OTHER TEXT WAS', 'APPENDED AFTER', 'FUNCTION CALL']
let topspaces = matchstr(getline(a:firstline), '\s*')
let lines = []
for line in toplines
call add(lines, topspaces . line)
endfor
call append(a:firstline - 1, lines)
endfun
I have a text file with many lines.
test =
more text more text more text more text
more text more text more text more text
... etc....
more text more text more text more text
more text more text more text more text
1 text
test2 =
more text more text more text more text
more text more text more text more text
3 more text
etc
What I want to do is to move lines up starting with a number
and attach them after the first line found (going backwards) ending with '=\s'
expected output:
test = 1 text
more text more text more text more text
more text more text more text more text
... etc....
more text more text more text more text
more text more text more text more text
test2 = 3 more text
more text more text more text more text
more text more text more text more text
I have no idea how to do this.
Can someone help me?
Using :global, :norm, :move and the possibility to use a search as target for Ex commands:
:g/^\d/m?.*=$|norm kJ
Breakdown:
:g/pattern/command " executes command for every line matching pattern
^\d " pattern for "lines that start with a number"
m?.*=$ " move matched line to right below the first
" line ending with = upward
| " separator between Ex commands
norm " execute normal mode command
kJ " go to line above and join
A macro may help...
/^\d<cr>:.m?=<cr>kJ
short explanation:
/^\d " find line beginning with number
:.m?= " move current line under the previous line with (=)
kJ "move cursor back to the line with (=), and join the next
it is working like:
(it seems that I typed one more ? and the last n in screenshot, but I won't record it again.)