Vim Basics
- Save and Exit (Command mode)
1. 'w' : Save the file with latest changes but not exit out of editor
2. 'wq': Save the file and exit.
3. 'e' : Refresh the file if the file has undergone modification from a different process.
Note: Any changes to that file from the editor will be lost.
1. 'Ctrl f' : One page forward
2. 'Ctrl b' : One page backwards
3. 'w' : Move forward by 1 word
'W' : Move forward until next space character
4. 'b' : Move back 1 word
'B' : Move back to previous space character.
1. 'Shift 8' / '*' : Search the word under cursor
- Increment and Decrement number ( normal/edit mode ):
'Ctrl-a' and 'Ctrl-x'
- Add/modify a column of file (vertically)
Follow the steps:
1. 'Ctrl-v' : Creates visual block.
2. Navigate up/down the lines for which you need add/modify. ( Still in visual mode)
3. 'Shift i'
4. 'esc'
- Windows (commandline mode):
'vsp <filename>' : Vertical Split
'sp <filename>' : Split window horizontally
Navigation keys:
'Ctrl-ww' : Rotate between windows
'Ctrl-w<arrow keys>' : Navigate to corresponding window as per the arrow key. [ eg: right arrow to move to right window from current line in the current window]
'tabe <filename>': Will open the file in a new tab.
Navigation keys:
'gt' : Move to next tab. Rotates to first if it's the last tab.
'gt <number> : Moves to the tab with the given number. [ Ex: 'gt 2' will goto the second tab]
Optimizing file operations efforts using 'vim'.
Modify multiple files with "similar" operation
This is achieved by recording the operation and recursing that over all the files.
- Open all the files in vim together. 'vim file1 file2 file3.. file4' ( If you have a file with list of all the filenames, you can open as: vim `cat file_with_paths` ( notice that back quotes are used here!)
- Start the first recording: Type 'q' followed by <any character> in command mode.That particular character will be the name of what is being recorded. Example: 'qs'. All the vim operations you do or commands you run, will start getting recorded now.
- Do the "similar" operation. Lets consider substituting all the occurrences of the word "BAD" with "GOOD" as "similar" operation. In command mode, type '%s/BAD/GOOD/g'
- Save the file and move to the next file: 'wn' ( 'w' for save, 'n' is to open next file.)
- Now, run the recording. '@' followed by <any character> will run the recording. In our example, its '@s'. Basically, this particular recording is calling itself! which makes it recursive.
- End the recording: Type just the single character 'q' to end recording.We have the first recording encapsulated inside itself along with the commands which need to be run for all the files.
- Run the recording to finish: Type '@s'. Now, all the files having the word 'BAD' will have replaced with the word 'GOOD', in a jiffy :)
Vim Sessions
Create session
:mksession ~/mysession.vim
source that vim file to get the old session
:source ~/mysession.vim
$ vim -S ~/mysession.vim