Thursday, December 5, 2013

Running UNIX shell commands from vim










To run a single UNIX command use the command:
   :!UNIX_command
You can start a shell from within vi and use it as you would your usual UNIX environment, then exit the shell and return to vi
To start up a shell enter the command:
   :sh
The type of shell that is started is determined by the $SHELL variable. You can specify that some other shell is to be started by setting the vi shell option
Return to using vi by entering the command exit or Ctrl-D


Shell Functions



:! cmdExecutes shell command cmd; you can add these special characters to indicate:% name of current file# name of last file edited
!! cmdExecutes shell command cmd, places output in file starting at current line
:!!Executes last shell command
:r! cmdReads and inserts output from cmd
:f fileRenames current file to file
:w !cmdSends currently edited file to cmd as standard input and execute cmd
:cd dirChanges current working directory to dir
:shStarts a sub-shell (CTRL-d returns to editor)
:so fileReads and executes commands in file (file is a shell script)
!Motion_cmdSends text from current position to Motion Command to shell command cmd
!}sortSorts from current position to end of paragraph and replaces text with sorted text

Displaying vi option values

To display the current value of all options enter the command:
:set all
To display the value of those options whose values have been reset from their default enter the command:

:set


Change an option value temporarily

To change a the value of an option temporarily:
   :set option_name
or:
   :set option_name=value

This sets the value of the option until you quit vi.

Change an option value permanently

To make a lasting change, create a file named .exrc, containing the set commands, in your home directory. The next time you use vi these options will take effect and will remain in force until you edit the .exrcfile to change them.

You can, of course, temporarily change the value of any option.

Examples of setting vi options permanently

1. To set a number of options place the set commands in the file .exrc.
   set ic
   set number
   set sh=/usr/local/bin/Tcsh
   set wm=5
This sets vi to:
   - ignore the case of characters in searches
   - display line numbers
   - use the TC shell to execute UNIX commands
   - wrap text five characters from the right edge
     of the screen
2. Options can also be set using the environment variable EXINIT.
   setenv EXINIT 'set ic number sh=/usr/local/bin/Tcsh wm=5'
For the C and TC shell user, this sets the same options as in the example above.

If there is a .exrc file owned by you in your home directory or the current directory, vi will take its option values from this and not from the EXINIT environment variable.

No comments:

Post a Comment

Labels