How to set line numbers by default in vim? - macos

I know you can add line numbers in vim by using,
:set number
How do I set this to default behavior?

add this line to ~/.vimrc (if not exist, create a new file)
:set nu
and save the file

the settings in $HOME/.vimrc file would be loaded automatically.
Except for set number to show linenumber, I have this to toggle normal line number and relative line number: (by pressing <leader>nu)
"---------------------------------------------------------
"toggle relativeline number
"---------------------------------------------------------
function! ToggleRelativeNumber()
let &relativenumber = &relativenumber?0:1
"let &number = &relativenumber? 0:1
endfunction
nnoremap <silent> <Leader>nu :call ToggleRelativeNumber()<cr>
https://github.com/sk1418/myConf/blob/master/common/.vimrc#L704

Open the file /etc/vim/vimrc (in sudo mode) and add the following line: set number
By the way, you will also find other (highly recommended) interesting commands you can enable:
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
"set ignorecase " Do case insensitive matching
"set smartcase " Do smart case matching
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
"set mouse=a " Enable mouse usage (all modes)

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>

I want to removing highlighted braces in vim

" Configuration file for vim
set modelines=0
" CVE-2007-2438
" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible
" Use Vim defaults instead of 100% vi compatibility
set backspace=2
" more powerful backspacing
" Don't write backup file if vim is being called by "crontab -e"
au BufWrite /private/tmp/crontab.* set nowritebackup nobackup
" Don't write backup file if vim is being called by "chpass"
au BufWrite /private/etc/pw.* set nowritebackup nobackup
syntax on
set ai
set shiftwidth=4
set tabstop=4
set ruler
set backspace=2
set ic
set hlsearch
set incsearch
set smartindent
set confirm
set history=200
set cursorline
set number
:nohl
:set nowrap
set mouse=a
colo google
hi Normal ctermbg=none
the above is my .vimrc
As you can see, my braces was highlighted at line 3 and line 5 although my cursor is not on them(so, the highlighting may not be controlled by MatchParen).
However, while I can't remove their highlighting by using MatchParen, I think there must be another way to deal with it.
How can I disable these highlighting???
thx :)
You need to find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin. When you have the name of the offending syntax group, you can investigate where it comes from; (the last lines of) :scriptnames may help.

Vim Command Mapping "Copy to Clipboard" Not Working

I recently began using Vim as my primary editor instead of programs like Atom/VSCode. I added a number of leader mappings to simplify tasks I do quite often but I'm having trouble with a few of them.
In Visual mode, I would like to be able to press <Space>y to copy the current selection to the clipboard (+ register). I've verified that I can do this manually by entering visual mode, selecting the text I want, and pressing "+y. However, my mapping doesn't seem to work:
vmap <Leader>y "+y
I set my leader the following way:
map <Space> <Leader>
I do it this way so that when showcmd is set, I get a visual indicator in operator-pending mode. By looking at that indicator, I can tell that when I press <Space>, I do enter operator pending mode on the \ key as expected. Then, when I press y, I am no longer in operator pending mode, but I am still in visual mode and haven't yanked the selection to the register.
To make sure there wasn't a plugin colliding with my mapping, I backed up my .vimrc and replaced it with one that only has the following contents:
set showcmd
map <Space> <Leader>
vmap <Leader>y "+y
Does one of these keys need to be escaped? Or am I doing something else wrong?
(I'm currently running Ubuntu Bash on Windows. Vim is version 7.4)
For reference, I got the idea from this article (And use the exact same command):
https://sheerun.net/2014/03/21/how-to-boost-your-vim-productivity/
Thankfully, the fix is pretty simple.
map <Space> <Leader>
is incorrect. The right way is
let mapleader=" "
From :help mapleader
*<Leader>* *mapleader*
To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used. It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead. Example: >
:map <Leader>A oanother line<Esc>
Works like: >
:map \A oanother line<Esc>
But after: >
:let mapleader = ","
It works like: >
:map ,A oanother line<Esc>
Note that the value of "mapleader" is used at the moment the mapping is
defined. Changing "mapleader" after that has no effect for already defined
mappings.
How to define the "leader" key is explained under :help mapleader. If you want to use <Space> as "leader" you are supposed to do:
let mapleader = "\<Space>"
Note that the "leader" key is not a special key at all. With <Space> as "leader", the two mappings below are strictly equivalent:
vmap <leader>y "+y
vmap <Space>y "+y

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

How does one align code (braces, parens etc) in vi?

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

Resources