- "]s" - move to the next mispelled word
- "[s" - move to the previous mispelled word
- "zg" - add a word to the dictionary
- "zug" - undo the addition of a word to the dictionary
- "z=" - view spelling suggestions for a mispelled word
Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts
Monday, March 7, 2011
Vim Usage: spell checking
Yesterday, I needed to type a document using Latex. This is better to use Latex for document works especially for those needed proper layout. Latex provide a good layout function comparing with MS Word. However, for me, I probably will make many typo error and in fact I typed lots of misspell word yesterday. Sometimes it is easy to find out but sometimes not. MS Word provides spell checking already. Fortunately, Vim 7 provides this function also. To enable it, use ":set spell" in command mode. To turn it off, type ":set nospell". If you are using Vim in terminal, you can see the misspell words are highlighted. You can use move key to go to the wrong words and fix them. In fact, you can use "]s" that is a useful function which moves the cursor to the next misspelled word. To move to previous misspell word, use "[s". When your cursor in over the wrong word, you can press "z=" to ask Vim to provide suggestions. I tried that and I found Vim spell checking are quite accurate, most likely the first one will fix my typo. If you want more information about spell checking function, use ":help spell" to read the documentation.
Labels:
vim
Friday, February 25, 2011
Vim Usage: using clipboard
Vim is a very strong and powerful text processing software. People in Linux should hear this name although they may not know how to use it. However, when people is using vim, they may face a problem that is easy and common. But it is a strange question also, this is about clipboard of vim.
Let's do a simple experiment. Launch a browser and copy the text from a web page. Then, in vim, use "p" to paste the content as usual. Huh...you discover, there is nothing pasted here. Hoho...the reason is vim contains multiple clipboards and they are independent to system clipboard. Thus, when you copy the text from browser, it will save the content in the system clipboard. When you use "p" to paste the content in vim, vim is reading vim's clipboard.
Then, how to easy to copy and paste the content from system clipboard? Simple, you can use short-cut command "shift - insert" to achieve it although you need to move your hand far away. If you are using GVim, you can also use middle button. Okay, it is simple and now you learn the basic of vim clipboard.
As you said before, vim contains multiple clipboards. The objective for this is for user to do some complicated text processing. Now, let's talk about ":reg". When you type this command in command mode, you can see a list of output which is familiar to you. Right, they are the content you copied. You can see that vim contains many clipboard and it is indexed by characters, numbers and symbols. All the clipboard's name starts with '"'. Wow~ can you see the content indexed by '"+'. This store the content that you have just copied from browser. You are right. '"+' is System clipboard.
At this time, you should be able to view content of all clipboards. Now, we are going to paste content from specific clipboard. We all know that "p" is the command for pasting. But if we need to paste from certain clipboard, "p" is not enough. To specify a clipboard, we need to tell vim explicitly using '"[clipboard index]'. For example, we want to paste the content from system clipboard, we can press '"+p' in command mode that means change to '"+' clipboard (system clipboard) and then paste. Now, you can see you pasted the content from the web page you copied.
Now, you know how to paste content from certain clipboard. Then how to copy content to certain clipboard, for smart user, I think you know the answer now. The step is "1) specify a clipboard" and then "2) copy the content". You know how to specify the clipboard. To copy content in vim, use "y". For example, you want to copy the content to system clipboard so that you can paste the content to other application. First, use '"+yy' to change the clipboard to system clipboard and then copy the line (for more usage of yank[y], please read in the help ":help y"). Then you can paste the content in other application or you can check if it is success by ":reg" command.
Let's do a simple experiment. Launch a browser and copy the text from a web page. Then, in vim, use "p" to paste the content as usual. Huh...you discover, there is nothing pasted here. Hoho...the reason is vim contains multiple clipboards and they are independent to system clipboard. Thus, when you copy the text from browser, it will save the content in the system clipboard. When you use "p" to paste the content in vim, vim is reading vim's clipboard.
Then, how to easy to copy and paste the content from system clipboard? Simple, you can use short-cut command "shift - insert" to achieve it although you need to move your hand far away. If you are using GVim, you can also use middle button. Okay, it is simple and now you learn the basic of vim clipboard.
As you said before, vim contains multiple clipboards. The objective for this is for user to do some complicated text processing. Now, let's talk about ":reg". When you type this command in command mode, you can see a list of output which is familiar to you. Right, they are the content you copied. You can see that vim contains many clipboard and it is indexed by characters, numbers and symbols. All the clipboard's name starts with '"'. Wow~ can you see the content indexed by '"+'. This store the content that you have just copied from browser. You are right. '"+' is System clipboard.
At this time, you should be able to view content of all clipboards. Now, we are going to paste content from specific clipboard. We all know that "p" is the command for pasting. But if we need to paste from certain clipboard, "p" is not enough. To specify a clipboard, we need to tell vim explicitly using '"[clipboard index]'. For example, we want to paste the content from system clipboard, we can press '"+p' in command mode that means change to '"+' clipboard (system clipboard) and then paste. Now, you can see you pasted the content from the web page you copied.
Now, you know how to paste content from certain clipboard. Then how to copy content to certain clipboard, for smart user, I think you know the answer now. The step is "1) specify a clipboard" and then "2) copy the content". You know how to specify the clipboard. To copy content in vim, use "y". For example, you want to copy the content to system clipboard so that you can paste the content to other application. First, use '"+yy' to change the clipboard to system clipboard and then copy the line (for more usage of yank[y], please read in the help ":help y"). Then you can paste the content in other application or you can check if it is success by ":reg" command.
Remarks: Some people tell me that it cannot be used. You need to check whether your vim compiled with clipboard support. To check it, use command in terminal
$ vim --version
If you can find +clipboard or +xterm_clipboard, you can use it properly. Otherwise, you need to compile it again with clipboard support. In ubuntu, you can use
$ sudo apt-get install vim-gnometo obtain a vim that support these two functions
Labels:
vim
Wednesday, February 23, 2011
Vim Usage: viminfo
Yesterday, I talked about the session in vim which is used to store the layout, tabs, files opened records. How about mapping, abbreviate, mark and options, history, register, recent search records?
All of these values are stored in viminfo. To create a viminfo file manually, use ":wviminfo [filename]". In fact, before exiting vim, vim will automatically create a .viminfo file under home directory. To use ":wviminfo" is to create a viminfo explicitly. It is because .viminfo will be updated every vim exits. Who know what you do after using vim. Thus, it is better to create a viminfo by yourself such that the viminfo file you created will not be changed no matter how you use vim later (unless you save the viminfo file with same file name). Which ":wviminfo" will save what values and numbers of records depends on "viminfo" option setting. You can check it by ":set viminfo". If you want to edit it, you can read the manual by using ":help viminfo".
Now, to restore viminfo, just use ":rviminfo [filename]" command. Then, you can restore all values and registers. Thus, you may need to use following command if you need to work on a project to restore the environment with viminfo.
:wviminfo project1.viminfo " create a viminfo file
:qa " quit vim
Enter vim again, you will see a blank windows. Then, restore the environment from viminfo
:rviminfo project1.viminfo " read viminfo file
Now, you can get back the vim environment you exit before. If you want to restore the layout and tabs, please read my previous post about using session.
All of these values are stored in viminfo. To create a viminfo file manually, use ":wviminfo [filename]". In fact, before exiting vim, vim will automatically create a .viminfo file under home directory. To use ":wviminfo" is to create a viminfo explicitly. It is because .viminfo will be updated every vim exits. Who know what you do after using vim. Thus, it is better to create a viminfo by yourself such that the viminfo file you created will not be changed no matter how you use vim later (unless you save the viminfo file with same file name). Which ":wviminfo" will save what values and numbers of records depends on "viminfo" option setting. You can check it by ":set viminfo". If you want to edit it, you can read the manual by using ":help viminfo".
Now, to restore viminfo, just use ":rviminfo [filename]" command. Then, you can restore all values and registers. Thus, you may need to use following command if you need to work on a project to restore the environment with viminfo.
:wviminfo project1.viminfo " create a viminfo file
:qa " quit vim
Enter vim again, you will see a blank windows. Then, restore the environment from viminfo
:rviminfo project1.viminfo " read viminfo file
Now, you can get back the vim environment you exit before. If you want to restore the layout and tabs, please read my previous post about using session.
Labels:
vim
Tuesday, February 22, 2011
Vim Usage: use session
Many software has a function that it will restart the last status when you launch the program again. For example, eclipse will restore the layout, perspective, files opened when you launch it again.
When we are developing a project, this is not a short-period job. If you exit vim and launch it again from scratch, I think you will pull your hair out and want to kill vim immediately as this is a troublesome work!
Session is a good tool that store windows layout. For example, you may have many tabs and splitting windows. You can store them into session so that when you launch it, you can have such layout again.
To create a session, you can use ":mksession [filename]". If filename is omitted, vim will create a file named session.vim. In fact, session is a vim script and you can edit it if you like. You also can view the session file so that you can have better understanding how session works.
Which value is stored depends on 'sessionoptions'. By default, 'sessionoptions' is set to be "blank,buffers,curdir,folds,help,options,tabpages,winsize". You can check this with ":set sessionoptions" command in vim. That means vim will store current blank windows, buffers, current directory, folding, help windows, current options, tabs and windows size when you make a session file.
Remark: if you are using windows version vim and you want this session file can be execute under windows and vim, you need to add two more 'sessionoptions'. They are "slash" and "unix". "slash" is to replace back slash with slash and "unix" is to store the line break in unix format (There is some different between dos format and unix format, if you want to know more, you can read my previous post).
If you don't want to store current directory path, you want to set the current directory is same as location of session file. To do that, you need to modify 'sessionoptions' that to remove "curdir" and add "sesdir". After that, when you load the session file, the location of session will be set to be current working directory. Also, after this setting, all path in the session file will become relative path instead of absolute path.
Ok, we talked a lot of advantage of using session and how to create a session. How can we use it? The answer is that it is easy, you can use ":source [sessionfile]" to load the session, or you can use in this command in terminal
Question: Does vim contain this function?
Answer: Yes for sure, otherwise, there are lots of people complaining it. To archive this function, we need to use session to store the status before you exit and restore it when launching.
When we are developing a project, this is not a short-period job. If you exit vim and launch it again from scratch, I think you will pull your hair out and want to kill vim immediately as this is a troublesome work!
Session is a good tool that store windows layout. For example, you may have many tabs and splitting windows. You can store them into session so that when you launch it, you can have such layout again.
To create a session, you can use ":mksession [filename]". If filename is omitted, vim will create a file named session.vim. In fact, session is a vim script and you can edit it if you like. You also can view the session file so that you can have better understanding how session works.
Which value is stored depends on 'sessionoptions'. By default, 'sessionoptions' is set to be "blank,buffers,curdir,folds,help,options,tabpages,winsize". You can check this with ":set sessionoptions" command in vim. That means vim will store current blank windows, buffers, current directory, folding, help windows, current options, tabs and windows size when you make a session file.
Remark: if you are using windows version vim and you want this session file can be execute under windows and vim, you need to add two more 'sessionoptions'. They are "slash" and "unix". "slash" is to replace back slash with slash and "unix" is to store the line break in unix format (There is some different between dos format and unix format, if you want to know more, you can read my previous post).
If you don't want to store current directory path, you want to set the current directory is same as location of session file. To do that, you need to modify 'sessionoptions' that to remove "curdir" and add "sesdir". After that, when you load the session file, the location of session will be set to be current working directory. Also, after this setting, all path in the session file will become relative path instead of absolute path.
Ok, we talked a lot of advantage of using session and how to create a session. How can we use it? The answer is that it is easy, you can use ":source [sessionfile]" to load the session, or you can use in this command in terminal
$ vim -S [session file]
Labels:
vim
Monday, February 21, 2011
Vim Usage: how to press your ``Esc''
In vim, how would you press the Esc key to switch from insert mode to command mode? For sure, it is a key that you need to move your hand out of alphabet keys. Some people would slap the key..haha..it sounds a little bit violence. For me, I use my little finger, huh, some people think I have a very big hand or a very long little finger.
If you have read Vim Tips, you probably know what's going on. In fact, I am pressing cap lock instead of Esc. In Linux, it is convenient to set your key. You can use xmodmap to modify X windows layout. Since I seldom to use Cap lock, and in order to speed up using vim, I swap Esc and Cap lock. To do that, you can create a .Xmodmap with following content under your home directory
Then execute
Then you will realise that your caps lock become Esc key already. For sure, if you like this mapping, you need to start the script on Gnome start up to take the effect.
If you have read Vim Tips, you probably know what's going on. In fact, I am pressing cap lock instead of Esc. In Linux, it is convenient to set your key. You can use xmodmap to modify X windows layout. Since I seldom to use Cap lock, and in order to speed up using vim, I swap Esc and Cap lock. To do that, you can create a .Xmodmap with following content under your home directory
remove Lock = Caps_Lock keysym Escape = Caps_Lock keysym Caps_Lock = Escape add Lock = Caps_Lock
Then execute
$ xmodmap ~/.Xmodmap
Then you will realise that your caps lock become Esc key already. For sure, if you like this mapping, you need to start the script on Gnome start up to take the effect.
Sunday, February 20, 2011
Vim Usage: encrypt your text file
Computer security is a very important issue when the computer is public. File in computer can be easy accessed by other people if you don't protect your file in a serious manner. Vim provides a security function that you can set a password to your file.
To encrypt a file, you need to start vim with -x parameter. e.g.
Vim then will ask you to enter a password for this file. The password will be hidden with stars to prevent from other people seeing your password. Thus, you need to enter the same password again to avoid typo mistakes.
Now you can edit your file normally as usual. When you save the file, the file will automatically be encrypted.
When you use vim to open this file, vim will ask you to input the password in order to authentic the editor (you won't need to put -x in the command). You also can use ":edit" command to edit encrypted file.
If you use other editors such as notepad to view the encrypted file, you should see messy symbols. If you input a wrong password, you also will get error. Vim doesn't provide password verification, every key can open the file, but only correct one will decrypt the file and the decrypted content is meaningful. This method makes it more hard to crack.
If you want to clear the password, you can use ":set key=" to set the key to be null. Then you save the file, this file will not be encrypted any more. In fact, you can use ":set key=[your password]" to set your key. However, it is bad since every body can see what you typed on the screen. To avoid this problem, you can use ":X" command which is same as -x parameter in vim to ask you input password.
There is other issue about the security of vim. The encryption of vim is enough to prevent the people want to view your file, but if people was a professional hacker, he can crack your file with using many time. Also, vim will create a swap file when you edit it. This swap file can be viewed by the super user. Thus, we can avoid this by using "-n" to not create swap file for vim. You can use the command to launch vim:
If you are editing the file, you can use ":setlocal noswapfile" to stop create swap file. However, since there is no swap file, file recovery is difficult to achieve. One way to solve it is to save your file in shorter time. :-)
To encrypt a file, you need to start vim with -x parameter. e.g.
$ vim -x password.txt"
Vim then will ask you to enter a password for this file. The password will be hidden with stars to prevent from other people seeing your password. Thus, you need to enter the same password again to avoid typo mistakes.
Now you can edit your file normally as usual. When you save the file, the file will automatically be encrypted.
When you use vim to open this file, vim will ask you to input the password in order to authentic the editor (you won't need to put -x in the command). You also can use ":edit" command to edit encrypted file.
If you use other editors such as notepad to view the encrypted file, you should see messy symbols. If you input a wrong password, you also will get error. Vim doesn't provide password verification, every key can open the file, but only correct one will decrypt the file and the decrypted content is meaningful. This method makes it more hard to crack.
If you want to clear the password, you can use ":set key=" to set the key to be null. Then you save the file, this file will not be encrypted any more. In fact, you can use ":set key=[your password]" to set your key. However, it is bad since every body can see what you typed on the screen. To avoid this problem, you can use ":X" command which is same as -x parameter in vim to ask you input password.
There is other issue about the security of vim. The encryption of vim is enough to prevent the people want to view your file, but if people was a professional hacker, he can crack your file with using many time. Also, vim will create a swap file when you edit it. This swap file can be viewed by the super user. Thus, we can avoid this by using "-n" to not create swap file for vim. You can use the command to launch vim:
$ vim -x -n filename.txt
If you are editing the file, you can use ":setlocal noswapfile" to stop create swap file. However, since there is no swap file, file recovery is difficult to achieve. One way to solve it is to save your file in shorter time. :-)
Labels:
encryption,
vim
Saturday, February 19, 2011
Vim Usage: generate HTML format source code with syntax hightlight
In vim, this supports to have syntax highlight which makes the program attractive, at least it is not dull and easy to debug. Sometimes, you may want to publish your code online so people can view your program. One way is to put the source code to web host directly, so people can download your source code and view it in their own favourite editor. The other way is to publish your code in HTML format, thus people can view it anywhere once there is a web browser.
Vim provides more function on that, it can generate a HTML from your program with syntax highlight. You just need to type the following in command mode:
":TOhtml"
Then, you may need to wait a little bit time which depends on your size of the program. Once it is done, it will generate a html named program_name.html and you can save it by ":w" and publish it if you want. Cool~
But, if you want to modify the style, you will think it is a trouble. Vim by default won't output using CSS, for example, it uses font tags instead of using style. For formatting, it use pre tags instead of replacing space with and newline with
tag. To force vim output using css and not pre, you can set it by ":let html_use_css=1" and ":let html_no_pre=1". If you want to output standard xhtml also, add ":let use_xhtml=1".
Vim provides more function on that, it can generate a HTML from your program with syntax highlight. You just need to type the following in command mode:
":TOhtml"
Then, you may need to wait a little bit time which depends on your size of the program. Once it is done, it will generate a html named program_name.html and you can save it by ":w" and publish it if you want. Cool~
But, if you want to modify the style, you will think it is a trouble. Vim by default won't output using CSS, for example, it uses font tags instead of using style. For formatting, it use pre tags instead of replacing space with and newline with
tag. To force vim output using css and not pre, you can set it by ":let html_use_css=1" and ":let html_no_pre=1". If you want to output standard xhtml also, add ":let use_xhtml=1".
Labels:
vim
Friday, February 18, 2011
Vim Usage: add more syntax to support special extension
Vim is able to detect your programming language in the file you open. The mechanism for vim to do that is to check extension of the file. However, some files are not included by default in vim like .inc. To make vim can correctly detect the syntax with your file extension. You need to set file type explicitly. In command mode, input ":set filetype=php" to tell vim that the current opened file is using php syntax.
However, it is annoying that you need to type this command whenever you open .inc file. To do that permanently, modify filetype.vim under ~/.vim folder which store all vim settings and plugin for current session user. Then, find "au BufNewFile,BufRead *.php,*.php\d,*.phtml,*.ctp setf php" which mean for the file with extension *.php, *.phtml and *.ctp, vim will treat them as php syntax. Thus, you just need to add *.inc to this line, that becomes "au BufNewFile,BufRead *.php,*.php\d,*.phtml,*.ctp,*.inc setf php", save and then reopen .inc file. You can see it is using php syntax now.
If you have some syntax you want to diy, you can create a syntax file under ~/.vim/syntax folder. Then in filetype.vim add "au BufNewFile,BufRead *.inc setf inc". Save it and you will use your own inc syntax format for inc extension files. Nice!
However, it is annoying that you need to type this command whenever you open .inc file. To do that permanently, modify filetype.vim under ~/.vim folder which store all vim settings and plugin for current session user. Then, find "au BufNewFile,BufRead *.php,*.php\d,*.phtml,*.ctp setf php" which mean for the file with extension *.php, *.phtml and *.ctp, vim will treat them as php syntax. Thus, you just need to add *.inc to this line, that becomes "au BufNewFile,BufRead *.php,*.php\d,*.phtml,*.ctp,*.inc setf php", save and then reopen .inc file. You can see it is using php syntax now.
If you have some syntax you want to diy, you can create a syntax file under ~/.vim/syntax folder. Then in filetype.vim add "au BufNewFile,BufRead *.inc setf inc". Save it and you will use your own inc syntax format for inc extension files. Nice!
Labels:
vim
Thursday, February 17, 2011
Vim Usage: batch comment out in program code
When doing programming, commenting out some code for debugging is needed in many cases. In fact, most IDE like my favourite java IDE, Eclipse, they include this function already. You can select the lines you need to comment out/undo comment by using their fancy edit tab. In vim, how can you do that?
There are so many solutions in vim.
To uncomment out the lines, use "Ctrl - v" again to enter block selection mode. Highlight all comment symbol, then press "d". All the characters you selected will be deleted. Cool!
To comment lines, ":[startline], [endline]s/^/[comment symbols]/g"
This means replace "^" with comment symbols from startline to endline globally. What "^" means is the beginning of line. This is the same meaning in Regex (Regex is very useful in vim, and in *nix system).
To uncomment lines, ":[startline], [endline]s/^[comment symbols]//g"
This means replace comment symbols which are located at the beginning of line to empty. In order words, to delete all comment symbols which are located at the beginning of line.
Example (remember to use appropriate seperator):
There are so many solutions in vim.
Method 1:
If you are using your own vim and you need to do commenting out code very often, I suggest you to use vim plugin - comments. The web site includes the installation guide and how to use it. You can check it.Method 2:
You can use block selection mode in vim. To enter block selection mode, press "Ctrl - v". Then, use arrow keys or "h j k l" to move the cursor and select the lines you need to comment out. Then, press "I" which mean insertion at the beginning of line. Depending on your programming language, you can input "//" for C, C++ or java, etc, or "#" for shell script, python, etc. Finally, press "Esc" to escape the insert mode. See...Vim will automatically insert the comment symbol for the lines you selected.To uncomment out the lines, use "Ctrl - v" again to enter block selection mode. Highlight all comment symbol, then press "d". All the characters you selected will be deleted. Cool!
Method 3:
Last time you talked about mastering replacing function in Vim. You can use this to comment lines also. (Want to have more detail, please refer to my post)To comment lines, ":[startline], [endline]s/^/[comment symbols]/g"
This means replace "^" with comment symbols from startline to endline globally. What "^" means is the beginning of line. This is the same meaning in Regex (Regex is very useful in vim, and in *nix system).
To uncomment lines, ":[startline], [endline]s/^[comment symbols]//g"
This means replace comment symbols which are located at the beginning of line to empty. In order words, to delete all comment symbols which are located at the beginning of line.
Example (remember to use appropriate seperator):
- add // within line 10 and line 20: ":10,20s#^#//#g"
- uncomment within line 10 and line 20: ":10,20s#^//##g"
- add # within line 10 and line 20: ":10,20s/^/#/g"
- uncomment within line 10 and line 20: ":10,20s/^#//g"
- add // within line 10 and line 20: ":10,20s+^+//+g"
- uncomment within line 10 and line 20: ":10,20s+^//++g"
- add # within line 10 and line 20: ":10,20s+^+#+g"
- uncomment within line 10 and line 20: ":10,20s+^#++g"
Labels:
vim
Wednesday, February 16, 2011
Vim Usage: mastering the replace function
Replacing text is a daily using for text editing. For most visual editor, you probably can click edit->replace to achieve the goal. Right now, it maybe a hard time that you have to do it in vim. What you need to remember is ":s" which stands for substitution. To use it, you need to provide the search text and replace text like ":s/abc/123". When you press enter, you may not be satisfied. This command means to substitute first occurred "abc" with "123" in current line. If you want to do it in whole line, you need to put one more parameter (/g). Thus the command is ":s/abc/123/g" which is substituting "abc" with "123" globally within the line.
In fact, vim provides lots of flexibility for users. You can combine this with range. That means you can replace text within a range of lines. You can type ":n, $s/abc/123" which replace the first occurrence of "abc" to "123" for each line in the range. If you need to replace all occurrence between line n and the end of file, you should use ":n,$s/abc/123/g" to replace them globally from line n to the end. Remember, "n" is line no, if you want to set the range from current line to the end of file, you can use ".". "." is a very useful key in vim.
Now, you may want to replace within whole file. You may think you can use ":1,$s/abc/123" to specify replace from line 1 to end line. However, you can use other one if you like to save some key strokes. ":%s/abc/123" is to replace first occurred "abc" with "123" in every line. ":%s/abc/123/g" is to replace all "abc" with "123" in every line. (Actually, if you want, you may want to remember two more which do the same action: ":g/abc/s//123" and ":g/abc/s//123/g")
Last problem, what if I want to replace the text with "/"? Should I use ":s/abc//123//" to replace "abc/" with "123/"? However, "/" in this command is used for separator and vim can't interpret this command definitely. Luckily, vim doesn't force you to use "/" with separator. You can use "#" or "+". For example, to replace "/home/user1" with "/home/user2", you can use ":s#/home/user1#/home/user2#g" or ":s+/home/user1+/home/user2+g".
Conclusion:
If you just need to find a function which is same as replace function in usual editors, ":%s/target/replace/g" is enough for you.
Summary:
Now, you know what the problem is and for sure, what you need to do is to delete all "^M" in the document. Thus, please use ":%s/^M//g" in command mode. Be careful, here "^" is not same as shift 6. You need to type: ":%s/[ctrl-v][ctrl-M]//g".
In fact, vim provides lots of flexibility for users. You can combine this with range. That means you can replace text within a range of lines. You can type ":n, $s/abc/123" which replace the first occurrence of "abc" to "123" for each line in the range. If you need to replace all occurrence between line n and the end of file, you should use ":n,$s/abc/123/g" to replace them globally from line n to the end. Remember, "n" is line no, if you want to set the range from current line to the end of file, you can use ".". "." is a very useful key in vim.
Now, you may want to replace within whole file. You may think you can use ":1,$s/abc/123" to specify replace from line 1 to end line. However, you can use other one if you like to save some key strokes. ":%s/abc/123" is to replace first occurred "abc" with "123" in every line. ":%s/abc/123/g" is to replace all "abc" with "123" in every line. (Actually, if you want, you may want to remember two more which do the same action: ":g/abc/s//123" and ":g/abc/s//123/g")
Last problem, what if I want to replace the text with "/"? Should I use ":s/abc//123//" to replace "abc/" with "123/"? However, "/" in this command is used for separator and vim can't interpret this command definitely. Luckily, vim doesn't force you to use "/" with separator. You can use "#" or "+". For example, to replace "/home/user1" with "/home/user2", you can use ":s#/home/user1#/home/user2#g" or ":s+/home/user1+/home/user2+g".
Conclusion:
If you just need to find a function which is same as replace function in usual editors, ":%s/target/replace/g" is enough for you.
Summary:
- ":s/abc/123" - replace first occurred "abc" with "123" in current line
- ":s/abc/123/g" - replace all "abc" with "123" in current line
- ":n, $s/abc/123" - replace first occurred "abc" with "123" from line n to end of file
- ":n, $s/abc/123/g" - replace all "abc" with "123" from line n to end of file
- ":., $s/abc/123/g" - replace all "abc" with "123" from current line to end of file
- ":%s/abc/123" - replace first occurred "abc" with "123" in every line
- ":%s/abc/123/g" - replace all "abc" with "123" in every line
Remark:
Do you remember that you have introduced two commands to convert dos format file to *nix format? One is using vim to set the encoding and the other one is using dos2unix command. Huh........you actually can do that using replace function in vim. First, what is the different between dos format and *nix format file? The answer is that the new line characters are different in the system. DOS uses (0A0D) to represent the end of line while *nix uses (0A) to represent a new line. Thus, if you are viewing a dos file in, you will see many "^M" in the end of each line which is a extra character for windows to know that which is a new line. [Vice versa, if you are viewing *nix file under windows, you may see whole file concatenated in one line if you are using notepad. The reason is that windows doesn't know it is a time to change to new line without (0D)]Now, you know what the problem is and for sure, what you need to do is to delete all "^M" in the document. Thus, please use ":%s/^M//g" in command mode. Be careful, here "^" is not same as shift 6. You need to type: ":%s/[ctrl-v][ctrl-M]//g".
Labels:
vim
Tuesday, February 15, 2011
Vim Usage: Copy and paste multiple lines across different files
If you use vim, it is better to avoid using mouse. (I think that's good and convenience if you can control your vim without leaving your hand out of keyboard. In fact, I think it is the most important reason to use vim.) However, you may need to copy multiple lines in one document and paste them into same document or other files. Probably, you can use mouse and highlight multiple lines, then edit->copy. I think this is ok for vim newbie and definitely this will take some times for hand switching from keyboard and mouse. Here is a better way, at least I use this one. (If you have better command, please feel free to give a comment :-) )
I will use "m" to help me. "m" is used for making a marker. You can use it in your text file. Then, you can jump to any line. In command mode, you press "m + [any letter (a-z)]". It will make a marker with the letter you choose. For example, you can move to line 10 and type "ma" (I usually use a as my marker). Then you make a marker on line 10. Once, you move to other place within the file. You can type "'a" to go back your marker a (line 10 in our example). Thus, it helps you to have better navigation experience in vim.
But, how can I make use of marker to copy and paste multiple lines? Remember, when you need to copy something in vim, you have to use yank "y". You can go to any lines other than line 10, then press "y'a", which means you copy from current location to marker a (line 10). You then can you paste command "p" to paste the thing you just copied.
Then, our next step is to pasting the copied content to other files rather than current file. To edit other file, you can use ":ex [filename]". Then you should be editing other file right now, you can press "p" to paste. Nice!
Summary:
I will use "m" to help me. "m" is used for making a marker. You can use it in your text file. Then, you can jump to any line. In command mode, you press "m + [any letter (a-z)]". It will make a marker with the letter you choose. For example, you can move to line 10 and type "ma" (I usually use a as my marker). Then you make a marker on line 10. Once, you move to other place within the file. You can type "'a" to go back your marker a (line 10 in our example). Thus, it helps you to have better navigation experience in vim.
But, how can I make use of marker to copy and paste multiple lines? Remember, when you need to copy something in vim, you have to use yank "y". You can go to any lines other than line 10, then press "y'a", which means you copy from current location to marker a (line 10). You then can you paste command "p" to paste the thing you just copied.
Then, our next step is to pasting the copied content to other files rather than current file. To edit other file, you can use ":ex [filename]". Then you should be editing other file right now, you can press "p" to paste. Nice!
Summary:
- "m + [a - z]" - create a marker with the letter [a - z]
- "'m +[a - z]" - go to marker [a - z]
- "y'[a - z]" - yank from current line to marker [a - z]
- ":ex [filename]" - edit other file
Labels:
vim
Monday, February 14, 2011
Vim Usage: Convert to Unix format from dos format
When a dos format text file is edited by *nix system, a "^M" character will be added at the end of each line. This is because there is a difference between line break of dos system and that of *nix system.
If you are vim user, you certainly will open the file with vim. You probably will see some information that ""dos.txt" [dos] 55L, 33C". [dos] means you are editing a dos format file. If you see [mac] instead of [dos], it means it is created under mac environment.
Some time, you will get an error about wrong interpreter if you wrote a shell under windows and then execute under *nix system. One possible reason is due to "^M" character, thus you need to delete all this character. There are many methods to do so. Previously, I introduced a unix tool to convert dos file to unix file using dos2unix (read this post). This time, I use vim since it is built-in *nix already.
In command mode
If you are vim user, you certainly will open the file with vim. You probably will see some information that ""dos.txt" [dos] 55L, 33C". [dos] means you are editing a dos format file. If you see [mac] instead of [dos], it means it is created under mac environment.
Some time, you will get an error about wrong interpreter if you wrote a shell under windows and then execute under *nix system. One possible reason is due to "^M" character, thus you need to delete all this character. There are many methods to do so. Previously, I introduced a unix tool to convert dos file to unix file using dos2unix (read this post). This time, I use vim since it is built-in *nix already.
In command mode
: set ff
[this will file format of current file] : set fileformat=unix
Labels:
vim
Sunday, February 13, 2011
Vim Usage: remove all empty lines
In text editing, especially when you copied the content from some website, you might want to delete all empty lines in your text. In the past, I will do something stupid which is to delete the empty line one by one. It takes time and I use this command to achieve my goal
dd, (go to next empty line), then "." ,until you delete all the empty line.
Explanation:
dd, (go to next empty line), then "." ,until you delete all the empty line.
Explanation:
- dd - delete whole line
- . - repeat the last command (I like this one, very useful)
:g/^$/d
Feel free to use it if you need to delete all empty lines :-)
Labels:
vim
Friday, February 11, 2011
Vim Usage: Change upper case/lower case
I am addicted to use vim all the time, in programming (except eclipse), in text editing. I like to use it since it increase my working progress and the command helps me a lot.
I am going to post some notes when I learn some new useful command/macro.
One useful function is to change letter case. For one character, you can simply use "~" to change to case from upper to lower or vice versa.
However, what if I want to change whole passage to UPPERCASE or lowercase. We can use gU / gu command.
1. Change whole passage to lower case
you can use "ggguG" in command mode.
Explain:
2. You can use motion command to change case
guw、gue、gUw、gUe
3. some special combinations
I am going to post some notes when I learn some new useful command/macro.
One useful function is to change letter case. For one character, you can simply use "~" to change to case from upper to lower or vice versa.
However, what if I want to change whole passage to UPPERCASE or lowercase. We can use gU / gu command.
1. Change whole passage to lower case
you can use "ggguG" in command mode.
Explain:
- gg - go to the first line and first character
- gu - change letter to lower case
- G - go to the last character of last line
2. You can use motion command to change case
guw、gue、gUw、gUe
3. some special combinations
- gUU - change current line to uppercase
- 10gUU - change 10 lines including current line to uppercase
- gU0 - change first character of current line to uppercase
- gU$ - change last character of current line to uppercase
- gUG - change character from current position to last charactor of last line
Saturday, January 8, 2011
Taglist for vim
ctags is a very important tools for programmers who use vim as the major editor. You can install taglist plugin for vim. However, ctags program is not installed by default in Ubuntu. Thus, it shows the following error when lauching vim with taglist.
To solve this, for sure we need to install ctags. To do so
Taglist: Exuberant ctags (http://ctags.sf.net) not found in PATH. Plugin is not loaded.
To solve this, for sure we need to install ctags. To do so
$ sudo apt-get install exuberant-ctags
`gnome-256color' not known
When using vim, I usually set it to be gnome-256color to have better appearance.
However, it showed an error to me when I launched vim. The error is :
To solve this problem, we need to install the corresponding package
However, it showed an error to me when I launched vim. The error is :
E558: Terminal entry not found in terminfo
'gnome-256color' not known. Available builtin terminals are:
builtin_riscos
builtin_amiga
builtin_beos-ansi
builtin_ansi
builtin_pcansi
builtin_win32
builtin_vt320
builtin_vt52
builtin_xterm
builtin_iris-ansi
builtin_debug
builtin_dumb
defaulting to 'ansi'
To solve this problem, we need to install the corresponding package
$ sudo apt-get install ncurses-term
Subscribe to:
Posts (Atom)