Move cursor position in bash at specific column - bash

I want to move the bash cursor in an echo command on a specific column, but without changing the line. What I have so far is:
this.echo('NONE found on ' + accountName + '(' + accountPos + ')' + '\033[30f !!!');
I want the 30 to be the column number, but the line to stay the same, but for a reason or another, the above just thinks my line number is 0, hence it resets the line to that value.

Found the answer to my own question, by moving the cursor at the beginning of line and then move it forward by 30 columns, as in this example:
\033[50D\033[30C My Text Goes Here

There's more than one way, but the simplest would be HPA (refer to XTerm Control Sequences):
CSI Pm ` Character Position Absolute [column] (default = [row,1])
(HPA).
For example
printf '\033[30`%s\n' "My Text Goes Here"
Further reading: ECMA-48:
Control Functions for Coded Character Sets

Below are instructions on how to move your cursor within a bash script or determine the current location of your cursor within a bash script ( or any text file ) using the vi text editor from a bash shell ( i.e. terminal ).
First, you will want to open the bash script ( or any text file ) using the vi editor.
vi bashscript.sh
If you want to move the cursor to line 10 and column 2 within a bash script using the vi editor run the below command ( note you type ':' to enter a command ).
:cal cursor(10, 2)
If you want to see your current row and column within a bash script using the vi editor run the below command.
:echo "Row = " line('.') ", Col = " virtcol('.')

Related

insert bash script output in vim file

I'm writing markdown in Vim and using this shortcut to set correct markdown image syntax:
nnoremap <leader>p :s/.*/![](\0)/ <CR>
when pressing ,p, /path/to/image/img.jpg (in my vim text file) becomes
![](/path/to/image/img.jpg)
But I want to add after that this { width = *variable*% }, like this
![](img.jpg){ width = *variable*% }
I made this little bash script (img.sh) that gives me the variable according to the image size:
#!/bin/bash
VAR=$(identify -format '%h' $1)
echo "scale=3; 300 * (100/$VAR)" | bc
If I do this in vim :r !img.sh /path/to/image/img.jpg I get a number in this case 32
I would want to launch this script with the shortcut above, I tried this:
nnoremap <leader>p :s/.*/![](\0){ width=/ <CR> :r !img.sh /path/to/image/img.jpg <CR> % }
I want you to help me to find a way to not type path to image to execute the script. Path to image is already written in the text if i could find a way to indicate to vim to place it in the shortcut after img.sh it would be great !
You can use a Vimscript expression on the replacement side of your :s command, that way you can refer to the text in the match (which is the path to the image) using submatch(0). You can use an expression on the replacement of a :s by beginning it with \=, see :help sub-replace-expression for more details.
Using a replacement expression, you can also use the system() function to call the external command, instead of using a separate :r !... command to read its output into the current buffer.
Putting it all together:
:s/.*/\='![]('.submatch(0).']{ width='.trim(system('img.sh '.submatch(0))).' }'/
Or to add a mapping to it:
nnoremap <leader>p :s/.*/\='![]('.submatch(0).']{ width='.trim(system('img.sh '.submatch(0))).' }'/<CR>
(NOTE: You might also want to add a :noh command to the end of your mapping, otherwise it will keep highlighting the search for .* which matches everything.)
nnoremap <leader>p :s/.*/\='![]('.submatch(0).']{ width='.trim(system('img.sh '.submatch(0))).' }'/\|noh<CR>

vim - execute range of lines as shell, output in same buffer

Let's say I'm in a buffer like this, on line 4, I want to run line 1 to 2 and have the output in the same buffer on line 4 (where cursor is):
echo "Testing"
echo "more testing"
# and here I want the output from running lines 1 to 2
...I know I can do 1,2w !sh to run lines 1 and 2 and have the output shown in whatever that temporary buffer is. But, how do I get into my actual buffer for later editing?
(And the same thing to work with visual mode selected text, not just with line ranges given by numbers.)
You were using :w !... (:help :w_c), but you probably want :! (:help :!):
gg - go to top
Vj - select the two lines
y - yank into a buffer
4gg - go to 4th line
V - select it
p - paste over it
gv - reselect the pasted range
:!sh<CR> - execute in shell and replace
or, trusting ex commands more,
:4d
:1,2y
:3pu
:4,5!sh
NB: !sh is in most cases equivalent to !, as ! will call your default shell.
Yey! Found it. In case anyone else needs this exact same hack on a virgin/foreign vim (plugin-less or someone else's server/config):
:1,2r !sh %
(yeah, output goes after commands, or technically the commands are replaced with their echo but whatever, not at cursor position, but good enough for me to replicate my Sublime + SublimeCommand workflow in vim :) )

Underlining text in vi

Is there a quick way to insert a line with the same number of hyphens or = characters as there are in the current line in vi(m)? That is, to go from:
My Heading
to:
My Heading
==========
without going to the end of the line, reading the number of characters in it from CTRL-G, opening a new line underneath, and typing <n>i= ?
I use the following mappings in my own markdown.vim:
" Level 1 Heading
nnoremap <leader>1 :co.<CR>Vr=A<CR><Esc>
inoremap <leader>1 <Esc>:co.<CR>Vr=A<CR>
The first mapping is for when already in Normal mode while the second Insert mode mapping returns to Insert mode with the cursor at the start of the line underneath the heading.
Use the Ex copy command, :co to copy the line without affecting the contents of any of the Vim registers.
Then select all of the copied line with V.
Finally, r= replaces each of those characters with an equal sign.
For the mapping keys, I use the leader key (defaults to \) followed by the numeral 1 to represent a Level 1 heading in Markdown.

Bash, Always display colored bar at top of screen

Is there anyway to modify the bash profile scripts to always display a colored bar at top of screen. I have a requirement to show a colored hostname, username, and ipaddress on the screen at all times, but i don't want to overload PS1 as it would make the prompt take up over half of the default console width.
Not perfect, but this shows you how to fix part of your prompt on the first row of the screen:
PS1='\w \[\e[s\e[1;1H\e[42m\]\h \u ipaddress\[\e[0m\e[u\]\$ '
A breakdown:
\e[s - save the current cursor position
\e[1;1H - move the cursor to row 1, column 1 (numbered from the upper left-hand corner
\e[u - restore the cursor to the previously saved position
\e42m - make the background green
\e0m - restore the default foreground/background colors
\[...\] - enclose the various non-printing characters so that bash can correctly compute the length of the prompt.
Wikipedia lists other escape codes. The two things missing from this answer are how to extend the bar all the way across the string and how to set the correct IP address.
Update: I believe this covers the changes that ruckc made:
PS1='\[\e[s\e[1;1H\e[42m\e[K\h \u ipaddress\e[0m\e[u\]\w \$ '
How about add a \n inside your PS1, so that you always use a new line with full width?
if you are looking for something less hacky (but maybe overkill), consider byobu
https://en.wikipedia.org/wiki/Byobu_(software)
Alternatively, if you are using xterms, you could set the xterm title instead:
export PS1="\[\033]0;\u $(host $(hostname))\007\]\u#\h:\w\$ "
This sets your xterm title, and sets your prompt to contain username#host:pwd.
My .bashrc contains something like this so PS1 is set correctly depending on whether we're in an xterm or not:
if [[ -n "$TERM" ]] ; then
if ( echo $TERM | $GREP -q xterm ) ; then
export PS1="\[\033]0;\u#\h:\w\007\]\u#\h:\w\$ "
else
export PS1="\u#\h:\w\$ "
fi
fi

Why do some Vim mappings include <C-U> after a colon?

I'm trying to figure out the syntax of the mapping commands, like onoremap, in vim.
Specially, I am confused over this line in the manual, regarding the use of <C-U>:
The CTRL-U (<C-U>) is used to remove the range that Vim may insert.
Can someone explain this?
That isn't part of the syntax for the onoremap command, that is explaining what a particular mapping does. That mapping is:
onoremap <silent> F :<C-U>normal! 0f(hviw<CR>
So, when the F key is used while an operator is pending vim will replace that with the bits in the next argument to the onoremap command. That starts with a : to begin an ex mode command. If there is a visual selection when the mapping is used, vim will automatically insert the range '<,'> so that the following ex command will apply to the visual selection, leaving the command line looking like:
:'<,'>
The <C-U> in the mapping tells vim that after the : is entered the Control+U combination should be used to clear the command line, eliminating the automatically inserted range leaving the command line looking like:
:
Then the remainder of the mapping is used.
You can see this for yourself by using V to begin a line-wise visual selection, then : to start entering a command. The range will show up, you can then use Control+U to clear it just as the example mapping does.
The portion of vim help that contains that mapping explains the remainder of it.
The Ctrl-U Vim-map operates as the same short-cut from the terminal command line. Check: https://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/
Avoid remapping few of them (like which breaks out a process on a terminal), but the majority (like Ctr-A or Ctrl-X) can be remapped.
If your VIM is no terminal one (like gVim), you can remap them all inconsiderately.
Btw: Ctrl-Shift-Letter is like Ctrl-Letter map for VIM-terminal.
Some terminal short-cuts:
" copy-paste
" <C-S-c> copy
" <C-S-v> paste (or replace visual selected)
" manage running processes
" <C-c> break out of a command or process on a terminal. This will stop a running program immediately.
" <C-z> send a running program in the background
" <C-d> If you are using an SSH connection, it will be closed. If you are using a terminal directly, it will be closed
" control what appears on the screen
" <C-l> clear terminal screen
" <C-s> Stop all output to the screen. This is particularly useful when running commands with a lot of long, verbose output, but you don’t want to stop the command itself with Ctrl+C.
" <C-q> Resume output to the screen after stopping it with Ctrl+S.
" Moving the Cursor
" <C-a> or Home: move cursor to beginning of line
" <C-e> or End: "" end ""
" <C-xx> Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the X key twice.
" <A-b> go left 1 word
" <C-b> "" char (like left-arrow)
" <A-f> go right 1 word
" <C-f> "" char (like right-arrow)
" Cutting and Pasting
" <C-u> erases everything from the current cursor position to the beginning of the line
" <C-k> erases everything from the current cursor position to the end of the line
" <C-w> erase the word preceding to the cursor position. If the cursor is on a word itself, it will erase all letters from the cursor position to the beginning of the word.
" <C-y> paste the erased text that you saw with Ctrl + W, Ctrl + U and Ctrl + K shortcuts
" Deleting Text
" <C-d> or Delete: Delete the character under the cursor
" <A-d> Delete all characters after the cursor on the current line.
" <C-h> Backspace: Delete the character before the cursor.
" Fixing Typos
" <A-t> Swap the current word with the previous word.
" <C-t> Swap the last two characters before the cursor with each other. You can use this to quickly fix typos when you type two characters in the wrong order.
" <C-_> Undo your last key press. You can repeat this to undo multiple times.
" Capitalizing Char
" <A-u> Capitalize every character from the cursor to the end of the current word
" <A-l> Uncapitalize every character from the cursor to the end of the current word
" <A-c> Capitalize the character under the cursor. Your cursor will move to the end of the current word.
" Command History
" <C-p> like up-arrow: press it repeatedly to keep on going back in the command history
" <C-n> like down-arrow: use this shortcut in conjugation with Ctrl+P. Ctrl+N displays the next command
" <A-r> revert any changes to a command you’ve pulled from your history if you’ve edited it.
" <C-r> search in your command history. Just press Ctrl+R and start typing. If you want to see more commands for the same string, just keep pressing Ctrl + R.
" <C-o> Run a command you found with Ctrl+R
" <C-g> Leave history searching mode without running a command

Resources