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
Related
Consider:
ssh -i "key.pem" root#server.com
Is there a quick way to jump to the beginning of "root" in such a Bash command in the terminal, without iterating over every word/character?
Yes. Bring up the last command using up arrow. These shortcuts will help:
Ctrl + E - go to the end of the line
Ctrl + A - go to the start of the line
Alt + left - go back one word
Alt + right - go right one word
Ctrl + W - delete the last word
To search the command line and the command line history, use:
Ctrl + R and then type the search string, e.g. "roo" to search backwards for "root#...". In the example you have given, however, it will be easier just to jump forward or back by words.
Ctrl + S search forwards (rarely useful). (If forward searching doesn't appear to work, try this other Stack Overflow answer.)
See also this page and google "Bash command line editing" for more tricks.
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('.')
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.
I've remapped the following keys in Bash:
bind '"a" "b"'
bind '"b" "c"'
If I press a or b both times a c will be printed.
How can I map the keys so that by pressing a and b will be printed and only by pressing a and c will be printed (like with Vims **nore**-map)?
You can make a char in the right part a literal with a preceeding "^V":
bind '"x":"^Vx "'
The key sequence to enter the text after colon is:
" Ctrl-v Ctrl-v x space "
In bash "Ctrl-v x" results in a literal x, without key-mapping interpretation .
You may find it useful to build your own keymapping file which maps the numeric signal you get from the keyboard to a character. This is done with the loadkeys command i.e. if you're switching to the dvorak layout you can do (this is a verbose way to show you the location of the map files:
loadkeys /usr/share/keymaps/i386/dvorak/dvorak.map.gz
You could copy the map file for the layout you use and change the relavent chars, and load your modified mapping. I think loadkeys only affects the command line, though there are similar methods for changing the behaviour in X I believe.
How do you prettify / align / format code in vi? What is the command?
I have pasted in a hunk of code and I need to have it all formatted/aligned... obviously I am a vi neophyte.
x
These commands in my answer work in vim. Most people who think they're using vi are using vim. To find out if your 'vi' is really 'vim', open vi and type :version -- if it's vim, it will say so. Otherwise you might just see a version number without the name of the program. Also, when you open vim for the first time you will usually see a splash screen of some sort that says "VIM - VI iMproved"...
Automatic Indentation
To turn auto-indentation on, make sure vim knows the file type you're editing (it usually automatically detects this from the file name extension, but might not figure it out with some file types). You can tell it the filetype using the menus for syntax highlighting. Then, do this:
:filetype indent on
You can disable auto-indentation with
:filetype indent off
Automatically adjusting/correcting indentation
In general, ={motion} will align code to an indentation level.
== align the current line
=i{ align the inner block
=% align to the matching parenthesis/bracket under the cursor
=14j or 14== align the next 14 lines
=G align to the end of the file
vG= same thing, align to the end of the
file (but using visual mode)
vjjj= align four lines (using visual mode)
Manual indentation
If vim is not guessing the indentation level correctly, there are two ways to change it:
If you are in normal mode (where everything is a command), do << to shift a line left, or >> to shift it right by one tab. You can do this with several lines by using the same movement commands I showed above (eg, >i{ indents the current inner code block).
If you are in insert mode, you can indent the line further (without moving the cursor) by doing a Ctrl-T, or un-indent one tab with Ctrl-D
Aligning equals signs, etc
If you want to align equals signs in a list of declarations, you should consider using this vim script: http://www.vim.org/scripts/script.php?script_id=294
Adjusting indentation/tab sizes
If you want vim to use spaces instead of tabs when it indents, run this command (or consider adding it to your vimrc file)
:set expandtab
To set how many spaces equal a tab, I usually do this:
:set expandtab softtabstop=3 tabstop=3 shiftwidth=3
tabstop - how many columns a tab counts for (affects display of existing tab characters)
shiftwidth - controls reindentation size with << and >>, among other commands.
softtabstop - how much space to insert when you press the tab key
expandtab - expand tab keys to spaces
But if you have to work with different amounts of tabs a lot, you could also use this function and keybinding:
function! Ktabs(tabsize)
execute "set softtabstop=" . a:tabsize . " tabstop=" . a:tabsize . " expandtab shiftwidth=" . a:tabsize
"set softtabstop=a:tabsize tabstop=a:tabsize expandtab shiftwidth=a:tabsize
endfunction
noremap <leader><Tab> :call Ktabs(3)<Left>
If you are editing a file with a mix of tabs and spaces, you may want to use this command after setting tab size:
:retab
={motion}
:h =
P.S. You shouldn't use vi if vim is available.
If manually adjusting indents I will open a visual block with V on the first or last line I want to re-indent, move to the brace containing the block, goto the other brace with % then shift the line with > or <
If indents are off by a lot I will shift everything all the way left with < and repeat it with . and then re-indent everything.
Another solution is to use the unix fmt command as described in Your problem with Vim is that you don't grok vi., {!}fmt