ViM

20 Notes
+ Color Scheme (June 13, 2022, 11:59 a.m.)

:colorscheme then Space followed by TAB. :colo --------------------------------------------------------------------------------- The short version of the command is :colo --------------------------------------------------------------------------------- :colo murphy ---------------------------------------------------------------------------------

+ Move cursor to the first or last line (June 4, 2022, 10:31 a.m.)

For going to the first line: gg For going to the last line: Press the Esc key and then press Shift + G

+ Comment lines (March 27, 2022, 2:39 a.m.)

Comment all lines: :%s/^/# Comment lines 10-15: :10,15s/^/# Comment line 10 to current line: :10,.s/^/# Comment line 10 to end: :10,$s/^/#

+ Display line numbers (Nov. 10, 2021, 10:16 p.m.)

To show line numbers: :set nu :set number To disable line numbers: :set nonu :set nonumber ----------------------------------------------------------------------- Make the vim editor display line number by default: vim ~/.vimrc Append: set number -----------------------------------------------------------------------

+ Python (Feb. 2, 2021, 3:25 p.m.)

# vim: tabstop=4 shiftwidth=4 softtabstop=4

+ See the full path of the current file (Nov. 21, 2020, 3:26 p.m.)

1 Ctrl+G Pressing 1 followed by Ctrl+G shows the full path of the current file. Pressing only Ctrl+G shows the path relative to Vim's current working directory,

+ Get the number of lines in the current file (June 7, 2020, 9:48 a.m.)

Use line() function: :echo line('$') ------------------------------------------------------------

+ Colors & Schemes (Oct. 14, 2019, 12:34 a.m.)

:colorscheme morning :colorscheme elflord ----------------------------------------------- :color desert :color koehler ----------------------------------------------- :set background=dark :set background=light -----------------------------------------------

+ Moving Screen (Feb. 18, 2017, 4:38 p.m.)

zz - move current line to the middle of the screen zt - move current line to the top of the screen zb - move current line to the bottom of the screen Ctrl-y Moves screen up one line Ctrl-e Moves screen down one line Ctrl-u Moves cursor & screen up ½ page Ctrl-d Moves cursor & screen down ½ page Ctrl-b Moves screen up one page, cursor to last line Ctrl-f Moves screen down one page, cursor to first line

+ Indent (Feb. 15, 2017, 5:25 p.m.)

:set shiftwidth=2 Select the lines using CTRL+V, and then press > (which is shift+.) ------------------------------------------------------------ If you don't want the text to get formatting/indented use the following command before pasting the content: :set paste This will prevent Vim from re-tabbing your code. When done pasting, :set nopaste will return to the normal behavior. ------------------------------------------------------------ gg=G gg to get the start of the file = to indent G to the end of the file. ------------------------------------------------------------ If you want to reindent the block you're in without having to type any chords, you can do: [[=]] ------------------------------------------------------------ The master of all commands is gg=G This indents the entire file! And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim. To indent the all the lines below the current line =G To indent the current line == To indent n lines below the current line n== For example, to indent 4 lines below the current line 4== To indent a block of code, go to one of the braces and use the command: =% ------------------------------------------------------------ Move a block of code or selected codes: Select the lines then the key > ------------------------------------------------------------ To indent five lines, 5 > > ------------------------------------------------------------ To indent a curly-braces block, put your cursor on one of the curly braces and use > % or from anywhere inside the block use > i B ------------------------------------------------------------

+ Convert tab or 8 spaces to 4 spaces (Feb. 15, 2017, 5:08 p.m.)

:set ts=4 ---------------------------------------------------------------- :set tabstop=4 ----------------------------------------------------------------

+ Comment / Uncomment (Feb. 11, 2017, 2:31 p.m.)

Comment all lines: :%s/^/# Comment lines 10-15: :10,15s/^/# Comment line 10 to current line: :10,.s/^/# Comment line 10 to end: :10,$s/^/# --------------------------------------------------------------------------------------------- Using visual block: 1- Select a multiple-line column after entering the visual block via Ctrl+v. 2- Press r followed by # to comment out the multiple-line block replacing the selection, or Shift+i # Esc to insert comment characters before the selection. --------------------------------------------------------------------------------------------- Comment: 1- Go to the first column of the first line you want to comment. 2- Press Ctrl+v and select the lines you want to comment. 3- Press Shift+i then # (The selection will disappear, but all lines within it will be modified after Step 4.) 4- Esc (hit twice) ------------------------------------------------------------------------ Uncomment: Using Ctrl+v, select the first column (#) from top lines to the bottom. Hit (d) or (x) or (delete) to remove them. ------------------------------------------------------------------------

