このエントリーをはてなブックマークに追加


bashのコマンドライン操作をviモードにする方法

vimでテキストファイルを編集し、bashに戻ってきたときに、コマンド入力操作もviのキー操作でしたいな。
と思った方は本資料が役に立つと思います。
以下、bashをviのキー操作で使用できる方法を以下に記します。


動作確認したディストリビューション

$ lsb_release -cd
Description:    Ubuntu 18.04.2 LTS
Codename:       bionic

bashをviモードにするのは簡単

bashをviモードにするには、以下のコマンドを端末(ターミナル)上に入力しEnterキーを押してください。

set -o vi

これでviモードになります。

viモードからもとに戻す場合

viモードから元に戻したい場合は、以下のコマンドになります。

set -o emacs

以下のsetコマンドでの出力を見るとわかるのですが、viモードをonにすると、emacsがoff
emacsモードをonにするとviモードがオフになるからです。

setコマンドで現在のモードを確認できる

以下のコマンドを実行すると現在の編集モードを確認することができます。

set -o

実際に実行したときの出力です。

$ set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off

上記の出力を見ると、viの行がoffになっていますね。

これをonにするために上記で紹介した

set -o vi

とするわけです。
以下は、set -o viを実行した後の出力となります。

$ set -o vi
$ set -o
allexport       off
braceexpand     on
emacs           off
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              on
xtrace          off

setコマンドのオプションを確認

以下の通り、--helpオプションをつけて使用方法を確認しました。
今回紹介した-oオプションにはいろいろな機能があることが確認できます。

$ set --help
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
   Set or unset values of shell options and positional parameters.
   Change the value of shell attributes and positional parameters, or
   display the names and values of shell variables.
   Options:
     -a  Mark variables which are modified or created for export.
<省略>
     -o option-name
         Set the variable corresponding to option-name:
             allexport    same as -a
             braceexpand  same as -B
             emacs        use an emacs-style line editing interface
             errexit      same as -e
             errtrace     same as -E
             functrace    same as -T
             hashall      same as -h
             histexpand   same as -H
             history      enable command history
             igncr        on Cygwin, ignore \r in line endings
             ignoreeof    the shell will not exit upon reading EOF
             interactive-comments
                          allow comments to appear in interactive commands
             keyword      same as -k
             monitor      same as -m
             noclobber    same as -C
             noexec       same as -n
             noglob       same as -f
             nolog        currently accepted but ignored
             notify       same as -b
             nounset      same as -u
             onecmd       same as -t
             physical     same as -P
             pipefail     the return value of a pipeline is the status of
                          the last command to exit with a non-zero status,
                          or zero if no command exited with a non-zero status
             posix        change the behavior of bash where the default
                          operation differs from the Posix standard to
                          match the standard
             privileged   same as -p
             verbose      same as -v
             vi           use a vi-style line editing interface
             xtrace       same as -x
<省略>

端末(ターミナル)起動時にviモードにしたい場合

端末(ターミナル)起動時にviモードにしたい場合は、
$HOME/.bashrcに今回のコマンドを追記すれば起動時にviモードになります。
以下のコマンドで、.bashrcに追記でします。

echo "set -o vi" >> $HOME/.bashrc 

以上、bashのコマンドラインをviモードにする方法でした。



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-03-05 (火) 22:44:33