+ Cut/Copy and Paste (Feb. 11, 2017, 2:29 p.m.)

Cut and paste: Position the cursor where you want to begin copying/cutting. Press v to select characters (or uppercase V to select whole lines). Move the cursor to the end of what you want to cut. Press d to cut (or y to copy). Move to where you would like to paste. Press P to paste before the cursor, or p to paste after. Copy and paste is performed with the same steps except for step 4 where you would press y instead of d: d = delete = cut y = yank = copy

+ 8 spaces using tab button (Sept. 16, 2016, 1:11 a.m.)

When using tab button, if you see 8 spaces, you can change it to normal mode (4) spaces using this command: set tabstop=4

+ Replace (March 11, 2015, 4:19 p.m.)

The :substitute command searches for a text pattern, and replaces it with a text string. There are many options, but these are what you probably want: :%s/foo/bar/g Find each occurrence of 'foo' (in all lines), and replace it with 'bar'. :s/foo/bar/g Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'. :%s/foo/bar/gc Change each 'foo' to 'bar', but ask for confirmation first. :%s/\<foo\>/bar/gc Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation. :%s/foo/bar/gci Change each 'foo' (case insensitive) to 'bar'; ask for confirmation. This may be wanted after using :set noignorecase to make searches case sensitive (the default). :%s/foo/bar/gcI Change each 'foo' (case sensitive) to 'bar'; ask for confirmation. This may be wanted after using :set ignorecase to make searches case insensitive. The g flag means global – each occurrence in the line is changed, rather than just the first. This tip assumes the default setting for the 'gdefault' and 'edcompatible' option (off), which requires that the g flag be included in %s///g to perform a global substitute. Using :set gdefault creates confusion because then %s/// is global, whereas %s///g is not (that is, g reverses its meaning). When using the c flag, you need to confirm for each match what to do. Vim will output something like: replace with foobar (y/n/a/q/l/^E/^Y)? (where foobar is the replacement part of the :s/.../.../ command. You can type y which means to substitute this match, n to skip this match, a to substitute this and all remaining matches ("all" remaining matches), q to quit the command, l to substitute this match and quit (think of "last"), ^E to scroll the screen up by holding the Ctrl key and pressing E and ^Y to scroll the screen down by holding the Ctrl key and pressing Y. However, the last two choices are only available, if your Vim is a normal, big or huge built or the insert_expand feature was enabled at compile time (look for +insert_expand in the output of :version). Also when using the c flag, Vim will jump to the first match it finds starting from the top of the buffer and prompt you for confirmation to perform replacement on that match. Vim applies the IncSearch highlight group to the matched text to give you a visual cue as to which match it is operating on (set to reverse by default for all three term types as of Vim 7.3). Additionally, if more than one match is found and you have search highlighting enabled with :set hlsearch, Vim highlights the remaining matches with the Search highlight group. If you do use search highlighting, you should make sure that these two highlight groups are visually distinct or you won't be able to easily tell which match Vim is prompting you to substitute.

+ Tab (March 4, 2015, 11:35 a.m.)

Open three files in separate tabs: vim -p file1 file2 file3 ----------------------------------------------------------------------------- Provide a summary of open tabs: :tabs ----------------------------------------------------------------------------- Search for a file in your current path: :tabf mohsen* (If you want to open a file called mohsen.txt) ----------------------------------------------------------------------------- Open a file in a new tab: :tabedit OR :tabe ----------------------------------------------------------------------------- Close current tab: :tabclose OR :tabc ----------------------------------------------------------------------------- Jump to the first tab: :tabfirst Jump to the last tab: :tablast ----------------------------------------------------------------------------- Close all OTHER tabs (leave ONLY currently selected tab): :tabonly OR :tabo ----------------------------------------------------------------------------- To move to the next tab: gt OR :tabn ----------------------------------------------------------------------------- To move to the previous tab: gT OR :tabp ----------------------------------------------------------------------------- Go to the 3rd tab: 3gt OR :tab 3 ----------------------------------------------------------------------------- Move current tab to position #: tabmove # (Where # is a number) ----------------------------------------------------------------------------- Move current tab to the end: tabmove ----------------------------------------------------------------------------- Show the tab bar: :set showtabline=2 Hide the tab bar: :set showtabline=0 ----------------------------------------------------------------------------- :q or :wq Close a tab ----------------------------------------------------------------------------- To save and close a tab, you can also use ZZ instead of :wq -----------------------------------------------------------------------------

+ Shortkeys (Jan. 28, 2015, 1:13 p.m.)

u: undo ------------------------------------------------------------- Ctrl-R: Redo changes which were undone (undo the undos). Compare to '.' to repeat a previous change, at the current cursor position. Ctrl-R will redo a previously undone change, wherever the change occurred. ------------------------------------------------------------- U: return the last line which was modified to its original state (reverse all changes in last modified line) U is not actually a true "undo" command as it does not actually navigate undo history like u and CTRL-R. This means that (somewhat confusingly) U is itself undo-able with u; it creates a new change to reverse previous changes. U is seldom useful in practice but is often accidentally pressed instead of u, so it is good to know about. ------------------------------------------------------------- Delete line: D Double "d" to delete the whole line. The command d w will delete from the current cursor position to the beginning of the next word character. The command d $ will delete from the current cursor position to the end of the current line. D is a synonym for d $. ------------------------------------------------------------- Windows: :sp Split Window ctrl + ww Switch between windows ctrl + w and then q Close window ctrl + w and then o Close all the windows except the focused one ------------------------------------------------------------- Reread (Reload) File: :edit :edit! ------------------------------------------------------------- Select all and delete: :%d Select all and copy to buffer :%y ------------------------------------------------------------- Recent File History :oldfiles ------------------------------------------------------------- Duplicate a line: yy p ------------------------------------------------------------- Select all: ggVG ------------------------------------------------------------- Select all and delete: :1,$d Select lines from 2 to 9 and delete: :2,12d ------------------------------------------------------------- :wq / :x Save and close file ZZ Save and quit ZQ Quit without checking changes ------------------------------------------------------------- -------------------------------------------------------------

+ Vundle Configuration (Jan. 28, 2015, 8:47 a.m.)

https://www.digitalocean.com/community/tutorials/how-to-use-vundle-to-manage-vim-plugins-on-a-linux-vps ----------------------------------------------------------------------------------------------- Copy these configuration parameters into the file /etc/vim/vimrc set nocompatible filetype off set rtp+=~/.vim/bundle/vundle/ call vundle#rc() Plugin 'gmarik/vundle' Plugin 'scrooloose/nerdtree.git' Plugin 'Buffergator' filetype plugin indent on ------------------------------------------------------- Enter this command to list available plugins: :PluginSearch or :PluginSearch! (The seconds is for refreshing the list) ------------------------------------------------------- To see the installed: :PluginList ------------------------------------------------------- For searching a special plugin: :PluginSearch jquery And move to the specific plugin and press `i` to install it. This will download and install the plugin, but it will not update your ~/.vimrc to make it auto-load correctly. To enable it, first re-check the correct plugin name using `:PluginList` and add the line to .vimrc: Plugin 'jQuery' ------------------------------------------------------- Using this command you can close the vundle buffer: :bdelete ------------------------------------------------------- For removing a plugin, list them using the command `:PluginList`, and press `D` on the specified plugin. After the removal procedure, delete the plugin line in the configuration file too (/etc/vim/vimrc) ------------------------------------------------------- For installing plugins from github: For example this plugin: https://github.com/junegunn/vim-plug You can use this command: (in vim) :Plugin 'pearofducks/ansible-vim' or download it using linux shell: git clone https://github.com/pearofducks/ansible-vim ~/.vim/bundle/ansible-vim

+ Installation and Configuration (Jan. 28, 2015, 8:07 a.m.)

apt-get install vim Create a directory in your `home`: mkdir ~/.vim ----------------------------------------------------------------------------------------- Installing Vundle: Vundle is short for Vim bundle and is a Vim plugin manager. Vundle makes it very easy to manage Vim plugins. You just need to specify a few rows along the lines of Bundle "user/plugin" in your .vimrc and then issue :BundleInstall to start the installation. $ mkdir ~/.vim/bundle $ git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle From here on, continue with the Vundle configuration using my tutorials. (Vundle Configuration)